Re: [R] fitted values from lmer (lme4 0.98)

From: Douglas Bates <dmbates_at_gmail.com>
Date: Wed 18 Jan 2006 - 08:31:40 EST

On 17 Jan 2006 22:00:38 +0100, Peter Dalgaard <p.dalgaard@biostat.ku.dk> wrote:
> Douglas Bates <dmbates@gmail.com> writes:
>
> > On 1/16/06, Daniel A. Powers <dpowers@mail.la.utexas.edu> wrote:
> > >
> > > -- R-List
> > >
> > > Can someone tell me how to get fitted values etc. after fitting lmer?
> > > for example, from lme, I can fit mod.1 <- lme(....) and get fitted values, coefficients, etc. in this way
> > >
> > > mod.1$fitted[,1] or mod.1$fitted[,2] etc.
> > >
> > > It seems lmer uses "slots" that are unfamiliar to me.
> >
> > The preferred way is to use the extractor functions fitted, fixef,
> > ranef and coef.
> >
> > Using the recently uploaded version 0.995-1 of the Matrix package we get
>
> But, at least with 0.99-6 (sorry for falling way behind, but I'd
> rather not upgrade just now...), fitted(fm1) are effectively BLUPs. If
> you want to get the estimated mean values, you need something like
>
> model.matrix(fm1@terms,sleepstudy) %*% fixef(fm1)
>
> I realize that since lmer models are not necessarily hierarchical, it
> takes more than cloning the "level" argument from fitted.lme, but it
> could be useful to at least have a "level=0" equivalent.

Would it help if there were an option in the fitted method to allow for fixed-effects only versus fixed- and random-effects? As you say, because lmer models do not need to be hierarchical it is not obvious what it would mean to include some but not all of the random effects terms in the "fitted values". However, it is easy and unambiguous to define fitted values for the fixed-effects only.

Up until a few days ago there was an option to do this but then I changed the calculation of the fitted values in an attempt to clean up the code. The calculation of the level = 0 fitted values in the new representation of the fitted model is quite easy. It is

fm1@X %*% fixef(fm1)

(except for complications introduced by na.exclude)

