Darren Norris
2008-Feb-15 00:55 UTC
[R] How to plot fitted values from lmer (lme4 package)?
I am modelling (at least trying to) the seasonal component of a variable using lmer. I think I am just about getting the hang of building the models but want to see what the fitted values look like. I need to plot 2 lines on the same graph - the original data ( copy of dataframe below) and the fitted values. I am doing this to a) start to understand how to use R and b) start to understand how to build and compare models. I have been using books (e.g. The R Book Michael Crawley) and pdf (e.g. time series analysis with R part 1 ? Walter Zuccchini and Oleg Nenadic) for reference and if the answer is there (which it probably is) I can?t understand it (I have a very slow dial up connection so even searching nabble frequently crashes)! An example of the model is: Scale the annual cycle to be length 1.0:- with(a_dataframe,length(mean_ind)) [1] 15 index<-1:15 15/4 [1] 3.75 time<-index/3.75 Model (index used as explanatory variable for trend, also allows different intercepts for years as a random effect ? I hope):- mtrend<-lmer(a_dataframe$mean_ind~index+sin(time*2*pi)+cos(time*2*pi)+(1|factor(a_dataframe$YEAR)),method="ML") using ?lm? I followed the following process to produce the graph I wanted. lmodel<-with(a_dataframe,lm(mean_ind~sin(time*2*pi)+cos(time*2*pi))) plot(time,a_dataframe$mean_ind,pch=".") lines(time,predict(lmodel)) How can I do the same with lmer? I have tried a variety of things with ?predict?, trying to put the results from lmer as a ?groupedData? object etc. But I can?t seem to make it work. Any simple solutions? Many thanks for any help. Sample data a_dataframe below (I removed NULL rows so row references are not concsequtive):- YEAR_QUARTER YEAR a_mean 1 2004_2 2004 71.46154 2 2004_3 2004 56.30000 3 2004_4 2004 37.88889 19 2005_1 2005 37.00000 20 2005_2 2005 88.44444 21 2005_3 2005 87.28571 22 2005_4 2005 43.75000 38 2006_1 2006 50.85714 39 2006_2 2006 142.25000 40 2006_3 2006 137.57143 41 2006_4 2006 79.86667 57 2007_1 2007 73.13333 58 2007_2 2007 146.28571 59 2007_3 2007 118.07692 60 2007_4 2007 73.50000 -- View this message in context: http://www.nabble.com/How-to-plot-fitted-values-from-lmer-%28lme4-package%29--tp15492920p15492920.html Sent from the R help mailing list archive at Nabble.com.
The trick is to use the fitted() function, not predict(), to get your fitted values. You should then be able to use that vector of values in just the same way that you use your current mean values as below. Darren Norris wrote:> > > lmodel<-with(a_dataframe,lm(mean_ind~sin(time*2*pi)+cos(time*2*pi))) > plot(time,a_dataframe$mean_ind,pch=".") > lines(time,predict(lmodel)) > > How can I do the same with lmer? I have tried a variety of things with > ?predict?, trying to put the results from lmer as a ?groupedData? object > etc. But I can?t seem to make it work. Any simple solutions? > Many thanks for any help. > > >-- View this message in context: http://www.nabble.com/How-to-plot-fitted-values-from-lmer-%28lme4-package%29--tp15492920p15495816.html Sent from the R help mailing list archive at Nabble.com.