Homework 3: Longitudinal Analysis

CTS 533: Advanced Topics in Biostatistics

Author

Sungshin Na

Link to published qmd file for this hw can also be found here:

http://rpubs.com/nasa22/1478462

Objectives

  1. Summarize and plot longitudinal data with missing measurements.
  2. Fit and interpret linear mixed effects models with a random intercept, and with a random intercept and slope.
  3. Compare models with AIC and test whether treatment groups differ in their trends over time.
  4. 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 models

chol <- read.csv("chol.csv")                      # read the data: one row per patient
chol$high_dose <- as.numeric(chol$trt == 1)       # indicator: 1 = high dose, 0 = placebo

chol_long <- reshape(chol,                                             # the wide data
                     direction = "long",                               # convert from wide to long
                     varying = list(c("y1", "y2", "y3", "y4", "y5")),  # the columns to stack
                     v.names = "cholesterol",                          # name of the outcome column
                     timevar = "month",                                # name of the time column
                     times = c(0, 6, 12, 20, 24),                      # the month for y1 to y5
                     idvar = "id")                                     # identifies each patient
chol_long <- chol_long[order(chol_long$id, chol_long$month), ]         # sort by patient, then month
chol_long <- chol_long[!is.na(chol_long$cholesterol), ]                # drop visits with no measurement
head(chol_long, 10)                                                    # show the first 10 rows
     id trt high_dose month cholesterol
1.0   1   1         1     0         178
1.6   1   1         1     6         246
1.12  1   1         1    12         295
1.20  1   1         1    20         228
1.24  1   1         1    24         274
2.0   2   1         1     0         254
2.6   2   1         1     6         260
2.12  2   1         1    12         278
2.20  2   1         1    20         245
2.24  2   1         1    24         340

Question 1

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.

Answer:

Year High dose (trt=1) Placebo (trt=2)
1

n=62.00

mean= 226.02

SD= 39.66

n=41.00

mean=235.93

SD= 55.87

2

n= 62.00

mean= 245.53

SD= 39.45

n= 41.00

mean= 243.17

SD= 49.24

3

n= 55.00

mean= 252.02

SD= 38.33

n= 38.00

mean= 244.76

SD= 46.11

4

n= 44.00

mean= 256.80

SD= 34.49

n= 35.00

mean= 257.60

SD= 51.14

5

n= 38.00

mean= 254.55

SD= 49.96

n= 31.00

mean= 257.48

SD= 49.39

#
visits <- c("y1", "y2", "y3", "y4", "y5")

lapply(visits, function(v) {
  aggregate(
    chol[[v]] ~ chol$trt,
    FUN = function(x) c(
      n = sum(!is.na(x)),
      mean = round(mean(x, na.rm = TRUE), 2),
      sd = round(sd(x, na.rm = TRUE), 2)
    )
  )
})
[[1]]
  chol$trt chol[[v]].n chol[[v]].mean chol[[v]].sd
1        1       62.00         226.02        39.66
2        2       41.00         235.93        55.87

[[2]]
  chol$trt chol[[v]].n chol[[v]].mean chol[[v]].sd
1        1       62.00         245.53        39.45
2        2       41.00         243.17        49.24

[[3]]
  chol$trt chol[[v]].n chol[[v]].mean chol[[v]].sd
1        1       55.00         252.02        38.33
2        2       38.00         244.76        46.11

[[4]]
  chol$trt chol[[v]].n chol[[v]].mean chol[[v]].sd
1        1       44.00         256.80        34.49
2        2       35.00         257.60        51.14

[[5]]
  chol$trt chol[[v]].n chol[[v]].mean chol[[v]].sd
1        1       38.00         254.55        49.96
2        2       31.00         257.48        49.39
#code for plot
# 1. Create chol_summary first
trt_f <- factor(chol_long$trt, levels = c(1, 2), labels = c("High Dose", "Placebo"))

n_tbl <- aggregate(
  chol_long$cholesterol,
  by = list(trt = trt_f, month = chol_long$month),
  FUN = function(x) sum(!is.na(x))
)
names(n_tbl)[3] <- "n"

