beeprogrammer
beeprogrammer

Reputation: 649

Using predict on lm list with confidence interval

I have a linear model fitted to a grouped data which generates a list of objects of type lm. I want to use this linear model to predict the value of y at a given x with a given confidence interval. I want a unique prediction for variable w. Here is the code with sample data:

x<-c(.34,.355,.37,.385,.34,.355,.37,.385,.34,.355,.37,.385,.34,.355,.37,.385)
y<-c(40,35,28,25,42,36,29,25,44,37,26,23,46,37,33,27)
w<-c(1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4)
data<-data.frame(x,y,w)
ggplot(data=data,aes(x=x,y=log10(y),color=factor(w)))+
   geom_point(size=4)+
   geom_smooth(method=lm,aes(group=w,fill=factor(w)),fullrange=TRUE)+
   facet_wrap(~w,nrow=2)+
   scale_x_continuous(limit=c(.33,.5))
mod <- dlply(data, .(w), function(df) lm(log(y)~x, data = df))

I am trying to use the function predict but it does not work on list. So I can predict on an element of list

newdata<-data.frame(x=.5)    
predict(mod[[1]],newdata,interval="confidence")

which gives the following output

     fit       lwr      upr
1 0.8476424 0.5981407 1.097144

However, I want to be able to apply predict on each element of the list. I tried to use library nlme to do the following but it does not give the confidence interval.

library(nlme)

ll=lmList(log10(y)~x|w,data = data)
predict(ll,newdata,interval="confidence")

output:

1         2         3         4 
0.8476424 0.8042928 0.5818862 0.8633285 

I used ggplot2 just for visual aid but I need the actual values at the boundary of the confidence interval to compute the range of predicted y variable.

Upvotes: 1

Views: 1213

Answers (1)

jeremycg
jeremycg

Reputation: 24945

If you are wanting to apply on each element of a list, lapply (list apply) is the way to go:

do.call(rbind, lapply(mod, function(x) predict(x, newdata, interval="confidence")))

       fit       lwr      upr
1 1.951769 1.3772699 2.526268
1 1.851953 1.4852869 2.218618
1 1.339843 0.1453728 2.534312
1 1.987887 1.4446006 2.531174

so using lapply, we are running our anonymous function predict(x, newdata, interval="confidence")) with x being each element of mod. The do.call turns the list output into a nicer matrix.

Upvotes: 1

Related Questions