Dear all, I have three concerns: 1) I am running models with the lme4 package. I cannot find a way to pull out a vector of the fitted values and the residuals. Does anybody know how to do it? 2) How can I nest a random effect variable into a "two-level" fixed effect variable? 3) Suppose I have the following model: y = a + b|c + d + error, where 'a' is a fixed effect, 'c' is a random effect nested with the random effect 'b', and 'd' is a non-nested random effect. Suppose I obtain parameter estimates for all of the predictor variables. Now, I would like to calculate a fitted value, 'yhat', from only the parameter estimates for 'a' and 'b|c'. Can this be done in lme4. That is; can I pull out all of the requisite elements from the output to calculate 'yhat'? Thanks for your help, Frank. --
On 11/16/06, Frank Johannes <fjohannes at fastmail.fm> wrote:> Dear all, > I have three concerns: > 1) > I am running models with the lme4 package. I cannot find a way to pull > out a vector of the fitted values and the residuals. Does anybody know > how to do it?The fitted() and resid() extractor functions are the usual way of doing this for a fitted model in R and, astonishingly enough, they work! Try library(lme4) example(lmer) fitted(fm1) resid(fm1)> 2) > How can I nest a random effect variable into a "two-level" fixed effect > variable?I'm not quite sure what you mean. Are you talking about a situation like having a random effect for "Patient" and a fixed effect for "Treatment" with Patient nested within Treatment. If the Patients are designated in a sensible way, so that each Patient has a distinct identifier (i.e. the number of levels of the Patient factor corresponds to the number of Patients) then you can ignore the nesting and specify the model as lmer(response ~ Treatment + (1|Patient), ...) Sometimes the Patients are designated with implicit nesting so that the treatment group has a patient labeled "1" and the control group has another patient also labeled as "1". In that case you need to specify the grouping factor for the random effects as the Treatment:Patient interaction. That is, lmer(response ~ Treatment + (1|Treatment:Patient), ...) The second version also works properly if the Patients had been designated in a sensible way so, if you want to be safe, use the second version.> 3) > Suppose I have the following model: > y = a + b|c + d + error, > where 'a' is a fixed effect, 'c' is a random effect nested with the > random effect 'b', and 'd' is a non-nested random effect.That's not the greatest notation to use for the R-Help list because it doesn't correspond to the way that R formulas are parsed.> Suppose I obtain parameter estimates for all of the predictor variables. > Now, I would like to calculate a fitted value, 'yhat', from only the > parameter estimates for 'a' and 'b|c'. Can this be done in lme4. That > is; can I pull out all of the requisite elements from the output to > calculate 'yhat'?The answer to the question of "Can this be done in lme4?" is install.packages("fortunes"); library(fortunes); fortune("Yoda") I could describe how to do it if I had a better idea of what the calculation you had in mind was. I am having difficulty grasping how you obtain a prediction from a model involving a factor 'd' without assigning a value to that factor. I suppose that someone who understands the deep magic of quantities like SAS's LS-means may be able to tell you what you want to know but I can't. I can say that you can obtain the coefficient estimates for the fixed effects with fixef(fm1) and a list of the conditional modes of the random effects (also called the BLUPs) as ranef(fm1) After that you are on your own.
Hello, Indeed, implementing the fitted and resid calls on the fm1 object from example(lmer), the fitted function worked, but the resid did not:> resid(fm1)Error in resid(fm1) : no slot of name "family" for this object of class "lmer" I am using R 2.4.0 (on Windows XP), with the update of lme4 (lmer within) written for 2.4.0. Thanks, Joel.> Message: 79 Date: Fri, 17 Nov 2006 03:14:24 +0100 From: "Renaud > Lancelot" <renaud.lancelot at gmail.com> Subject: Re: [R] lme4 package: > Fitted values and residuals To: "Douglas Bates" <bates at stat.wisc.edu> > Cc: r-help at stat.math.ethz.ch, Frank Johannes <fjohannes at fastmail.fm> > Message-ID: > <c2ee56800611161814w419d7a59u13fbdfe0c02fa784 at mail.gmail.com> > Content-Type: text/plain; charset=ISO-8859-1; format=flowed > 2006/11/16, Douglas Bates <bates at stat.wisc.edu>: >> > On 11/16/06, Frank Johannes <fjohannes at fastmail.fm> wrote: >>> > > Dear all, >>> > > I have three concerns: >>> > > 1) >>> > > I am running models with the lme4 package. I cannot find a way to pull >>> > > out a vector of the fitted values and the residuals. Does anybody know >>> > > how to do it? >> > >> > The fitted() and resid() extractor functions are the usual way of >> > doing this for a fitted model in R and, astonishingly enough, they >> > work! Try >> > >> > library(lme4) >> > example(lmer) >> > fitted(fm1) >> > resid(fm1) > > But resid does not seem to work with GLMMs: > >> > library(lme4) ## version 0.9975-9 > Le chargement a n?cessit? le package : Matrix > Le chargement a n?cessit? le package : lattice >> > m1 <- lmer(cbind(incidence, size - incidence) ~ period + (1 | herd), > + family = binomial, data = cbpp) > relative tolerance set to 9.8721686386158e-05 >> > resid(m1) > Erreur : 'resid' n'est pas encore impl?ment? > > [in English: Error: 'resid' is not implemented yet] > > Best, > > Renaud >
>>>>> "Joel" == Joel Dubin <jdubin at uwaterloo.ca> >>>>> on Fri, 17 Nov 2006 16:19:16 -0500 writes:Joel> Hello, Indeed, implementing the fitted and resid calls Joel> on the fm1 object from example(lmer), the fitted Joel> function worked, but the resid did not: >> resid(fm1) Joel> Error in resid(fm1) : no slot of name "family" for Joel> this object of class "lmer" Joel> I am using R 2.4.0 (on Windows XP), with the update of Joel> lme4 (lmer within) written for 2.4.0. I think you should update lme4 [ update.packages(.....) ], since the above looks suspiciously like a bug that had been fixed not so long ago. IIRC, the posting guide asks you to provide sessionInfo() in such situations. Regards, Martin
Thank you, Martin, for pointing this out (not to mention the missing sessionInfo() piece). Indeed the resid() call works as expected with the most recent update of lme4. Cheers, Joel. Martin Maechler wrote:>>>>>> "Joel" == Joel Dubin <jdubin at uwaterloo.ca> >>>>>> on Fri, 17 Nov 2006 16:19:16 -0500 writes: >>>>>> > > Joel> Hello, Indeed, implementing the fitted and resid calls > Joel> on the fm1 object from example(lmer), the fitted > Joel> function worked, but the resid did not: > > >> resid(fm1) > Joel> Error in resid(fm1) : no slot of name "family" for > Joel> this object of class "lmer" > > Joel> I am using R 2.4.0 (on Windows XP), with the update of > Joel> lme4 (lmer within) written for 2.4.0. > > I think you should update lme4 [ update.packages(.....) ], > since the above looks suspiciously like a bug that had been > fixed not so long ago. > > IIRC, the posting guide asks you to provide sessionInfo() > in such situations. > > Regards, > Martin >