Transcription of Using lme4: Mixed-Effects Modeling in R
1 SimpleLongitudinalInteractionsTheoryGLMM Item ResponseNLMMU sing lme4 : Mixed-Effects Modeling in RDouglas BatesUniversity of Wisconsin - Madisonand R Development Core 11, 2008 SimpleLongitudinalInteractionsTheoryGLMM Item ResponseNLMMO utlineOrganizing and plotting data; simple, scalar random effectsModels for longitudinal dataInteractions of grouping factors and other covariatesEvaluating the log-likelihoodGeneralized linear mixed ModelsItem Response Models as GLMMsNonlinear mixed ModelsSimpleLongitudinalInteractionsTheo ryGLMMItem ResponseNLMMO utlineOrganizing and plotting data; simple, scalar random effectsModels for longitudinal dataInteractions of grouping factors and other covariatesEvaluating the log-likelihoodGeneralized linear mixed ModelsItem Response Models as GLMMsNonlinear mixed ModelsSimpleLongitudinalInteractionsTheo ryGLMMItem ResponseNLMMO utlineOrganizing and plotting data; simple, scalar random effectsModels for longitudinal dataInteractions of grouping factors and other covariatesEvaluating the log-likelihoodGeneralized linear mixed ModelsItem Response Models as GLMMsNonlinear mixed ModelsSimpleLongitudinalInteractionsTheo ryGLMMItem ResponseNLMMO utlineOrganizing and plotting data.
2 Simple, scalar random effectsModels for longitudinal dataInteractions of grouping factors and other covariatesEvaluating the log-likelihoodGeneralized linear mixed ModelsItem Response Models as GLMMsNonlinear mixed ModelsSimpleLongitudinalInteractionsTheo ryGLMMItem ResponseNLMMO utlineOrganizing and plotting data; simple, scalar random effectsModels for longitudinal dataInteractions of grouping factors and other covariatesEvaluating the log-likelihoodGeneralized linear mixed ModelsItem Response Models as GLMMsNonlinear mixed ModelsSimpleLongitudinalInteractionsTheo ryGLMMItem ResponseNLMMO utlineOrganizing and plotting data; simple, scalar random effectsModels for longitudinal dataInteractions of grouping factors and other covariatesEvaluating the log-likelihoodGeneralized linear mixed ModelsItem Response Models as GLMMsNonlinear mixed ModelsSimpleLongitudinalInteractionsTheo ryGLMMItem ResponseNLMMO utlineOrganizing and plotting data; simple, scalar random effectsModels for longitudinal dataInteractions of grouping factors and other covariatesEvaluating the log-likelihoodGeneralized linear mixed ModelsItem Response Models as GLMMsNonlinear mixed ModelsSimpleLongitudinalInteractionsTheo ryGLMMItem ResponseNLMMWeb sites associated with the bates/UseR2008 Materials for the Main web site for the R Comprehensive R Archive Network primary Main mirror for R-Forge, development site for many public Rpackages.
3 This is also the URL of the repository forinstalling the development versions of thelme4andMatrixpackages, if you are so development site for thelme4packageSimpleLongitudinalInteract ionsTheoryGLMMItem ResponseNLMMO utlineOrganizing and plotting data; simple, scalar random effectsModels for longitudinal dataInteractions of grouping factors and other covariatesEvaluating the log-likelihoodGeneralized linear mixed ModelsItem Response Models as GLMMsNonlinear mixed ModelsSimpleLongitudinalInteractionsTheo ryGLMMItem ResponseNLMMO rganizing data in R Standard rectangular data sets (columns are variables, rowsare observations) are stored inRasdata frames. The columns can benumericvariables ( measurements orcounts) orfactorvariables (categorical data) ororderedfactorvariables. These types are called theclassof the variable. Thestrfunction provides a concise description of thestructure of a data set (or any other class of object in R).
4 Thesummaryfunction summarizes each variable according to itsclass. Both are highly recommended for routine use. Entering just the name of the data frame causes it to beprinted. For large data frames use theheadandtailfunctions to view the first few or last few ResponseNLMMR packages Packages incorporate functions, data and documentation. You can produce packages for private or in-house use or youcan contribute your package to the Comprehensive R ArchiveNetwork (CRAN), We will be Using thelme4package from CRAN. Install it fromthePackagesmenu item or with> (" lme4 ") You only need to install a package once. If a new versionbecomes available you can update (see the menu item). To use a package in an R session you attach it Using > require( lme4 )or> library( lme4 )(This usage causes widespread confusion of the terms package and library .)SimpleLongitudinalInteractionsTheoryGL MMItem ResponseNLMMA ccessing documentation To be added to CRAN, a package must pass a series of qualitycontrol checks.
5 In particular, all functions and data sets mustbe documented. Examples and tests can also be included. Thedatafunction provides names and brief descriptions ofthe data sets in a package.> data(package = " lme4 ")Data sets in package lme4 :Dyestuff Yield of dyestuff by batchDyestuff2 Yield of dyestuff by batchPastes Paste strength by batch and caskPenicillin Variation in penicillin testingcake Breakage angle of chocolate cakescbpp Contagious bovine pleuropneumoniasleepstudy Reaction times in a sleep deprivation study Use?followed by the name of a function or data set to viewits documentation. If the documentation contains an examplesection, you can execute it with ResponseNLMML attice graphics One of the strengths of R is its graphics capabilities.
6 There are several styles of graphics in R. The style inDeepayan Sarkar slatticepackage is well-suited to the type ofdata we will be discussing. I will not show every piece of code used to produce the datagraphics. The code is available in the script files for the slides(and sometimes in the example sections of the data set sdocumentation). Deepayan s book,Lattice: Multivariate Data Visualizationwith R(Springer, 2008) provides in-depth documentation andexplanations of lattice graphics. I also recommend Phil Spector s book,Data Manipulationwith R(Springer, 2008).SimpleLongitudinalInteractionsTheo ryGLMMItem ResponseNLMMThe Dyestuff data set TheDyestuff,PenicillinandPastesdata sets all comefrom the classic bookStatistical Methods in Research andProduction, edited by Davies and first published in 1947. TheDyestuffdata are a balanced one-way classification oftheYieldof dyestuff from samples produced from sixBatchesof an intermediate product.
7 See?Dyestuff.> str(Dyestuff) : 30 obs. of 2 variables:$ Batch: Factor w/ 6 levels "A","B","C","D",..: 1 1 1 1 1 2 2 2 2 2 ..$ Yield: num 1545 1440 1440 1520 1580 ..> summary(Dyestuff)Batch YieldA:5 Min. :1440B:5 1st Qu.:1469C:5 Median :1530D:5 Mean :1528E:5 3rd Qu.:1575F:5 Max. :1635 SimpleLongitudinalInteractionsTheoryGLMM Item ResponseNLMMThe effect of the batches To emphasize thatBatchis categorical, we use letters insteadof numbers to designate the levels. Because there is no inherent ordering of the levels ofBatch,we will reorder the levels if, say, doing so can make a plotmore informative. The particular batches observed are just a selection of thepossible batches and are entirely used up during the course ofthe experiment. It is not particularly important to estimate and compare yieldsfrom these batches. Instead we wish to estimate thevariability in yields due to batch-to-batch variability.
8 TheBatchfactor will be used inrandom-effectsterms inmodels that we ResponseNLMMD yestuff data plotYield of dyestuff (grams of standard color)BatchFDABCE1450150015501600lllllll lllllllllllllllllllllll The line joins the mean yields of the six batches, which havebeen reordered by increasing mean yield. The vertical positions are jittered slightly to reduceoverplotting. The lowest yield for batch A was observed ontwo distinct preparations from that ResponseNLMMA Mixed-Effects model for the dyestuff yield> fm1 <- lmer(Yield ~ 1 + (1 | Batch), Dyestuff)> print(fm1) linear mixed model fit by REMLF ormula: Yield ~ 1 + (1 | Batch)Data: DyestuffAIC BIC logLik deviance effects:Groups Name Variance (Intercept) of obs: 30, groups: Batch, 6 Fixed effects:Estimate Std. Error t value(Intercept) Fitted modelfm1has one fixed- effect parameter, the meanyield, and one random -effects term, generating a simple,scalar random effect for each level ResponseNLMME xtracting information from the fitted model fm1is an object of class"mer"( Mixed-Effects representation).
9 There are manyextractorfunctions that can be applied tosuch objects.> fixef(fm1)(Intercept) > ranef(fm1, drop = TRUE)$BatchA B C D E > fitted(fm1)[1] [9] [17] [25] ResponseNLMMD efinition of linear Mixed-Effects models A Mixed-Effects model incorporates two vector-valued randomvariables: the response,Y, and the random effects,B. Weobserve the value,y, ofY. We do not observe the value ofB. In alinear Mixed-Effects modelthe conditional distribution,Y|B, and the marginal distribution,B, are independent,multivariate normal (or Gaussian ) distributions,(Y|B=b) N(X +Zb, 2I),B N(0, 2 ),(Y|B) B. The scalar is thecommon scale parameter; thep-dimensional is thefixed-effects parameter; then pXand then qZare known, fixedmodel matrices; and theq qrelative variance-covariance matrix ( )is a positivesemidefinite, symmetricq qmatrix that depends on theparameter.
10 SimpleLongitudinalInteractionsTheoryGLMM Item ResponseNLMMC onditional modes of the random effects Technically we do not provide estimates of the randomeffects because they are not parameters. One answer to the question, so what are those numbersanyway? is that they are BLUPs (Best linear UnbiasedPredictors) but that answer is not informative and the conceptdoes not generalize. A better answer is that those values are the conditionalmeans, E[B|Y=y], evaluated at the estimated , we can only evaluate the conditional means forlinear mixed models. However, these values are also the conditional modes and thatconcept does generalize to other types of mixed ResponseNLMMC aterpillar plot for fm1 For linear mixed models we can evaluate the means andstandard deviations of the conditional distributionsBj|Y,j= 1,..,q. We show these in the form of a 95%prediction interval, with the levels of the grouping factorarranged in increasing order of the conditional mean.