2 views

I am receiving a "non-numeric argument to binary operator" when executing the below code:

R = function(PD, AVC) {AVC*(0.12*(1-exp(-50*PD))/(1-exp(-50)) +

0.24*(1-(1-exp(-50*PD))/(1-exp(-50))))}

b = function(PD) {(0.11852-0.05478*log(PD))^2}

integrand = function(PD=0.1, AVC =1, LGD = 1,q=0.999) {pnorm(sqrt(1/(1-R(PD, AVC)))*qnorm(PD) +

sqrt(R(PD, AVC)/(1-R(PD, AVC)))*qnorm(q))}

integrate(integrand, lower = 0.999, upper = 1)

K_ES = function(PD,AVC =1, LGD = 1,q=0.999) {LGD/(1-q)*integrate(integrand, lower = q, upper = 1)-PD*LGD}

K_ES(PD=0.1,AVC =1, LGD = 1,q=0.999)

What might be causing this error and how do I resolve it?

by (108k points)

See basically, the integrate() returns a list, not a numeric vector. You probably want the value element:

K_ES = function(PD,

AVC = 1,

LGD = 1,

q = 0.999) {

LGD / (1 - q) * integrate(integrand, lower = q, upper = 1)\$value - PD * LGD

}

K_ES(PD = 0.1, AVC = 1 , LGD = 1, q = 0.999)

# [1] 0.9

If you are a beginner and want to know more about R then do check out the R programming tutorial