[R-sig-ME] Paired ttest using lme4

Lenth, Russell V ru@@e||-|enth @end|ng |rom u|ow@@edu
Tue Jun 18 17:16:55 CEST 2024


Ghader,

An odd thing about this solution is that the emmeans package is loaded, but not used. Assuming (I hope so) that you care to see the estimated means and then test the difference, I suggest:

    mod <- lmerTest::lmer(y ~ g + (1 | id), data = tt2)

    ( emm <- emmeans(mod, "g") )      # obtain the estimated means

    pairs(emm)                                      # compare them

For this example, this does NOT yield the same P value as the anova test, and that is because the anova test is clearly wrong! (It shows that there are 4 d.f., but there are only 3 subjects so there should be 2 d.f. as you will obtain using the code above.)

After testing, I found that the error is due to a singularity issue in fitting the model. That issue is due to the fact that the 'id' effect is estimated as zero, hence we are not really estimating a subject effect, and hence we in a sense don't really have a paired t test. If you include real subject variations in the data, e.g., 

    tt2$y = tt2$y + rep(c(3,-2,-1), 2)

and repeat the procedure, there will be no singularity issues in fitting the model and the anova and emmeans results will agree on 2 d.f.
--
Russ Lenth
U of Iowa

-----Original Message-----

Date: Mon, 17 Jun 2024 14:01:25 +0000
From: Ghader Mirzaghaderi <gh.mirzaghaderi using uok.ac.ir>
To: "r-sig-mixed-models using r-project.org"
	<r-sig-mixed-models using r-project.org>
Subject: [R-sig-ME] Paired ttest using lme4
Message-ID: <2094dcb0fef24f318e7b77049bc8bcd0 using uok.ac.ir>
Content-Type: text/plain; charset="utf-8"



Dear Ben,

I appreciate your letting me know how the paired t-test is done using lmer function.

using the following code, the p-value from lmer is different from the paired t.test.

Many thanks

Best regards,

Ghader


tt <- read.table(text = "
g y
1 2.39
1 2.59
1 2.44
2 4.34
2 2.89
2 3.77
", header = T)
t.test(tt$y ~ tt$g, var.equal = F, paired = T)


tt2 <- read.table(text = "
id  g y
a1 a 2.39
a2 a 2.59
a3 a 2.44
a1 b 4.34
a2 b 2.89
a3 b 3.77
", header = T)
library(lme4)
library(emmeans)
anova(lmerTest::lmer(y ~ g + (1 | id), data = tt2))



More information about the R-sig-mixed-models mailing list