>
> > > (fm1 <- lmer(Reaction ~ Days + (Days|Subject), sleepstudy))
> > Linear mixed-effects model fit by REML
> > Formula: Reaction ~ Days + (Days | Subject)
> > Data: sleepstudy
> > AIC BIC logLik MLdeviance REMLdeviance
> > 1753.628 1769.593 -871.8141 1751.986 1743.628
> > Random effects:
> > Groups Name Variance Std.Dev. Corr
> > Subject (Intercept) 612.090 24.7405
> > Days 35.072 5.9221 0.066
> > Residual 654.941 25.5918
> > # of obs: 180, groups: Subject, 18
> >
> > Fixed effects:
> > Estimate Std. Error t value
> > (Intercept) 251.4051 6.8246 36.838
> > Days 10.4673 1.5458 6.771
> >
> > Correlation of Fixed Effects:
> > (Intr)
> > Days -0.138
> > > fixef(fm1)
> > (Intercept) Days
> > 251.40510 10.46729
> > > ranef(fm1)
> > An object of class $-1˛Řlmer.ranef˛ř
> > [[1]]
> > (Intercept) Days
> > 308 2.2585636 9.1989720
> > 309 -40.3985870 -8.6197013
> > 310 -38.9602563 -5.4488780
> > 330 23.6905071 -4.8143326
> > 331 22.2602104 -3.0698958
> > 332 9.0395288 -0.2721711
> > 333 16.8404364 -0.2236253
> > 334 -7.2325817 1.0745765
> > 335 -0.3336930 -10.7521594
> > 337 34.8903592 8.6282824
> > 349 -25.2101185 1.1734156
> > 350 -13.0699625 6.6142058
> > 351 4.5778374 -3.0152575
> > 352 20.8635979 3.5360123
> > 369 3.2754538 0.8722165
> > 370 -25.6128786 4.8224661
> > 371 0.8070403 -0.9881552
> > 372 12.3145428 1.2840291
> >
> > > coef(fm1)
> > $Subject
> > (Intercept) Days
> > 308 253.6637 19.6662580
> > 309 211.0065 1.8475846
> > 310 212.4448 5.0184079
> > 330 275.0956 5.6529533
> > 331 273.6653 7.3973901
> > 332 260.4446 10.1951148
> > 333 268.2455 10.2436606
> > 334 244.1725 11.5418624
> > 335 251.0714 -0.2848734
> > 337 286.2955 19.0955683
> > 349 226.1950 11.6407015
> > 350 238.3351 17.0814918
> > 351 255.9829 7.4520285
> > 352 272.2687 14.0032983
> > 369 254.6806 11.3395024
> > 370 225.7922 15.2897520
> > 371 252.2121 9.4791308
> > 372 263.7196 11.7513151
> >
> > > fitted(fm1)
> > [1] 253.6637 273.3299 292.9962 312.6624 332.3287 351.9950 371.6612 391.3275
> > [9] 410.9937 430.6600 211.0065 212.8541 214.7017 216.5493 218.3969 220.2444
> > [17] 222.0920 223.9396 225.7872 227.6348 212.4448 217.4633 222.4817 227.5001
> > [25] 232.5185 237.5369 242.5553 247.5737 252.5921 257.6105 275.0956 280.7486
> > [33] 286.4015 292.0545 297.7074 303.3604 309.0133 314.6663 320.3192 325.9722
> > [41] 273.6653 281.0627 288.4601 295.8575 303.2549 310.6523 318.0497 325.4470
> > [49] 332.8444 340.2418 260.4446 270.6397 280.8349 291.0300 301.2251 311.4202
> > [57] 321.6153 331.8104 342.0056 352.2007 268.2455 278.4892 288.7329 298.9765
> > [65] 309.2202 319.4638 329.7075 339.9512 350.1948 360.4385 244.1725 255.7144
> > [73] 267.2562 278.7981 290.3400 301.8818 313.4237 324.9656 336.5074 348.0493
> > [81] 251.0714 250.7865 250.5017 250.2168 249.9319 249.6470 249.3622 249.0773
> > [89] 248.7924 248.5076 286.2955 305.3910 324.4866 343.5822 362.6777 381.7733
> > [97] 400.8689 419.9644 439.0600 458.1556 226.1950 237.8357 249.4764 261.1171
> > [105] 272.7578 284.3985 296.0392 307.6799 319.3206 330.9613 238.3351 255.4166
> > [113] 272.4981 289.5796 306.6611 323.7426 340.8241 357.9056 374.9871 392.0686
> > [121] 255.9829 263.4350 270.8870 278.3390 285.7911 293.2431 300.6951 308.1471
> > [129] 315.5992 323.0512 272.2687 286.2720 300.2753 314.2786 328.2819 342.2852
> > [137] 356.2885 370.2918 384.2951 398.2984 254.6806 266.0201 277.3596 288.6991
> > [145] 300.0386 311.3781 322.7176 334.0571 345.3966 356.7361 225.7922 241.0820
> > [153] 256.3717 271.6615 286.9512 302.2410 317.5307 332.8205 348.1102 363.4000
> > [161] 252.2121 261.6913 271.1704 280.6495 290.1287 299.6078 309.0869 318.5661
> > [169] 328.0452 337.5243 263.7196 275.4710 287.2223 298.9736 310.7249 322.4762
> > [177] 334.2275 345.9789 357.7302 369.4815
> >
> > I hope this helps.
> >
> > Doug Bates
> >
> > ______________________________________________
> > R-help@stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
> >
>
> --
> O__ ---- Peter Dalgaard ěster Farimagsgade 5, Entr.B
> c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K
> (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918
> ~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk) FAX: (+45) 35327907
>



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Wed Jan 18 08:40:13 2006

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:42:05 EST