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.