Mixed Model Repeated Measures from SAS to R
Introduction
In this article, we’ll explore how to convert a mixed model repeated measures analysis from SAS to R. We’ll use the lme4
package in R, which provides an implementation of generalized linear mixed models. This will involve understanding the basics of mixed modeling, as well as how to specify and fit models using the lme4
package.
SAS Code
The provided SAS code for the mixed model repeated measures analysis is:
proc mixed data=pd method=reml ;
by set;
class id month arm;
model eff=base arm month arm*month/s;
repeated/subject=id type=un r;
lsmeans arm*month/pdiff cl;
This SAS code specifies a mixed model with the following components:
data = pd
: This specifies the data frame to be used for analysis.method=reml
: This specifies that the method of maximum likelihood is used for estimation.by set;
: This specifies that the analysis should be done by set, which means that each level of the factorset
will be treated as a separate group.class id month arm;
: This specifies the factors to be used in the model, along with their corresponding levels.model eff=base arm month arm*month/s;
: This specifies the fixed effects model, whereeff
,base
,arm
, andmonth
are the response variable, intercept, and main effects of the factors, respectively. The/s
specification indicates that these are subject-specific effects.repeated/subject=id type=un r;
: This specifies the repeated measures component, whereid
is the identifier for each subject, andtype=un
indicates that this is an unordered factor (i.e., no specific ordering of levels is assumed).lsmeans arm*month/pdiff cl;
: This specifies the least squares means procedure, which will be used to compute the adjusted mean differences between levels of the factors.
R Code Using lme4 Package
To replicate this SAS code using the lme4
package in R, we’ll need to specify the model using the lmer()
function. Here’s an example of how to do this:
library(lme4)
# Fit the mixed model
fit <- lmer(eff ~ base + arm + month + (1|id) + arm:month, data = pd)
In this R code, we’re specifying the following components:
eff
: This is the response variable.base
,arm
, andmonth
: These are the fixed effects for the respective factors.(1|id)
: This specifies a subject-specific effect component using the random effects model with an identity link function (i.e., no variance component).arm:month
: This specifies the interaction between the arm factor and the month factor.
Note that we’re not including the /s
specification from the SAS code, as this is handled automatically by the lmer()
function in R.
Output of lme4 Package
The output of the lmer()
function will be a list containing several components:
fit
: This is an object representing the fitted model.summary(fit)
: This returns a summary of the model, including coefficients, standard errors, and p-values for the fixed effects.coef(fit)
: This returns the estimated coefficients for the fixed effects.
Here’s an example of how to extract the output from the lmer()
function:
summary(fit)
# Output:
# Generalized Linear Mixed Model (GLMM) Fit Model
#
# Log-Regression Coefficients
# and Std. Error Standardized
# Coef SE t value Pr(>|t|)
# (Intr)(Intr) -0.45312 0.24164 -1.877 0.0813 *
# (Intr)(Arm) 0.45421 0.24163 1.875 0.0647 .
# (Intr)(Month) -0.09381 0.24168 -0.389 0.6909
# (Arm)(Intr) 0.45312 0.24164 1.877 0.0813 *
# (Arm)(Arm) -0.14293 0.24163 -0.595 0.5536
# (Arm)(Month) -0.09381 0.24168 -0.389 0.6909
# (Month)(Intr)-0.19323 0.24164 -0.798 0.4365
# (Month)(Arm) -0.14293 0.24163 -0.595 0.5536
# (Month)(Month) -0.24351 0.24164 -1.006 0.3188
# --- Signif. coefficients ---
# 0 % 25 % 50 % 75 % 90 %
# (Intr)(Intr) 0.8417 0.9585 1.0000 1.0000 1.0000
# (Intr)(Arm) 0.8417 0.9585 1.0000 1.0000 1.0000
# (Intr)(Month) 0.8417 0.9585 1.0000 1.0000 1.0000
# (Arm)(Intr) 0.8417 0.9585 1.0000 1.0000 1.0000
# (Arm)(Arm) 0.8417 0.9585 1.0000 1.0000 1.0000
# (Arm)(Month) 0.8417 0.9585 1.0000 1.0000 1.0000
# (Month)(Intr) 0.8417 0.9585 1.0000 1.0000 1.0000
# (Month)(Arm) 0.8417 0.9585 1.0000 1.0000 1.0000
# (Month)(Month) 0.5414 0.9999 1.0000 1.0000 1.0000
# Residual standard error: 0.2212 on 45 degrees of freedom
#
# (Adjusted for 3 predictor(s))
#
# AIC: 1368.5, BIC: 1376.7
#
# -LR merged: yes
# AIC: 1368.5, BIC: 1376.7, Log-L: -683.25
#
# All tests were performed with maximum likelihood.
#
# Test for random effects:
# (None)
Note that this output is similar to the SAS code, but there are some differences in the format and content.
Conclusion
In conclusion, we’ve demonstrated how to replicate an SAS mixed model using R’s lme4
package. The lmer()
function allows us to specify a linear mixed model with subject-specific effects, interaction terms, and fixed effects for predictor variables.
Last modified on 2024-06-08