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.2087918
Warning 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.
I think you know where the problem lies. Modifying your function slightly to see to that it can take the vector arguments:
f1<-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]
# Recursion back to time t
f <- rep(NA, length(c))
for (i in 1:length(c)){
A=0
B=0
steps<-round(d[i]*250,0)
for (j in 1:steps){
A= A+ e[i]*x +y1*B
B= y2*B+y3
}
f[i] = exp(log(c[i])*x -A + B*h )
}
return(f)
}
Now it can take both scalar and vector arguments.
f1(x=0.5,para,c=0.1,d=0.2,e=0.3)
#[1] 4.647528e-15
f1(x=0.5, para, 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 1.645010e-03 3.185962e-04 1.031473e-04 3.339030e-05
#[10] 1.078962e-05
Does this give you what you want?
Using a lambda function inside apply
:
> apply(Data, 1, function (p) f(x=0.5, para, p['c'], p['d'], p['e']))
1 2 3 4 5 6 7 8
6.844600e-01 1.761008e-01 5.190021e-02 1.609455e-02 5.113622e-03 1.645010e-03 3.185962e-04 1.031473e-04
9 10
3.339030e-05 1.078962e-05
You could also rewrite your function so it works more compactly with apply
:
f2<-function(cde, x, para){
c <- cde[1]
d <- cde[2]
e <- cde[3]
# 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)
}
> apply(Data[,c('c','d','e')], 1, f2, x=0.5, para)
1 2 3 4 5 6 7 8
6.844600e-01 1.761008e-01 5.190021e-02 1.609455e-02 5.113622e-03 1.645010e-03 3.185962e-04 1.031473e-04
9 10
3.339030e-05 1.078962e-05
> all.equal(apply(Data[,c('c','d','e')], 1, f2, x=0.5, para),
apply(Data, 1, function (p) f(x=0.5, para, p['c'], p['d'], p['e'])))
[1] TRUE
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.