Imagine these two lists as follows:
seq<-seq(1,16)
L1<-list(A=seq, B=seq, C=seq, D=seq)
v1=c(11,15,17,19,21,22,24,25,26,27,28,28,29,29,30,30)
v2=c(14,19,23,24,26,27,28,29,30,30,31,32,32,32,32,33)
v3=c(2,4,5,6,6,7,8,8,9,9,9,10,10,10,11,11)
v4=c(8,13,17,20,22,24,26,27,28,29,30,31,32,33,33,34)
L2<-list(A=v1,B=v2,C=v3,D=v4)
Now what I want to do is to fit to these lists a specific model with the nls formula, L1$A with L2$A, L1$B with L2$B and so on. I can do it with each vector individually, example:
nls.A<-nls(L2.A~(a/b)*(1-exp(-b*L1.A)),
start=list(a=1, b=0.1),
trace= TRUE, data=data.frame(L1$A, L2$A))
but I would like to do it to the four four elements of each list , and retrieve an object which is also a list with all four ($A, $B, $C, $D) fit results.
I've tried using for loops although they're not very recommended, like this
for (i in L1) {
nls(L2.i~(a/b)*(1-exp(-b*L1.i)),
start=list(a=1, b=0.1),trace= TRUE,
data=data.frame(L1$i, L2$i))
}
But that wouldn't also answer my question (I really need the damned list) I assume that something with lapply would be the ideal, but have no idea on how to do such a thing using lapply. Any thoughts?
Use mapply
with your annonymus function
res <- mapply(function(x,y){
nls(y~(a/b)*(1-exp(-b*x)),
start=list(a=1, b=0.1),
trace= TRUE, data=data.frame(x, y))
},L1,L2, SIMPLIFY=FALSE)