mean_tbl <- aggregate(
  chol_long$cholesterol,
  by = list(trt = trt_f, month = chol_long$month),
  FUN = function(x) mean(x, na.rm = TRUE)
)
names(mean_tbl)[3] <- "mean"

sd_tbl <- aggregate(
  chol_long$cholesterol,
  by = list(trt = trt_f, month = chol_long$month),
  FUN = function(x) sd(x, na.rm = TRUE)
)
names(sd_tbl)[3] <- "sd"

chol_summary <- merge(merge(n_tbl, mean_tbl), sd_tbl)
chol_summary <- chol_summary[order(chol_summary$trt, chol_summary$month), ]

# 2. Now subset and plot
high_dose <- subset(chol_summary, trt == "High Dose")
placebo   <- subset(chol_summary, trt == "Placebo")

y_range <- range(chol_summary$mean, na.rm = TRUE)
y_pad   <- diff(y_range) * 0.08

plot(
  high_dose$month, high_dose$mean,
  type = "b",
  pch = 19,
  lwd = 2,
  col = "steelblue",
  xlim = range(chol_summary$month, na.rm = TRUE),
  ylim = c(y_range[1] - y_pad, y_range[2] + y_pad),
  xaxt = "n",
  xlab = "Time (Months)",
  ylab = "Mean Serum Cholesterol",
  main = "Mean Serum Cholesterol Over Time by Treatment Group"
)

axis(1, at = sort(unique(chol_summary$month)))

lines(placebo$month, placebo$mean, type = "b", pch = 17, lwd = 2, col = "firebrick")

legend(
  "topright",
  legend = c("High Dose", "Placebo"),
  col = c("steelblue", "firebrick"),
  pch = c(19, 17),
  lwd = 2,
  bty = "n"
)

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.

Answer:

Model equation:

\[ cholesterol_{ij} = \beta_0 + \beta_1\,\text{Trt}_i + \beta_2\,t_{ij} + \beta_3\,(\text{Trt}_i \times t_{ij}) + b_{i} + \varepsilon_{ij} \]

where \(b_{i}\) is the random intercept for patient \(i\) and \(\varepsilon_{ij}\) is the within-patient residual error.

library("nlme")

chol_long$trt_f <- factor(chol_long$trt, levels = c(2, 1), labels = c("Placebo", "High Dose"))

lme1 <- lme(
  cholesterol ~ trt_f + month + trt_f * month,
  random = ~ 1 | id,
  data = chol_long,
  na.action = na.omit
)

# Extract and format the coefficient table
coef_table <- as.data.frame(summary(lme1)$tTable)
coef_table$Parameter <- c(
  "Intercept",
  "Treatment",
  "Time",
  "Treatment × Time Interaction"
)

coef_table <- coef_table[, c("Parameter", "Value", "Std.Error", "p-value")]
knitr::kable(
  coef_table,
  digits = 4,
  row.names = FALSE,
  col.names = c("Parameter", "Estimate", "Std. Error", "p-value")
)
Parameter Estimate Std. Error p-value
Intercept 236.1231 6.5430 0.0000
Treatment -3.5082 8.4400 0.6785
Time 0.9589 0.2087 0.0000
Treatment × Time Interaction 0.1808 0.2772 0.5146

Question 3

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?

Answer:

\[ cholesterol_{ij} = \beta_0 + \beta_1\,\text{Trt}_i + \beta_2\,t_{ij} + \beta_3\,(\text{Trt}_i \times t_{ij}) + b_{0i} + b_{1i}\,t_{ij} + \varepsilon_{ij} \]

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 model
coef_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 = 2
subj_coefs[c("1", "2"), ]
  (Intercept) trt_fHigh Dose    month trt_fHigh Dose:month
1    227.6750      -3.671683 1.414059            0.2138852
2    258.7577      -3.671683 1.251444            0.2138852
# Check which treatment group id = 1 and id = 2 belong to
unique(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 1
227.6750-3.671683
[1] 224.0033
#subject 2
258.7577-3.671683
[1] 255.086
#subject specific slope
#subject 1
1.414059+ 0.2138852
[1] 1.627944
#subject 2
1.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.