locus
locus

Reputation: 431

test significance between models with emmeans

Let's say I have these two models

dat1 <- data.frame(x=factor(c(1,2,1,1,2,2)),y=c(2,5,2,1,7,9))
dat2 <- data.frame(x=factor(c(1,2,1,1,2,2)),y=c(3,3,4,3,4,2))

mod1 <- lm(y~x,data=dat1)
mod2 <- lm(y~x, data=dat2)

and calculate a t test between the levels of x in each model

t1 <- pairs(emmeans(mod1, ~x))
t2 <- pairs(emmeans(mod2, ~x))

How can I assess whether the two models are significantly different for this contrast using emmeans?

Upvotes: 0

Views: 206

Answers (1)

Russ Lenth
Russ Lenth

Reputation: 6810

dat1$dataset <- "dat1"
dat2$dataset <- "dat2"
alldat <- rbind(dat1, dat2)
modsame <- lm(y ~ x, data = alldat)
moddiff <- lm(y ~ x * dataset, data = alldat)
anova(modsame, moddiff)

Don't try to use emmeans() to do this; that isn't its purpose. The anova() call above compares the two models: modsame presumes that the x effects are the same in each dataset; moddiff adds two terms, dataset which accounts for the change in overall mean, and x:dataset which accounts for the change in x effects.

The comparison between the two models comprises a joint test of both the dataset and the x:dataset effects -- it is an F test with 2 numerator d.f. -- not a t test.

Upvotes: 1

Related Questions