0
$\begingroup$

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?

$\endgroup$
5
  • $\begingroup$ The 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 the Trial 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$
    – EdM
    Commented Nov 19 at 20:46
  • 1
    $\begingroup$ I am using the dataset given to me by ChatGPT. I tested it on a different computer and got the same results which is different from the result from ChatGPT. My packages are a little more up to date, but none of those explain why my results are so different. The only thing I can think of is ChatGPT is just making up numbers for the example. $\endgroup$
    – CAA
    Commented Nov 19 at 21:11
  • $\begingroup$ Possibly didn't give you great advice either with data$Trial <- as.numeric(data$Trial) which is redundant anyway. $\endgroup$
    – dipetkov
    Commented Nov 19 at 21:16
  • 1
    $\begingroup$ Running this model in an actual R session reproduces your results. The only difference between defaults in lme4::lmer and lmerTest 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$
    – PBulls
    Commented Nov 19 at 21:26
  • 1
    $\begingroup$ I've seen a situation in which ChatGPT, when asked for scientific references on a topic, gave results that seemed plausible but ended up as fictional once you tried to find them. My guess is that you are correct: "ChatGPT is just making up numbers." For a while, at least, CrossValidated will continue to be more reliable than ChatGPT. $\endgroup$
    – EdM
    Commented Nov 19 at 21:41

1 Answer 1

5
$\begingroup$

ChatGPT is simply making up numbers.

lmerTest provides a wrapper around lme4 which provides denominator degrees-of-freedom computations and a few other helper functions. The actual fitting is done with exactly the same machinery in lme4 and lmerTest.

The only reason I can think of that you would need to revert from lmerTest to lme4 is that occasionally when something goes wrong with the fitting it can be a little harder to figure out what's going on with lmerTest (it hides some warning messages). In this case you could detach("package:lmerTest"); library("lme4"), but this isn't guaranteed to work perfectly — it would be safer to start a clean R session where you load lme4 instead of lmerTest.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.