I have three trials, want to know if the order of trial affected the outcome. I was checking my work with ChatGPT and noticed a discrepancy between their results and mine. This is the dummy code from ChatGPT:
# Load necessary libraries
library(lme4)
library(lmerTest)
library(emmeans)
# Define synthetic dataset with explicit scores for each trial
set.seed(123) # Set seed for reproducibility
# Scores for 20 subjects, each completing 3 trials
scores <- c(80, 85, 78, # Subject 1
75, 80, 82, # Subject 2
84, 79, 81, # Subject 3
77, 82, 79, # Subject 4
80, 83, 85, # Subject 5
79, 78, 76, # Subject 6
82, 84, 79, # Subject 7
81, 80, 82, # Subject 8
78, 80, 84, # Subject 9
82, 81, 83, # Subject 10
79, 78, 77, # Subject 11
84, 87, 89, # Subject 12
83, 85, 84, # Subject 13
82, 81, 83, # Subject 14
80, 79, 82, # Subject 15
78, 80, 81, # Subject 16
85, 88, 87, # Subject 17
79, 82, 80, # Subject 18
81, 82, 85, # Subject 19
84, 85, 86) # Subject 20
# Create the dataset
data <- data.frame(
Subject = rep(1:20, each = 3), # 20 subjects
Trial = rep(1:3, times = 20), # Three trials
Score = scores # Defined scores
)
# Treat Trial as a numeric variable
data$Trial <- as.numeric(data$Trial)
# Fit the linear mixed-effects model treating Trial as numeric
model_continuous <- lmer(Score ~ Trial + (1 | Subject), data = data)
# Model summary
summary(model_continuous)
I am getting a different output. I think it has something to do with lmerMod and lmerTest, and I don't know how to replicate their results.
Mine:
Linear mixed model fit by REML. t-tests use Satterthwaites method ['lmerModLmerTest']
Formula: Score ~ Trial + (1 | Subject)
Data: data
REML criterion at convergence: 282.2
Scaled residuals:
Min 1Q Median 3Q Max
-1.93939 -0.48327 0.08381 0.51236 1.94184
......
Fixed effects:
Estimate Std. Error df t value Pr(>|t|)
(Intercept) 80.0833 0.8456 54.7267 94.707 <2e-16 ***
Trial 0.7500 0.3157 39.0000 2.376 0.0225 *
ChatGPT:
Linear mixed model fit by REML ['lmerMod']
Formula: Score ~ Trial + (1 | Subject)
Data: data
REML criterion at convergence: 104.7192
Scaled residuals:
Min 1Q Median 3Q Max
-2.1934 -0.5690 -0.0162 0.5091 2.3361
.....
Fixed effects:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 80.1000 0.7123 112.48 <2e-16 ***
Trial 2.0000 0.6232 3.21 0.0056 **
From R Documentation:
In cases when a valid lmer-object (lmerMod) is produced, but when the computations needed for Satterthwaite df fails, the lmerMod object is returned - not an lmerModLmerTest object.
What does this mean? Which is correct? How do I know which to use? How do I switch between lmerMod and lmerModLmerTest if I need to?
lmerTest
package provides tools for inference, and the results for p-values etc. might differ depending on the particular assumptions used. What you primarily have, however, is a big discrepancy between theTrial
fixed effect coefficients from the two summaries (0.75 versus 2.0). That suggests a difference in the data presented to the two systems or some other fundamental problem in implementation. $\endgroup$data$Trial <- as.numeric(data$Trial)
which is redundant anyway. $\endgroup$lme4::lmer
andlmerTest
is the degrees of freedom approximation, model coefficients themselves should remain unchanged. I asked ChatGPT the same; it gave three different results across three sessions. This seems like a good lesson about relying on spicy autocomplete for statistical analysis (it is very certainly not actually executing this code). $\endgroup$