admin管理员组文章数量:1123433
I often use the nest() function in dplyr to perform models on nested tibbles. For example, testing within each group if the value changes over time:
library(dplyr)
library(lme4)
set.seed(123)
data <- tibble(
ID = rep(1:50, each = 2),
time = rep(c(0, 1), times = 50),
group = rep(sample(c("A", "B", "C"), 50, replace = TRUE), each = 2),
value = runif(100)
)
data %>%
group_by(group) %>%
nest() %>%
mutate(lmm = map(data, \(x) lme4::lmer(value ~ time + (1|ID),
data = x)))
Now, I want to compare how groups differ over time by excluding one group at a time. Specifically, I want to fit the model value ~ time * group + (1 | ID)
for all combinations of two groups.
Is it possible to nest the data such that each row in the nested tibble represents combinations of two groups? How can this be achieved efficiently in dplyr?
I often use the nest() function in dplyr to perform models on nested tibbles. For example, testing within each group if the value changes over time:
library(dplyr)
library(lme4)
set.seed(123)
data <- tibble(
ID = rep(1:50, each = 2),
time = rep(c(0, 1), times = 50),
group = rep(sample(c("A", "B", "C"), 50, replace = TRUE), each = 2),
value = runif(100)
)
data %>%
group_by(group) %>%
nest() %>%
mutate(lmm = map(data, \(x) lme4::lmer(value ~ time + (1|ID),
data = x)))
Now, I want to compare how groups differ over time by excluding one group at a time. Specifically, I want to fit the model value ~ time * group + (1 | ID)
for all combinations of two groups.
Is it possible to nest the data such that each row in the nested tibble represents combinations of two groups? How can this be achieved efficiently in dplyr?
Share Improve this question edited 15 hours ago ThomasIsCoding 99.9k9 gold badges34 silver badges99 bronze badges asked 15 hours ago user23485480user23485480 474 bronze badges 3 |1 Answer
Reset to default 1If I understand correctly, you could use combn
to create a tibble with pairwise combinations, then map
it:
mdl_results <- tibble(groups = combn(unique(data$group), 2, simplify = FALSE)) %>%
mutate(
lmm = purrr::map(groups, ~ lme4::lmer(value ~ time * group + (1 | ID),
data = filter(data, group %in% .x))),
summary = purrr::map(lmm, summary)
)
# groups lmm summary
# <list> <list> <list>
# 1 <chr [2]> <lmerMod> <smmry.mM>
# 2 <chr [2]> <lmerMod> <smmry.mM>
# 3 <chr [2]> <lmerMod> <smmry.mM>
Though this is just programmatic - I agree with the comment that you may want to ask the statistical validity on Cross Validated. Good luck!
本文标签: rHow to Nest Data for Pairwise Group Comparisons in dplyrStack Overflow
版权声明:本文标题:r - How to Nest Data for Pairwise Group Comparisons in dplyr? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736568390a1944736.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
nest()
is a function intidyr
and not indplyr
. (Perhaps you loaded both withtidyverse
?) – Jon Spring Commented 10 hours agom <- lmer(value ~ time * group + (1 | ID), data = data)
, thenemmeans::emtrends(m, pairwise ~ group, 'time')
. That should give you the pairwise difference in slopes between groups. – Axeman Commented 8 hours ago