1
votes

I want to feed a function into another function in R.

I have a function that I have named R and it has 1 argument which is PD. I want to create a second function called K which will have 2 arguments namely PD and LGD and it will feed the function R into function K

R = function(PD) {(0.12*(1-exp(-50*PD))/(1-exp(-50)) + 0.24*(1-(1-exp(-50*PD))/(1-exp(-50))))}

K = function(PD, LGD) {LGD*(dnorm(sqrt(1/(1-R))*qnorm(PD) + sqrt(R/(1-R))*qnorm(0.999)))}

K(0.1, 0.4)

When I run the code I get the following error;

R = function(PD) {(0.12*(1-exp(-50*PD))/(1-exp(-50)) + 0.24*(1-(1-exp(-50*PD))/(1-exp(-50))))}

K = function(LGD, PD) {LGD*(dnorm(sqrt(1/(1-R))*qnorm(PD) + sqrt(R/(1-R))qnorm(0.999)))} K = function(PD, LGD) {LGD(dnorm(sqrt(1/(1-R))*qnorm(PD) + sqrt(R/(1-R))*qnorm(0.999)))} K(0.1, 0.4) Error in 1 - R : non-numeric argument to binary operator'

1
You have to call the function: R(PD) Compare: 1-sqrt vs. 1-sqrt(4)jogo
Thank you it is now workingN. Fungura

1 Answers

0
votes

Your function R requires an argument, so you have to modify your K function by adding the PD argument to every R function call:

K <- function(PD, LGD) {
    LGD*(dnorm(sqrt(1/(1-R(PD)))*qnorm(PD) + sqrt(R(PD)/(1-R(PD)))*qnorm(0.999)))
}