Summarize and plot longitudinal data with missing measurements.
Fit and interpret linear mixed effects models with a random intercept, and with a random intercept and slope.
Compare models with AIC and test whether treatment groups differ in their trends over time.
Obtain and interpret subject-specific intercepts and slopes (BLUPs).
Instructions
Put chol.csv (on Canvas) in the same folder as this file. Write your code in the chunks provided and your answers, in full sentences, below each question. Type model equations in LaTeX between $$ signs. Render to HTML and submit the .html file.
Background
In the National Cooperative Gallstone Study (NCGS), a major interest was the safety of the drug chenodiol for treating cholesterol gallstones. Patients were randomly assigned to high-dose chenodiol (750 mg per day), low-dose chenodiol (375 mg per day), or placebo. This assignment uses a subset of patients who had floating gallstones and were assigned to high dose or placebo.
Chenodiol was expected to dissolve gallstones but might raise serum cholesterol. Serum cholesterol (mg/dL) was therefore measured at baseline, before treatment started (month 0), and at 6, 12, 20, and 24 months of follow-up. Some measurements are missing because of missed visits, lost or inadequate blood specimens, or termination of follow-up.
Variables in chol.csv:
id: patient ID
trt: treatment group (1 = high dose, 2 = placebo)
y1 to y5: serum cholesterol at months 0, 6, 12, 20, and 24
Data preparation
This code reads the data, reshapes it to long format (one row per patient per visit), and removes visits with no measurement. Mixed models use all remaining measurements.
library(nlme) # lme() for linear mixed effects modelschol <-read.csv("chol.csv") # read the data: one row per patientchol$high_dose <-as.numeric(chol$trt ==1) # indicator: 1 = high dose, 0 = placebochol_long <-reshape(chol, # the wide datadirection ="long", # convert from wide to longvarying =list(c("y1", "y2", "y3", "y4", "y5")), # the columns to stackv.names ="cholesterol", # name of the outcome columntimevar ="month", # name of the time columntimes =c(0, 6, 12, 20, 24), # the month for y1 to y5idvar ="id") # identifies each patientchol_long <- chol_long[order(chol_long$id, chol_long$month), ] # sort by patient, then monthchol_long <- chol_long[!is.na(chol_long$cholesterol), ] # drop visits with no measurementhead(chol_long, 10) # show the first 10 rows
In a table, report the number of measurements, mean, and standard deviation of serum cholesterol at each occasion for each treatment group. Then plot mean serum cholesterol versus time (in months) for the two groups. Describe the time trends in each group and how the number of measurements changes over follow-up.
Over time, both groups had less patients showing up for follow up at each time point.
Furthermore, in both the high dose and placebo groups, mean serum cholesterol increased over time.
Question 2
Model cholesterol over time with a random intercept for each patient. Let the mean intercept and slope depend on treatment: include treatment, time (in months, as a continuous variable), and a treatment × time interaction as fixed effects. Before fitting the model, write the model equation using β terms (not estimated values). Report the estimated coefficients with standard errors and p-values in a table.
Extend the model from Question 2 to include a random slope for time. Write the model equation, fit the model, and report the estimated coefficients with standard errors and p-values in a table. Based on AIC, which model fits the data better, the model from Question 2 or from Question 3?
library(nlme)# Fit random-intercept AND random-slope model for time (month)lme2 <-lme( cholesterol ~ trt_f + month + trt_f * month,random =~1+ month | id,data = chol_long,na.action = na.omit)# 1. Coefficient table for the random-intercept + random-slope modelcoef_table_rs <-as.data.frame(summary(lme2)$tTable)coef_table_rs$Parameter <-c("Intercept","Treatment","Time","Treatment × Time Interaction")coef_table_rs <- coef_table_rs[, c("Parameter", "Value", "Std.Error", "p-value")]knitr::kable( coef_table_rs,digits =4,row.names =FALSE,col.names =c("Parameter", "Estimate", "Std. Error", "p-value"),caption ="Random-Intercept and Random-Slope Model Fixed Effects")
Random-Intercept and Random-Slope Model Fixed Effects
Parameter
Estimate
Std. Error
p-value
Intercept
236.1655
6.8311
0.0000
Treatment
-3.6717
8.8113
0.6778
Time
0.9505
0.2377
0.0001
Treatment × Time Interaction
0.2139
0.3144
0.4967
# 2. AIC comparison between Question 2 (random intercept only) and Question 3 (random intercept + slope)aic_comp <-AIC(lme1, lme2)aic_comp$Model <-c("Question 2: Random Intercept Only", "Question 3: Random Intercept + Slope")aic_comp <- aic_comp[, c("Model", "df", "AIC")]knitr::kable( aic_comp,digits =2,row.names =FALSE,caption ="AIC Comparison Between Models")
AIC Comparison Between Models
Model
df
AIC
Question 2: Random Intercept Only
6
4360.36
Question 3: Random Intercept + Slope
8
4359.45
AIC for question 2 is 4360 while AIC for question 3 is 4359. The model from question 3 is better because it has a lower AIC. A lower AIC indicates better balance between goodness of fit of the model and model complexity.
Question 4
Using the better-fitting model, state whether the treatment groups have different trends in cholesterol over time, and support your answer with an estimate, a 95% confidence interval, and a p-value. Report the mean intercept and slope for each treatment group and explain what they mean in the units of the data.
Answer:
The better fitting model (question 3) had an interaction estimate of 0.2139mg/dL per month and the p-value for the interaction term of 0.4967 which is greater than 0.05 and a 95% confidence interval of (-0.414, 0.832) which crosses 0, thus there is no evidence that the treatment groups have different trends in cholesterol over time.
Placebo Mean Intercept (\(236.17\text{ mg/dL}\)): At baseline (Month \(0\)), patients in the Placebo group have an estimated mean serum cholesterol level of \(236.17\text{ mg/dL}\).
Placebo Mean Slope (\(+0.95\text{ mg/dL per month}\)): On average, serum cholesterol in the Placebo group increases by \(0.95\text{ mg/dL}\) for each additional month of follow-up, which was statistically significant (\(p = 0.0001\)).
Treatment Mean Intercept (236.17-3.6717=\(232.49\text{ mg/dL}\)): This means athat at at baseline (Month \(0\)), patients in the High Dose group have an estimated mean serum cholesterol level of \(232.49\text{ mg/dL}\) (\(3.67\text{ mg/dL}\) lower than Placebo, although this was not statistically significant at baseline, \(p = 0.6778\)).
High Dose Mean Slope (0.9505+0.2139=\(+1.16\text{ mg/dL per month}\)): On average, serum cholesterol in the High Dose group increases by \(1.16\text{ mg/dL}\) for each additional month of follow-up, which is \(0.21\text{ mg/dL per month}\) steeper than the Placebo slope, though this difference was not statistically significant (\(p = 0.4967\)).
Question 5
Using the better-fitting model, report the subject-specific intercept and slope (if the model has random slopes) for patients id = 1 and id = 2. How do they compare with the mean intercept and slope of their treatment group?
library(nlme)# Total subject-specific intercepts and slopes (fixed + random effects)subj_coefs <-coef(lme2)# View rows for id = 1 and id = 2subj_coefs[c("1", "2"), ]
# Check which treatment group id = 1 and id = 2 belong tounique(chol_long[chol_long$id %in%c(1, 2), c("id", "trt_f")])
id trt_f
1.0 1 High Dose
2.0 2 High Dose
#subject specific intercept#subject 1227.6750-3.671683
[1] 224.0033
#subject 2258.7577-3.671683
[1] 255.086
#subject specific slope#subject 11.414059+0.2138852
[1] 1.627944
#subject 21.251444+0.2138852
[1] 1.465329
Answer:
Subject 1:
subject specific intercept: 224.00
subject specific slope: 1.63
Subject 2:
subject specific intercept: 255.09
subject specific slope: 1.47
Overall:
High dose mean intercept: 232.49
High dose mean slope: 1.16
Compared with the overall means, patient ID 1 has a specific intercept lower than the high dose mean intercept (224.0 vs 232.49) and the cholesterol rises 0.47 mg/dL (1.63-1.16) faster than the high dose group average.
Compared with the overall means, patient ID 2 has a higher specific intercept than the high dose mean intercept (255.09 vs 232.49) and the cholesterol rises 0.31 mg/dL (1.47-1.16) faster than the high dose group average.
Sources
Gemini was used to help with troubleshooting of the R codes for this assignment.