Skip to content

Instantly share code, notes, and snippets.

@gregorp
Last active January 19, 2018 19:41
Show Gist options
  • Save gregorp/9dc9f07a86e98b3c0c8a2dce94554b5b to your computer and use it in GitHub Desktop.
Save gregorp/9dc9f07a86e98b3c0c8a2dce94554b5b to your computer and use it in GitHub Desktop.
Investigating alleged differences in `lmer` results depending on whether the input is in a list or not
library(lmer)
library(lmerTest)
# Comparing the `lmer` output on identical data frames, one in a list, one not.
# This example uses data built in to the `lme4` package, and the model in the `?lmer` help examples.
## split data into two groups by subject
subject_sample = sample(unique(sleepstudy$Subject), size = length(unique(sleepstudy$Subject)) / 2)
sleep_split = split(sleepstudy, f = sleepstudy$Subject %in% subject_sample)
sleep_1 = subset(sleepstudy, !Subject %in% subject_sample)
# make sure groups are the same - first list item is the same as sleep_1 data frame
identical(sleep_1, sleep_split[[1]])
# [1] TRUE
# create models
## list
results = list()
for (i in seq_along(sleep_split)) {
results[[i]] = lmer(Reaction ~ Days + (Days | Subject), sleep_split[[i]])
}
## not list
mod_1 = lmer(Reaction ~ Days + (Days | Subject), sleep_1)
# compute summary and anova test
## list
summ_list = lapply(results, summary)
anova_list = lapply(results, lmerTest::anova)
## not list
summ_1 = summary(mod_1)
anova_1 = lmerTest::anova(mod_1)
# Comparisons #####
all.equal(summ_1, summ_list[[1]])
# [1] "Component “call”: target, current do not match when deparsed"
## only the call is different, otherwise the summaries are identical
identical(anova_1, anova_list[[1]])
# [1] TRUE
## anova results are identical
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment