Fitting mixed models in R

Size: px
Start display at page:

Download "Fitting mixed models in R"

Transcription

1 Fitting mixed models in R Contents 1 Packages 2 2 Specifying the variance-covariance matrix (nlme package) Illustration: Technical point: why to use as.numeric(time) in corsymm Handling missing data (nlme package) Illustration: Methods for GLS objects Test linear hypothesis Analysis of variance recall in R Getting confidence intervals for the coefficients Displaying the variance covariance matrix Predictions Punctual estimate Confidence intervals References 17 1

2 1 Packages Several packages can be used in R to fit mixed models. In this course we will use the two following packages: nlme package: it enables to specify the form of the correlation structure between residuals, to model a potential heteroscedasticity and to consider random effects. It is limited to gaussian variables but can handle non-linear relationships (e.g. Y exp(βx)). Main fonctions: gls lme nlme Reference book: (Pinheiro and Bates 2000) lme4 package: it is a numerically more efficient alternative to nlme which is recommanded for large datasets or when several random effects are considered. Contrary to nlme, the correlation structure between residuals can only be model through random effects. No option for dealing with heteroscedasticity. However lme4 enables to model non-gaussian dependant variables (e.g. binary, Poisson,... ). Main functions: lmer glmer nlmer Reference book: (D. M. Bates 2010) See or for a broader overview. The first link also includes interesting discussion on the practical use of mixed models. 2

3 2 Specifying the variance-covariance matrix (nlme package) gls, lme and nlme use two arguments to construct the variance covariance matrix that will be used to fit the mixed model: argument correlation: specifies the form of the covariance matrix (e.g. correlation = corcompsymm(form = ~1 animal)). It is composed of three parts: the structure of the correlation matrix (e.g. corcompsymm). See?corClasses for a list of the available structures. the postion variable form = ~1: usually only an intercept but one may specify explanatory variables here if, for instance, the correlation between observation times is assumed be different regarding the age or the geographical position. the grouping variable animal. Here we indicates that observations are correlated within the same animal. argument weight: can be used to model a potential heteroscedasticity, e.g. variance dependant on some variables. As the correlation argument, it is composed of three parts: the structure of the heteroschedasticity (e.g. varident). See?varClasses for a list of the available structures. the postion variable form = ~1: usually only an intercept but one may specify explanatory variables here if, for instance, the variance is assumed to depend of a given variable. the grouping variable time. Here we indicates that observations observed at the same time have common variance. 2.1 Illustration: library(nlme) setkey(dtl.data, Id) # sort data by Id print(dtl.data[1:11]) # only 3 observations for patient 1 Id Gender Treatment Age time outcome 1: 1 Male Yes time : 1 Male Yes time : 1 Male Yes time : 2 Male Yes time

4 5: 2 Male Yes time : 2 Male Yes time : 2 Male Yes time : 3 Female Yes time : 3 Female Yes time : 3 Female Yes time : 3 Female Yes time # compound symmetry fm1 <- gls(outcome ~ Gender + Age + Treatment, correlation = corcompsymm(form = ~1 Id), data = dtl.data) # unstructured with constant variance fm2 <- gls(outcome ~ Gender + Age + Treatment, correlation = corsymm(form = ~as.numeric(time) Id), data = dtl.data) # unstructured with different variances fm3 <- gls(outcome ~ Gender + Age + Treatment, correlation = corsymm(form = ~as.numeric(time) Id), weights = varident(form =~ 1 time), data = dtl.data) 2.2 Technical point: why to use as.numeric(time) in corsymm Using ~as.numeric(time) Id in corsymm may seems a weird syntax. as.numeric(time) is here to indicate the time repetition. In absence of missing values, one can also use ~1 Id. However when dealing with missing values, this last syntax can lead to unexpected behavior (Galecki and Burzykowski 2013): # adapted from example R11.5 page 207, chapter 11.4 Sigma <- corar1(value = 0.3, form = ~1 Id) Isigma <- Initialize(Sigma, dtl.data) cormatrix(isigma)[[2]] [,1] [,2] [,3] [,4] [1,] [2,] [3,] [4,]

5 dtl.datana <- dtl.data[-5,] dtl.datana[dtl.datana$id==2,] # time 2 is missing Id Gender Treatment Age time outcome 1: 2 Male Yes time : 2 Male Yes time : 2 Male Yes time IsigmaNA <- Initialize(Sigma, dtl.datana) cormatrix(isigmana)[[2]] # not ok [,1] [,2] [,3] [1,] [2,] [3,] valid syntax Sigma <- corar1(value = 0.3, form = ~as.numeric(time) Id) IsigmaNA <- Initialize(Sigma, dtl.datana) cormatrix(isigmana)[[2]] # ok [,1] [,2] [,3] [1,] [2,] [3,]

6 3 Handling missing data (nlme package) gls, lme and nlme read the argument na.action to know how to handle missing values: na.fail (default option) leads to an error in presence of missing values. na.omit deals with missing values by removing the corresponding lines in the dataset. na.exclude ignores the missing values but, compared to na.omit, it enables to have outputs with the same number of observations compared to the original dataset. na.pass will continue the execution of the function without any change. If the function cannot manage missing values, it will lead to an error. 3.1 Illustration: library(nlme) data(orthodont, package = "nlme") dataset.na <- rbind(na, Orthodont) NROW(dataset.NA) # 109 observations [1] 109 na.fail attributes(try( lme.fail <- lme(distance ~ age, data = dataset.na),silent = TRUE ))$condition <simpleerror in na.fail.default(structure(list(age = c(na, 8, 10, 12, 14, 8, 10, 12, # same as: fm1 <- lme(distance ~ age, data = dataset.na, na.action = na.fail) na.omit lme.omit <- lme(distance ~ age, data = rbind(na, Orthodont), na.action = na.omit) NROW(predict(lme.omit)) # 108 fitted values [1] 108 na.exclude lme.exclude <- lme(distance ~ age, data = rbind(na, Orthodont), na.action = na.exclude) NROW(predict(lme.exclude)) # 109 fitted values (1 NA others) 6

7 [1] 109 predict(lme.exclude)[1] <NA> NA na.pass attributes(try( lme.pass <- lme(distance ~ age, data = rbind(na,orthodont), na.action = na.pass),silent = TRUE ))$condition <simpleerror in if (max(tmpdims$zxlen[[1l]]) < tmpdims$qvec[1l]) { warning(gettext 7

8 4 Methods for GLS objects methods(class=class(fm1)) # find available methods for gls objects [1] ACF anova augpred coef [5] comparepred fitted formula getdata [9] getgroups getgroupsformula getresponse getvarcov [13] intervals loglik nobs plot [17] predict print qqnorm residuals [21] summary update Variogram vcov see '?methods' for accessing help and source code 4.1 Test linear hypothesis # Wald tests summary(fm1)$ttable Value Std.Error t-value p-value (Intercept) e-11 GenderMale e-02 Age e-01 TreatmentYes e-03 # to test a linear combinaison of coefficients library(multcomp, verbose = FALSE, quietly = TRUE) Attaching package: 'TH.data' The following object is masked from 'package:mass': geyser Allcoef <- coef(fm1) # create a contrast matrix C <- matrix(0,nrow = 1, ncol=length(allcoef), dimnames = list(null, names(allcoef))) C[1,"GenderMale"] <- 1 C[1,"TreatmentYes"] <- 1 summary(glht(fm1, linfct = C)) 8

9 Simultaneous Tests for General Linear Hypotheses Fit: gls(model = outcome ~ Gender + Age + Treatment, data = dtl.data, correlation = corcompsymm(form = ~1 Id)) Linear Hypotheses: Estimate Std. Error z value Pr(> z ) 1 == (Adjusted p values reported -- single-step method) # combined tests C <- matrix(0,nrow = 2, ncol=length(allcoef), dimnames = list(null, names(allcoef))) C[1,"GenderMale"] <- 1 C[2,"TreatmentYes"] <- 1 anova(fm1, L = C) # no punctual estimate Denom. DF: 75 F-test for linear combination(s) GenderMale TreatmentYes numdf F-value p-value summary(glht(fm1, linfct = C)) # performs separate tests Simultaneous Tests for General Linear Hypotheses Fit: gls(model = outcome ~ Gender + Age + Treatment, data = dtl.data, correlation = corcompsymm(form = ~1 Id)) Linear Hypotheses: Estimate Std. Error z value Pr(> z ) 1 == == * --- Signif. codes: 0 '***' '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 (Adjusted p values reported -- single-step method) 9

10 4.2 Analysis of variance recall Denote SS(A) the explained sum of squares by the variable A. Sequential anova = Type I Anova: SS(A) for factor A. SS(B A) = SS(A, B) - SS(A) for factor B. SS(AB B, A) = SS(A, B, AB) - SS(A, B) for interaction AB. It will give different results depending on which main effect is considered first. It tests the first factor without controlling for the other factor(s). Marginal anova = Type II/III Anova (depending if we consider an interaction or not): SS(A B) for factor A if no interaction else SS(A B, AB) SS(B A) for factor B if no interaction else SS(B A, AB) This type tests for each main effect after controlling for the other effects. (from in R fmi <- gls(outcome ~ Gender + Age * Treatment, correlation = corcompsymm(form = ~1 Id), data = dtl.data) By default sequential ANOVA anova(fmi) # equivalent to anova(m.gls, type = "sequential") Denom. DF: 74 numdf F-value p-value (Intercept) <.0001 Gender Age Treatment Age:Treatment

11 marginal ANOVA anova(fmi, type = "marginal") Denom. DF: 74 numdf F-value p-value (Intercept) <.0001 Gender Age Treatment Age:Treatment equivalent to separate F tests n.coef <- length(coef(fmi)) Contrast <- matrix(0, nrow = 1, ncol = n.coef) colnames(contrast) <- names(coef(fmi)) Contrast1 <- Contrast2 <- Contrast3 <- Contrast Contrast1[,"GenderMale"] <- 1 anova(fmi, L = Contrast1) Denom. DF: 74 F-test for linear combination(s) [1] 1 numdf F-value p-value Contrast2[,"Age"] <- 1 anova(fmi, L = Contrast2) Denom. DF: 74 F-test for linear combination(s) [1] 1 numdf F-value p-value Contrast3[,"TreatmentYes"] <- 1 anova(fmi, L = Contrast3) Denom. DF: 74 F-test for linear combination(s) [1] 1 numdf F-value p-value

12 4.3 Getting confidence intervals for the coefficients intervals(fm1) Approximate 95% confidence intervals Coefficients: lower est. upper (Intercept) GenderMale Age TreatmentYes attr(,"label") [1] "Coefficients:" Correlation structure: lower est. upper Rho attr(,"label") [1] "Correlation structure:" Residual standard error: lower est. upper intervals(fm1,which = "coef") Approximate 95% confidence intervals Coefficients: lower est. upper (Intercept) GenderMale Age TreatmentYes attr(,"label") [1] "Coefficients:" 12

13 4.4 Displaying the variance covariance matrix getvarcov extracts the variance covariance matrix for patient 8 # compound symmetry getvarcov(fm1, individual = "8") Marginal variance covariance matrix [,1] [,2] [,3] [,4] [1,] [2,] [3,] [4,] Standard Deviations: # unstructured with constant variance getvarcov(fm2, individual = "8") Marginal variance covariance matrix [,1] [,2] [,3] [,4] [1,] [2,] [3,] [4,] Standard Deviations: # unstructured with different variances getvarcov(fm3, individual = "8") Marginal variance covariance matrix [,1] [,2] [,3] [,4] [1,] [2,] [3,] [4,] Standard Deviations: getvarcov(fm3, individual = "1") # patient with no observation at time 4 Marginal variance covariance matrix [,1] [,2] [,3] [1,] [2,] [3,] Standard Deviations:

14 4.5 Predictions Punctual estimate When performing prediction, the safest way is to build a data.frame that precisely match the format of the dataset used to train the model. Care must be taken in presence of factor since the predict function will ask to find the same levels for the factor variables. m.gls <- gls(outcome ~ Gender + Age + Treatment, correlation = corcompsymm(form = ~1 Id), data = dtl.data) str(dtl.data[,.(gender,age,treatment)]) Classes 'data.table' and 'data.frame': 79 obs. of 3 variables: $ Gender : Factor w/ 2 levels "Female","Male": $ Age : num $ Treatment: Factor w/ 2 levels "No","Yes": attr(*, ".internal.selfref")=<externalptr> df.test <- data.frame(gender = factor("male", levels = c("male","female")), Age = 25, Treatment = factor("no", levels = c("no","yes")) ) predict(m.gls, newdata = df.test) [1] attr(,"label") [1] "Predicted values" Note: when considering a mixed model, the prediction can be either made at the population level (unknown random effect) or at the individual level (known random effect). Note: In presence of missing values dtl.datana <- dtl.data dtl.datana[1:5,"outcome"] <- NA m.gls <- gls(outcome ~ Gender + Age + Treatment, correlation = corcompsymm(form = ~1 Id), data = dtl.datana, na.action = na.exclude) there is a difference between: 14

15 pred <- predict(m.gls, newdata = df.test) sum(is.na(pred)) # predicted values [1] 0 and fit <- predict(m.gls) sum(is.na(fit)) # fitted values [1] Confidence intervals Using the predictse.gls function from the package AICcmodavg: library(aiccmodavg,quietly = TRUE) dftempo <- data.frame(predictse.gls(m.gls, newdata = dtl.datana)) quantile_norm <- qnorm(p = 0.975) dftempo$fit_lower <- dftempo$fit - quantile_norm * dftempo$se.fit dftempo$fit_upper <- dftempo$fit + quantile_norm * dftempo$se.fit head(dftempo) fit se.fit fit_lower fit_upper If we want to do it ourself we can follow : the general recipe for computing predictions from a linear or generalized linear model is to: 1- figure out the model matrix X corresponding to the new data; 2- matrix-multiply X by the parameter vector β to get the predictions; 3- extract the variance-covariance matrix of the parameters V 4- compute XVX to get the variance-covariance matrix of the predictions; 5- extract the diagonal of this matrix to get variances of predictions; 15

16 6- take the square-root of the variances to get the standard deviations (errors) of the predictions; 7- compute confidence intervals based on a Normal approximation;" (from 1 Xmatrix <- model.matrix( ~ Gender + Age + Treatment, dtl.datana) 2 predictions <- predict(m.gls, newdata = dtl.datana) # same as Xmatrix %*% lme.1$coefficients$fixed # same as df.pred_swabs$predicted.1 3 VCOV.beta <- vcov(m.gls) 4 VCOV.predictions <- Xmatrix %*% VCOV.beta %*% t(xmatrix) 5 VAR.predictions <- diag(vcov.predictions) 6 SE.predictions <- sqrt(var.predictions) 7 dtl.datana$predicted_lower <- predictions - quantile_norm * SE.predictions dtl.datana$predicted_upper <- predictions + quantile_norm * SE.predictions Check that both approaches agrees: identical(as.double(dftempo$fit),as.double(predictions)) [1] TRUE identical(as.double(dftempo$se.fit),as.double(se.predictions)) [1] TRUE 16

17 References Bates, Douglas M lme4: Mixed-effects modeling with R. org/lmmwr/lrgprt.pdf. Galecki, Andrzej, and Tomasz Burzykowski Linear Mixed-Effects Models Using R: A Step-by-Step Approach. Statistics. Springer. Pinheiro, J. C., and D. M. Bates Mixed Effects Models in Sand S-PLUS. 17

Solution pigs exercise

Solution pigs exercise Solution pigs exercise Course repeated measurements - R exercise class 2 November 24, 2017 Contents 1 Question 1: Import data 3 1.1 Data management..................................... 3 1.2 Inspection

More information

R Output for Linear Models using functions lm(), gls() & glm()

R Output for Linear Models using functions lm(), gls() & glm() LM 04 lm(), gls() &glm() 1 R Output for Linear Models using functions lm(), gls() & glm() Different kinds of output related to linear models can be obtained in R using function lm() {stats} in the base

More information

These slides illustrate a few example R commands that can be useful for the analysis of repeated measures data.

These slides illustrate a few example R commands that can be useful for the analysis of repeated measures data. These slides illustrate a few example R commands that can be useful for the analysis of repeated measures data. We focus on the experiment designed to compare the effectiveness of three strength training

More information

A Handbook of Statistical Analyses Using R 2nd Edition. Brian S. Everitt and Torsten Hothorn

A Handbook of Statistical Analyses Using R 2nd Edition. Brian S. Everitt and Torsten Hothorn A Handbook of Statistical Analyses Using R 2nd Edition Brian S. Everitt and Torsten Hothorn CHAPTER 12 Analysing Longitudinal Data I: Computerised Delivery of Cognitive Behavioural Therapy Beat the Blues

More information

Solution: anti-fungal treatment exercise

Solution: anti-fungal treatment exercise Solution: anti-fungal treatment exercise Course repeated measurements - R exercise class 5 December 5, 2017 Contents 1 Question 1: Import data 2 1.1 Data management.....................................

More information

A Handbook of Statistical Analyses Using R 2nd Edition. Brian S. Everitt and Torsten Hothorn

A Handbook of Statistical Analyses Using R 2nd Edition. Brian S. Everitt and Torsten Hothorn A Handbook of Statistical Analyses Using R 2nd Edition Brian S. Everitt and Torsten Hothorn CHAPTER 12 Analysing Longitudinal Data I: Computerised Delivery of Cognitive Behavioural Therapy Beat the Blues

More information

Workshop 9.3a: Randomized block designs

Workshop 9.3a: Randomized block designs -1- Workshop 93a: Randomized block designs Murray Logan November 23, 16 Table of contents 1 Randomized Block (RCB) designs 1 2 Worked Examples 12 1 Randomized Block (RCB) designs 11 RCB design Simple Randomized

More information

Stat 579: Generalized Linear Models and Extensions

Stat 579: Generalized Linear Models and Extensions Stat 579: Generalized Linear Models and Extensions Linear Mixed Models for Longitudinal Data Yan Lu April, 2018, week 14 1 / 64 Data structure and Model t1 t2 tn i 1st subject y 11 y 12 y 1n1 2nd subject

More information

A Handbook of Statistical Analyses Using R. Brian S. Everitt and Torsten Hothorn

A Handbook of Statistical Analyses Using R. Brian S. Everitt and Torsten Hothorn A Handbook of Statistical Analyses Using R Brian S. Everitt and Torsten Hothorn CHAPTER 10 Analysing Longitudinal Data I: Computerised Delivery of Cognitive Behavioural Therapy Beat the Blues 10.1 Introduction

More information

Intruction to General and Generalized Linear Models

Intruction to General and Generalized Linear Models Intruction to General and Generalized Linear Models Mixed Effects Models IV Henrik Madsen Anna Helga Jónsdóttir hm@imm.dtu.dk April 30, 2012 Henrik Madsen Anna Helga Jónsdóttir (hm@imm.dtu.dk) Intruction

More information

Repeated measures, part 2, advanced methods

Repeated measures, part 2, advanced methods enote 12 1 enote 12 Repeated measures, part 2, advanced methods enote 12 INDHOLD 2 Indhold 12 Repeated measures, part 2, advanced methods 1 12.1 Intro......................................... 3 12.2 A

More information

STAT3401: Advanced data analysis Week 10: Models for Clustered Longitudinal Data

STAT3401: Advanced data analysis Week 10: Models for Clustered Longitudinal Data STAT3401: Advanced data analysis Week 10: Models for Clustered Longitudinal Data Berwin Turlach School of Mathematics and Statistics Berwin.Turlach@gmail.com The University of Western Australia Models

More information

#Alternatively we could fit a model where the rail values are levels of a factor with fixed effects

#Alternatively we could fit a model where the rail values are levels of a factor with fixed effects examples-lme.r Tue Nov 25 12:32:20 2008 1 library(nlme) # The following data shows the results of tests carried over 6 rails. The response # indicated the time needed for a an ultrasonic wave to travel

More information

Linear, Generalized Linear, and Mixed-Effects Models in R. Linear and Generalized Linear Models in R Topics

Linear, Generalized Linear, and Mixed-Effects Models in R. Linear and Generalized Linear Models in R Topics Linear, Generalized Linear, and Mixed-Effects Models in R John Fox McMaster University ICPSR 2018 John Fox (McMaster University) Statistical Models in R ICPSR 2018 1 / 19 Linear and Generalized Linear

More information

Package r2glmm. August 5, 2017

Package r2glmm. August 5, 2017 Type Package Package r2glmm August 5, 2017 Title Computes R Squared for Mixed (Multilevel) Models Date 2017-08-04 Version 0.1.2 The model R squared and semi-partial R squared for the linear and generalized

More information

Lecture 9 STK3100/4100

Lecture 9 STK3100/4100 Lecture 9 STK3100/4100 27. October 2014 Plan for lecture: 1. Linear mixed models cont. Models accounting for time dependencies (Ch. 6.1) 2. Generalized linear mixed models (GLMM, Ch. 13.1-13.3) Examples

More information

Overview. 1. Independence. 2. Modeling Autocorrelation. 3. Temporal Autocorrelation Example. 4. Spatial Autocorrelation Example

Overview. 1. Independence. 2. Modeling Autocorrelation. 3. Temporal Autocorrelation Example. 4. Spatial Autocorrelation Example 6. Autocorrelation Overview 1. Independence 2. Modeling Autocorrelation 3. Temporal Autocorrelation Example 4. Spatial Autocorrelation Example 6.1 Independence 6.1 Independence. Model assumptions All linear

More information

Stat 209 Lab: Linear Mixed Models in R This lab covers the Linear Mixed Models tutorial by John Fox. Lab prepared by Karen Kapur. ɛ i Normal(0, σ 2 )

Stat 209 Lab: Linear Mixed Models in R This lab covers the Linear Mixed Models tutorial by John Fox. Lab prepared by Karen Kapur. ɛ i Normal(0, σ 2 ) Lab 2 STAT209 1/31/13 A complication in doing all this is that the package nlme (lme) is supplanted by the new and improved lme4 (lmer); both are widely used so I try to do both tracks in separate Rogosa

More information

Solution Anti-fungal treatment (R software)

Solution Anti-fungal treatment (R software) Contents Solution Anti-fungal treatment (R software) Question 1: Data import 2 Question 2: Compliance with the timetable 4 Question 3: population average model 5 Question 4: continuous time model 9 Question

More information

The First Thing You Ever Do When Receive a Set of Data Is

The First Thing You Ever Do When Receive a Set of Data Is The First Thing You Ever Do When Receive a Set of Data Is Understand the goal of the study What are the objectives of the study? What would the person like to see from the data? Understand the methodology

More information

Package HGLMMM for Hierarchical Generalized Linear Models

Package HGLMMM for Hierarchical Generalized Linear Models Package HGLMMM for Hierarchical Generalized Linear Models Marek Molas Emmanuel Lesaffre Erasmus MC Erasmus Universiteit - Rotterdam The Netherlands ERASMUSMC - Biostatistics 20-04-2010 1 / 52 Outline General

More information

Mixed models with correlated measurement errors

Mixed models with correlated measurement errors Mixed models with correlated measurement errors Rasmus Waagepetersen October 9, 2018 Example from Department of Health Technology 25 subjects where exposed to electric pulses of 11 different durations

More information

Inferences on Linear Combinations of Coefficients

Inferences on Linear Combinations of Coefficients Inferences on Linear Combinations of Coefficients Note on required packages: The following code required the package multcomp to test hypotheses on linear combinations of regression coefficients. If you

More information

Introduction and Background to Multilevel Analysis

Introduction and Background to Multilevel Analysis Introduction and Background to Multilevel Analysis Dr. J. Kyle Roberts Southern Methodist University Simmons School of Education and Human Development Department of Teaching and Learning Background and

More information

STAT 526 Advanced Statistical Methodology

STAT 526 Advanced Statistical Methodology STAT 526 Advanced Statistical Methodology Fall 2017 Lecture Note 10 Analyzing Clustered/Repeated Categorical Data 0-0 Outline Clustered/Repeated Categorical Data Generalized Linear Mixed Models Generalized

More information

Independent Samples t tests. Background for Independent Samples t test

Independent Samples t tests. Background for Independent Samples t test Independent Samples t tests Dr. J. Kyle Roberts Southern Methodist University Simmons School of Education and Human Development Department of Teaching and Learning Background for Independent Samples t

More information

Introduction to Mixed Models in R

Introduction to Mixed Models in R Introduction to Mixed Models in R Galin Jones School of Statistics University of Minnesota http://www.stat.umn.edu/ galin March 2011 Second in a Series Sponsored by Quantitative Methods Collaborative.

More information

General Linear Model (Chapter 4)

General Linear Model (Chapter 4) General Linear Model (Chapter 4) Outcome variable is considered continuous Simple linear regression Scatterplots OLS is BLUE under basic assumptions MSE estimates residual variance testing regression coefficients

More information

A brief introduction to mixed models

A brief introduction to mixed models A brief introduction to mixed models University of Gothenburg Gothenburg April 6, 2017 Outline An introduction to mixed models based on a few examples: Definition of standard mixed models. Parameter estimation.

More information

13. October p. 1

13. October p. 1 Lecture 8 STK3100/4100 Linear mixed models 13. October 2014 Plan for lecture: 1. The lme function in the nlme library 2. Induced correlation structure 3. Marginal models 4. Estimation - ML and REML 5.

More information

Coping with Additional Sources of Variation: ANCOVA and Random Effects

Coping with Additional Sources of Variation: ANCOVA and Random Effects Coping with Additional Sources of Variation: ANCOVA and Random Effects 1/49 More Noise in Experiments & Observations Your fixed coefficients are not always so fixed Continuous variation between samples

More information

Non-Gaussian Response Variables

Non-Gaussian Response Variables Non-Gaussian Response Variables What is the Generalized Model Doing? The fixed effects are like the factors in a traditional analysis of variance or linear model The random effects are different A generalized

More information

R-companion to: Estimation of the Thurstonian model for the 2-AC protocol

R-companion to: Estimation of the Thurstonian model for the 2-AC protocol R-companion to: Estimation of the Thurstonian model for the 2-AC protocol Rune Haubo Bojesen Christensen, Hye-Seong Lee & Per Bruun Brockhoff August 24, 2017 This document describes how the examples in

More information

mgcv: GAMs in R Simon Wood Mathematical Sciences, University of Bath, U.K.

mgcv: GAMs in R Simon Wood Mathematical Sciences, University of Bath, U.K. mgcv: GAMs in R Simon Wood Mathematical Sciences, University of Bath, U.K. mgcv, gamm4 mgcv is a package supplied with R for generalized additive modelling, including generalized additive mixed models.

More information

Hierarchical Random Effects

Hierarchical Random Effects enote 5 1 enote 5 Hierarchical Random Effects enote 5 INDHOLD 2 Indhold 5 Hierarchical Random Effects 1 5.1 Introduction.................................... 2 5.2 Main example: Lactase measurements in

More information

Lecture 2. The Simple Linear Regression Model: Matrix Approach

Lecture 2. The Simple Linear Regression Model: Matrix Approach Lecture 2 The Simple Linear Regression Model: Matrix Approach Matrix algebra Matrix representation of simple linear regression model 1 Vectors and Matrices Where it is necessary to consider a distribution

More information

Finite Mixture Model Diagnostics Using Resampling Methods

Finite Mixture Model Diagnostics Using Resampling Methods Finite Mixture Model Diagnostics Using Resampling Methods Bettina Grün Johannes Kepler Universität Linz Friedrich Leisch Universität für Bodenkultur Wien Abstract This paper illustrates the implementation

More information

Inference with Heteroskedasticity

Inference with Heteroskedasticity Inference with Heteroskedasticity Note on required packages: The following code requires the packages sandwich and lmtest to estimate regression error variance that may change with the explanatory variables.

More information

Introduction to SAS proc mixed

Introduction to SAS proc mixed Faculty of Health Sciences Introduction to SAS proc mixed Analysis of repeated measurements, 2017 Julie Forman Department of Biostatistics, University of Copenhagen 2 / 28 Preparing data for analysis The

More information

Linear Probability Model

Linear Probability Model Linear Probability Model Note on required packages: The following code requires the packages sandwich and lmtest to estimate regression error variance that may change with the explanatory variables. If

More information

A Re-Introduction to General Linear Models (GLM)

A Re-Introduction to General Linear Models (GLM) A Re-Introduction to General Linear Models (GLM) Today s Class: You do know the GLM Estimation (where the numbers in the output come from): From least squares to restricted maximum likelihood (REML) Reviewing

More information

Multivariable Fractional Polynomials

Multivariable Fractional Polynomials Multivariable Fractional Polynomials Axel Benner September 7, 2015 Contents 1 Introduction 1 2 Inventory of functions 1 3 Usage in R 2 3.1 Model selection........................................ 3 4 Example

More information

Package blme. August 29, 2016

Package blme. August 29, 2016 Version 1.0-4 Date 2015-06-13 Title Bayesian Linear Mixed-Effects Models Author Vincent Dorie Maintainer Vincent Dorie Package blme August 29, 2016 Description Maximum a posteriori

More information

Mixed effects models

Mixed effects models Mixed effects models The basic theory and application in R Mitchel van Loon Research Paper Business Analytics Mixed effects models The basic theory and application in R Author: Mitchel van Loon Research

More information

Correlated Data: Linear Mixed Models with Random Intercepts

Correlated Data: Linear Mixed Models with Random Intercepts 1 Correlated Data: Linear Mixed Models with Random Intercepts Mixed Effects Models This lecture introduces linear mixed effects models. Linear mixed models are a type of regression model, which generalise

More information

Introduction to Within-Person Analysis and RM ANOVA

Introduction to Within-Person Analysis and RM ANOVA Introduction to Within-Person Analysis and RM ANOVA Today s Class: From between-person to within-person ANOVAs for longitudinal data Variance model comparisons using 2 LL CLP 944: Lecture 3 1 The Two Sides

More information

Answer to exercise: Blood pressure lowering drugs

Answer to exercise: Blood pressure lowering drugs Answer to exercise: Blood pressure lowering drugs The data set bloodpressure.txt contains data from a cross-over trial, involving three different formulations of a drug for lowering of blood pressure:

More information

Lecture 15 Topic 11: Unbalanced Designs (missing data)

Lecture 15 Topic 11: Unbalanced Designs (missing data) Lecture 15 Topic 11: Unbalanced Designs (missing data) In the real world, things fall apart: plants are destroyed/trampled/eaten animals get sick volunteers quit assistants are sloppy accidents happen

More information

Introduction to SAS proc mixed

Introduction to SAS proc mixed Faculty of Health Sciences Introduction to SAS proc mixed Analysis of repeated measurements, 2017 Julie Forman Department of Biostatistics, University of Copenhagen Outline Data in wide and long format

More information

lme4 Luke Chang Last Revised July 16, Fitting Linear Mixed Models with a Varying Intercept

lme4 Luke Chang Last Revised July 16, Fitting Linear Mixed Models with a Varying Intercept lme4 Luke Chang Last Revised July 16, 2010 1 Using lme4 1.1 Fitting Linear Mixed Models with a Varying Intercept We will now work through the same Ultimatum Game example from the regression section and

More information

Week 7 Multiple factors. Ch , Some miscellaneous parts

Week 7 Multiple factors. Ch , Some miscellaneous parts Week 7 Multiple factors Ch. 18-19, Some miscellaneous parts Multiple Factors Most experiments will involve multiple factors, some of which will be nuisance variables Dealing with these factors requires

More information

Information. Hierarchical Models - Statistical Methods. References. Outline

Information. Hierarchical Models - Statistical Methods. References. Outline Information Hierarchical Models - Statistical Methods Sarah Filippi 1 University of Oxford Hilary Term 2015 Webpage: http://www.stats.ox.ac.uk/~filippi/msc_ hierarchicalmodels_2015.html Lectures: Week

More information

Random and Mixed Effects Models - Part II

Random and Mixed Effects Models - Part II Random and Mixed Effects Models - Part II Statistics 149 Spring 2006 Copyright 2006 by Mark E. Irwin Two-Factor Random Effects Model Example: Miles per Gallon (Neter, Kutner, Nachtsheim, & Wasserman, problem

More information

Package MonoPoly. December 20, 2017

Package MonoPoly. December 20, 2017 Type Package Title Functions to Fit Monotone Polynomials Version 0.3-9 Date 2017-12-20 Package MonoPoly December 20, 2017 Functions for fitting monotone polynomials to data. Detailed discussion of the

More information

Outline. Linear OLS Models vs: Linear Marginal Models Linear Conditional Models. Random Intercepts Random Intercepts & Slopes

Outline. Linear OLS Models vs: Linear Marginal Models Linear Conditional Models. Random Intercepts Random Intercepts & Slopes Lecture 2.1 Basic Linear LDA 1 Outline Linear OLS Models vs: Linear Marginal Models Linear Conditional Models Random Intercepts Random Intercepts & Slopes Cond l & Marginal Connections Empirical Bayes

More information

Workshop 9.1: Mixed effects models

Workshop 9.1: Mixed effects models -1- Workshop 91: Mixed effects models Murray Logan October 10, 2016 Table of contents 1 Non-independence - part 2 1 1 Non-independence - part 2 11 Linear models Homogeneity of variance σ 2 0 0 y i = β

More information

Gov 2000: 9. Regression with Two Independent Variables

Gov 2000: 9. Regression with Two Independent Variables Gov 2000: 9. Regression with Two Independent Variables Matthew Blackwell Fall 2016 1 / 62 1. Why Add Variables to a Regression? 2. Adding a Binary Covariate 3. Adding a Continuous Covariate 4. OLS Mechanics

More information

STAT 572 Assignment 5 - Answers Due: March 2, 2007

STAT 572 Assignment 5 - Answers Due: March 2, 2007 1. The file glue.txt contains a data set with the results of an experiment on the dry sheer strength (in pounds per square inch) of birch plywood, bonded with 5 different resin glues A, B, C, D, and E.

More information

Time-Series Regression and Generalized Least Squares in R*

Time-Series Regression and Generalized Least Squares in R* Time-Series Regression and Generalized Least Squares in R* An Appendix to An R Companion to Applied Regression, third edition John Fox & Sanford Weisberg last revision: 2018-09-26 Abstract Generalized

More information

22s:152 Applied Linear Regression. Returning to a continuous response variable Y...

22s:152 Applied Linear Regression. Returning to a continuous response variable Y... 22s:152 Applied Linear Regression Generalized Least Squares Returning to a continuous response variable Y... Ordinary Least Squares Estimation The classical models we have fit so far with a continuous

More information

Multiple Predictor Variables: ANOVA

Multiple Predictor Variables: ANOVA Multiple Predictor Variables: ANOVA 1/32 Linear Models with Many Predictors Multiple regression has many predictors BUT - so did 1-way ANOVA if treatments had 2 levels What if there are multiple treatment

More information

Lecture 14: Introduction to Poisson Regression

Lecture 14: Introduction to Poisson Regression Lecture 14: Introduction to Poisson Regression Ani Manichaikul amanicha@jhsph.edu 8 May 2007 1 / 52 Overview Modelling counts Contingency tables Poisson regression models 2 / 52 Modelling counts I Why

More information

Modelling counts. Lecture 14: Introduction to Poisson Regression. Overview

Modelling counts. Lecture 14: Introduction to Poisson Regression. Overview Modelling counts I Lecture 14: Introduction to Poisson Regression Ani Manichaikul amanicha@jhsph.edu Why count data? Number of traffic accidents per day Mortality counts in a given neighborhood, per week

More information

Exploring Hierarchical Linear Mixed Models

Exploring Hierarchical Linear Mixed Models Exploring Hierarchical Linear Mixed Models 1/49 Last time... A Greenhouse Experiment testing C:N Ratios Sam was testing how changing the C:N Ratio of soil affected plant leaf growth. He had 3 treatments.

More information

Outline. Mixed models in R using the lme4 package Part 3: Longitudinal data. Sleep deprivation data. Simple longitudinal data

Outline. Mixed models in R using the lme4 package Part 3: Longitudinal data. Sleep deprivation data. Simple longitudinal data Outline Mixed models in R using the lme4 package Part 3: Longitudinal data Douglas Bates Longitudinal data: sleepstudy A model with random effects for intercept and slope University of Wisconsin - Madison

More information

Multivariable Fractional Polynomials

Multivariable Fractional Polynomials Multivariable Fractional Polynomials Axel Benner May 17, 2007 Contents 1 Introduction 1 2 Inventory of functions 1 3 Usage in R 2 3.1 Model selection........................................ 3 4 Example

More information

y i s 2 X 1 n i 1 1. Show that the least squares estimators can be written as n xx i x i 1 ns 2 X i 1 n ` px xqx i x i 1 pδ ij 1 n px i xq x j x

y i s 2 X 1 n i 1 1. Show that the least squares estimators can be written as n xx i x i 1 ns 2 X i 1 n ` px xqx i x i 1 pδ ij 1 n px i xq x j x Question 1 Suppose that we have data Let x 1 n x i px 1, y 1 q,..., px n, y n q. ȳ 1 n y i s 2 X 1 n px i xq 2 Throughout this question, we assume that the simple linear model is correct. We also assume

More information

1 gamma.mixed: Mixed effects gamma regression

1 gamma.mixed: Mixed effects gamma regression gamma.mixed: Mixed effects gamma regression Use generalized multi-level linear regression if you have covariates that are grouped according to one or more classification factors. Gamma regression models

More information

Simple, Marginal, and Interaction Effects in General Linear Models

Simple, Marginal, and Interaction Effects in General Linear Models Simple, Marginal, and Interaction Effects in General Linear Models PRE 905: Multivariate Analysis Lecture 3 Today s Class Centering and Coding Predictors Interpreting Parameters in the Model for the Means

More information

Lecture 3: Inference in SLR

Lecture 3: Inference in SLR Lecture 3: Inference in SLR STAT 51 Spring 011 Background Reading KNNL:.1.6 3-1 Topic Overview This topic will cover: Review of hypothesis testing Inference about 1 Inference about 0 Confidence Intervals

More information

22s:152 Applied Linear Regression. In matrix notation, we can write this model: Generalized Least Squares. Y = Xβ + ɛ with ɛ N n (0, Σ)

22s:152 Applied Linear Regression. In matrix notation, we can write this model: Generalized Least Squares. Y = Xβ + ɛ with ɛ N n (0, Σ) 22s:152 Applied Linear Regression Generalized Least Squares Returning to a continuous response variable Y Ordinary Least Squares Estimation The classical models we have fit so far with a continuous response

More information

Part II { Oneway Anova, Simple Linear Regression and ANCOVA with R

Part II { Oneway Anova, Simple Linear Regression and ANCOVA with R Part II { Oneway Anova, Simple Linear Regression and ANCOVA with R Gilles Lamothe February 21, 2017 Contents 1 Anova with one factor 2 1.1 The data.......................................... 2 1.2 A visual

More information

0.1 gamma.mixed: Mixed effects gamma regression

0.1 gamma.mixed: Mixed effects gamma regression 0. gamma.mixed: Mixed effects gamma regression Use generalized multi-level linear regression if you have covariates that are grouped according to one or more classification factors. Gamma regression models

More information

Supplemental Materials. In the main text, we recommend graphing physiological values for individual dyad

Supplemental Materials. In the main text, we recommend graphing physiological values for individual dyad 1 Supplemental Materials Graphing Values for Individual Dyad Members over Time In the main text, we recommend graphing physiological values for individual dyad members over time to aid in the decision

More information

Repeated measures, part 1, simple methods

Repeated measures, part 1, simple methods enote 11 1 enote 11 Repeated measures, part 1, simple methods enote 11 INDHOLD 2 Indhold 11 Repeated measures, part 1, simple methods 1 11.1 Intro......................................... 2 11.1.1 Main

More information

Designing Multilevel Models Using SPSS 11.5 Mixed Model. John Painter, Ph.D.

Designing Multilevel Models Using SPSS 11.5 Mixed Model. John Painter, Ph.D. Designing Multilevel Models Using SPSS 11.5 Mixed Model John Painter, Ph.D. Jordan Institute for Families School of Social Work University of North Carolina at Chapel Hill 1 Creating Multilevel Models

More information

Non-independence due to Time Correlation (Chapter 14)

Non-independence due to Time Correlation (Chapter 14) Non-independence due to Time Correlation (Chapter 14) When we model the mean structure with ordinary least squares, the mean structure explains the general trends in the data with respect to our dependent

More information

Log-linear Models for Contingency Tables

Log-linear Models for Contingency Tables Log-linear Models for Contingency Tables Statistics 149 Spring 2006 Copyright 2006 by Mark E. Irwin Log-linear Models for Two-way Contingency Tables Example: Business Administration Majors and Gender A

More information

Gov 2000: 9. Regression with Two Independent Variables

Gov 2000: 9. Regression with Two Independent Variables Gov 2000: 9. Regression with Two Independent Variables Matthew Blackwell Harvard University mblackwell@gov.harvard.edu Where are we? Where are we going? Last week: we learned about how to calculate a simple

More information

Value Added Modeling

Value Added Modeling Value Added Modeling Dr. J. Kyle Roberts Southern Methodist University Simmons School of Education and Human Development Department of Teaching and Learning Background for VAMs Recall from previous lectures

More information

Random effects & Repeated measurements

Random effects & Repeated measurements Random effects & Repeated measurements Bo Markussen bomar@math.ku.dk Department of Mathematical Sciences December 20, 2017 LAST (MATH) AS / SmB Day 5 1 / 55 Lecture outline Hypothetical data example: Comparison

More information

Mixed models in R using the lme4 package Part 2: Longitudinal data, modeling interactions

Mixed models in R using the lme4 package Part 2: Longitudinal data, modeling interactions Mixed models in R using the lme4 package Part 2: Longitudinal data, modeling interactions Douglas Bates Department of Statistics University of Wisconsin - Madison Madison January 11, 2011

More information

STK 2100 Oblig 1. Zhou Siyu. February 15, 2017

STK 2100 Oblig 1. Zhou Siyu. February 15, 2017 STK 200 Oblig Zhou Siyu February 5, 207 Question a) Make a scatter box plot for the data set. Answer:Here is the code I used to plot the scatter box in R. library ( MASS ) 2 pairs ( Boston ) Figure : Scatter

More information

Temporal Learning: IS50 prior RT

Temporal Learning: IS50 prior RT Temporal Learning: IS50 prior RT Loading required package: Matrix Jihyun Suh 1/27/2016 This data.table install has not detected OpenMP support. It will work but slower in single threaded m Attaching package:

More information

Investigating Models with Two or Three Categories

Investigating Models with Two or Three Categories Ronald H. Heck and Lynn N. Tabata 1 Investigating Models with Two or Three Categories For the past few weeks we have been working with discriminant analysis. Let s now see what the same sort of model might

More information

(Where does Ch. 7 on comparing 2 means or 2 proportions fit into this?)

(Where does Ch. 7 on comparing 2 means or 2 proportions fit into this?) 12. Comparing Groups: Analysis of Variance (ANOVA) Methods Response y Explanatory x var s Method Categorical Categorical Contingency tables (Ch. 8) (chi-squared, etc.) Quantitative Quantitative Regression

More information

Regression Analysis in R

Regression Analysis in R Regression Analysis in R 1 Purpose The purpose of this activity is to provide you with an understanding of regression analysis and to both develop and apply that knowledge to the use of the R statistical

More information

M. H. Gonçalves 1 M. S. Cabral 2 A. Azzalini 3

M. H. Gonçalves 1 M. S. Cabral 2 A. Azzalini 3 M. H. Gonçalves 1 M. S. Cabral 2 A. Azzalini 3 1 University of Algarve, Portugal 2 University of Lisbon, Portugal 3 Università di Padova, Italy user!2010, July 21-23 1 2 3 4 5 What is bild? an R. parametric

More information

Regression: Main Ideas Setting: Quantitative outcome with a quantitative explanatory variable. Example, cont.

Regression: Main Ideas Setting: Quantitative outcome with a quantitative explanatory variable. Example, cont. TCELL 9/4/205 36-309/749 Experimental Design for Behavioral and Social Sciences Simple Regression Example Male black wheatear birds carry stones to the nest as a form of sexual display. Soler et al. wanted

More information

Topic 28: Unequal Replication in Two-Way ANOVA

Topic 28: Unequal Replication in Two-Way ANOVA Topic 28: Unequal Replication in Two-Way ANOVA Outline Two-way ANOVA with unequal numbers of observations in the cells Data and model Regression approach Parameter estimates Previous analyses with constant

More information

UNIVERSITY OF TORONTO. Faculty of Arts and Science APRIL 2010 EXAMINATIONS STA 303 H1S / STA 1002 HS. Duration - 3 hours. Aids Allowed: Calculator

UNIVERSITY OF TORONTO. Faculty of Arts and Science APRIL 2010 EXAMINATIONS STA 303 H1S / STA 1002 HS. Duration - 3 hours. Aids Allowed: Calculator UNIVERSITY OF TORONTO Faculty of Arts and Science APRIL 2010 EXAMINATIONS STA 303 H1S / STA 1002 HS Duration - 3 hours Aids Allowed: Calculator LAST NAME: FIRST NAME: STUDENT NUMBER: There are 27 pages

More information

Analysis of means: Examples using package ANOM

Analysis of means: Examples using package ANOM Analysis of means: Examples using package ANOM Philip Pallmann February 15, 2016 Contents 1 Introduction 1 2 ANOM in a two-way layout 2 3 ANOM with (overdispersed) count data 4 4 ANOM with linear mixed-effects

More information

Univariate Analysis of Variance

Univariate Analysis of Variance Univariate Analysis of Variance Output Created Comments Input Missing Value Handling Syntax Resources Notes Data Active Dataset Filter Weight Split File N of Rows in Working Data File Definition of Missing

More information

Workshop 7.4a: Single factor ANOVA

Workshop 7.4a: Single factor ANOVA -1- Workshop 7.4a: Single factor ANOVA Murray Logan November 23, 2016 Table of contents 1 Revision 1 2 Anova Parameterization 2 3 Partitioning of variance (ANOVA) 10 4 Worked Examples 13 1. Revision 1.1.

More information

Regression #8: Loose Ends

Regression #8: Loose Ends Regression #8: Loose Ends Econ 671 Purdue University Justin L. Tobias (Purdue) Regression #8 1 / 30 In this lecture we investigate a variety of topics that you are probably familiar with, but need to touch

More information

Mixed Model: Split plot with two whole-plot factors, one split-plot factor, and CRD at the whole-plot level (e.g. fancier split-plot p.

Mixed Model: Split plot with two whole-plot factors, one split-plot factor, and CRD at the whole-plot level (e.g. fancier split-plot p. STAT:5201 Applied Statistic II Mixed Model: Split plot with two whole-plot factors, one split-plot factor, and CRD at the whole-plot level (e.g. fancier split-plot p.422 OLRT) Hamster example with three

More information

Subject-specific observed profiles of log(fev1) vs age First 50 subjects in Six Cities Study

Subject-specific observed profiles of log(fev1) vs age First 50 subjects in Six Cities Study Subject-specific observed profiles of log(fev1) vs age First 50 subjects in Six Cities Study 1.4 0.0-6 7 8 9 10 11 12 13 14 15 16 17 18 19 age Model 1: A simple broken stick model with knot at 14 fit with

More information

Mixed models in R using the lme4 package Part 5: Generalized linear mixed models

Mixed models in R using the lme4 package Part 5: Generalized linear mixed models Mixed models in R using the lme4 package Part 5: Generalized linear mixed models Douglas Bates 2011-03-16 Contents 1 Generalized Linear Mixed Models Generalized Linear Mixed Models When using linear mixed

More information

Interactions among Continuous Predictors

Interactions among Continuous Predictors Interactions among Continuous Predictors Today s Class: Simple main effects within two-way interactions Conquering TEST/ESTIMATE/LINCOM statements Regions of significance Three-way interactions (and beyond

More information

Homework 3 - Solution

Homework 3 - Solution STAT 526 - Spring 2011 Homework 3 - Solution Olga Vitek Each part of the problems 5 points 1. KNNL 25.17 (Note: you can choose either the restricted or the unrestricted version of the model. Please state

More information