Zhigang L.
asked 08/15/22How to extract coefficients corresponding to data from each time in a mixed effect model in R?
I am not good at statistics and might have a naive question. Any help is appreciated.
Here is the simplified question: Patients received two different vaccines twice and we collected their blood after each vaccination to measure antibody levels in the blood. There are other variables but here I omitted them and just want to know how different vaccines affect the antibody levels. It seems that the longitudinal data mixed effect model is a proper analysis.
Here is the simulated data. 6 patients received Vaccines A or B for the first vaccine and all received Vaccine B as the second vaccine; after each vaccination they visited a hospital to measure blood antibody levels (2 data points at 2 Visits). It is already confirmed with studies that Vaccine A induced higher antibody levels than Vaccine B. So if using only Visit 1 data to do a linear regression, we can see Vaccine A induced significantly higher antibody levels compared to that with Vaccine B. As all patients received A as the second vaccine, if using only Visit 2 data, there is no significance. My question is, if we include all the data in a linear mixed effect model, how to extract coefficients corresponding to each visit data? I only know how to use the summary() to get the coefficients for both visits data, however, the significance for the Vaccine type doesn't seem to be right.
Linear regression of Visit 1 data confirmed that Vaccine A induced higher antibody levels.
Linear regression of Visit 2 data indicated no significance as everyone got Vaccine A.
If using mixed model with both visits data, the vaccine variable is significant. Is it possible to extract coefficients corresponding to each visit? Just to show similar results to linear regression that Visit 1 showed significance but not Visit 2.
Originally, I performed two linear regression with each visit data. But I was told that for visit 2 data I should use linear mixed model as I got both visit data to account for repeated measurement within patient differences. Other covariates are not included in this simulated model just for simplicity.
Many thanks,
Jordan
1 Expert Answer
Nicole G. answered 01/19/26
PhD Candidate in Molecular Biosciences with 7+ Years of Teaching Exp.
You cannot estimate an A vs B effect at Visit 2 if nobody received B at Visit 2. The model can show a significant vaccine effect at Visit 1 but not at Visit 2.
In your current model, the vaccine effect is averaged across both visits, which is why it appears significant even though the difference only occurs at Visit 1. To get visit-specific results, you must include Visit in the model and allow the vaccine effect to vary by visit (for example, with a Vaccine × Visit interaction). Without this, there are no visit-specific coefficients to extract. With the interaction included, the model shows a vaccine difference at Visit 1 and no estimable difference at Visit 2 because all subjects received the same vaccine.
Visit is the part that allows the vaccine effect to differ by visit. Without it, the model only estimates a single, overall vaccine effect averaged across visits, and there are no visit-specific coefficients to extract.
# Fit mixed-effects model with Vaccine × Visit interaction
m_int <- lmer(Antibody ~ Vaccine * Visit + (1 | ID), data = data_raw)
Overall script:
# Load required packages
library(lme4)
library(lmerTest) # for p-values in summary()
library(emmeans) # for visit-specific comparisons
# Create the data
data_raw <- data.frame(
ID = c(1,2,3,4,5,6,1,2,3,4,5,6),
Antibody = c(50,60,70,30,40,35,101,102,102,102,101,103),
Visit = c(1,1,1,1,1,1,2,2,2,2,2,2),
Vaccine = c("A","A","A","B","B","B","A","A","A","A","A","A"),
VaccineChange = c(0,0,0,0,0,0,0,0,0,1,1,1)
)
# Convert Visit to a factor so R treats it as categorical
data_raw$Visit <- factor(data_raw$Visit)
# Fit mixed-effects model with Vaccine × Visit interaction
m_int <- lmer(Antibody ~ Vaccine * Visit + (1 | ID), data = data_raw)
# View model summary (fixed effects + random effects)
summary(m_int)
# Get vaccine comparisons separately for each visit
emmeans(m_int, pairwise ~ Vaccine | Visit)
# Optional: extract fixed-effect coefficients directly
fixef(m_int)
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Karl M.
If possible, I'd suggest adding an R tag in the topics of this question to get it in front of more eyes. Best of luck!08/15/22