I am trying to create a function that calculates recursive form which will applied to a data frame. I have a data frame object which has 6 columns and each one has a 10 rows.
Data <- data.frame()for(i in 1:(10)) {Data <- rbind(Data ,c(A=i+2,B=sqrt(i),C=1/i,D=i/120,E=i/250,F=i+3)); names(Data ) <- letters[1:6]}
I want to use the following recursive function :
f<-function(x,para,c,d,e){ # Constant h=0.25 #para_para<-c() set up the parameters of the model y1=para[1] y2=para[2] y3=para[3] # Terminal condition for the A and B at time T A=0 B=0 # Recursion back to time t steps<-round(d*250,0) for (i in 1:steps){ A= A+ e*x +y1*B B= y2*B+y3 } f = exp(log(c)*x -A + B*h ) return(f) }
Under some specific values the function works :
> para<-c(1,-0.001,0.5)> W<-f(x=0.5,para,c=0.1,d=0.2,e=0.3)> W[1] 4.647528e-15
I want to apply this funtion to my data frame with respect the rows of my data frame with : c=Data$c,d=Data$d,e=Data$e
. I tried this code with some warning:
f(x=0.5,para,c=Data$c,d=Data$d,e=Data$e) [1] 0.6844600 0.4820543 0.3920244 0.3381478 0.3012412 0.2738966 0.2525667 [8] 0.2353113 0.2209680 0.2087918Warning message:In 1:steps : numerical expression has 10 elements: only the first used
In fact thisis not correct, because the function is applied only for the first competent of d which is 2=d*250. The problem is the steps because it changes and takes values from the rows of the data frame. One of the correct way to do it is :
> mapply(function(c,d,e) f(x=0.5,para,c,d,e),c=Data$c,d=Data$d,e=Data$e) [1] 6.844600e-01 1.761008e-01 5.190021e-02 1.609455e-02 5.113622e-03 [6] 1.645010e-03 3.185962e-04 1.031473e-04 3.339030e-05 1.078962e-05
What I want to find is a simple way and direct way just using the f without using mapply.
Thanks in advance.