r - gls模型的`emmeans`不在`map`中运行
问题描述
这个问题的灵感来自can't use emmeans inside map,并与在拟合“gls”模型和“group_by ”后将“joint_tests”映射到列表并将分组级别保持为嵌套数据框的名称
我想将几个测试包装到一个工作流程中。
这适用于glm
模型。
library(dplyr)
library(purrr)
library(emmeans)
library(nlme)
diamond_result <- diamonds %>%
group_by(cut) %>%
nest() %>%
ungroup %>%
dplyr::mutate(models=map(data,~glm(price ~ x + y + z + clarity + color,data=.x)),
jt = map(models, ~emmeans::joint_tests(.x, data = .x$data)),
means=map(models,~emmeans::emmeans(.x,"color",data=.x$data)),
p_cont = map(means, ~emmeans::contrast(.x, "pairwise",infer = c(T,T))),
across(models:p_cont, stats::setNames, .$cut))
> diamond_result$jt
$Ideal
model term df1 df2 F.ratio p.value
x 1 Inf 611.626 <.0001
y 1 Inf 2.914 0.0878
z 1 Inf 100.457 <.0001
clarity 7 Inf 800.852 <.0001
color 6 Inf 256.796 <.0001
$Premium
model term df1 df2 F.ratio p.value
x 1 Inf 2074.371 <.0001
但是相同的语法不适用于gls
模型,所以我停在了emmeans()
一步。最终,我想要joint_tests
,emmeans
和contrast
在这mutate
一步。
diamonds_emm2 <- diamonds %>%
group_by(cut) %>%
nest() %>%
ungroup() %>%
dplyr::mutate(models=map(data,~gls(price ~ x + y + z + clarity,
weights = varIdent(form = ~ 1|color),
data =.x)),
means=map(models,~emmeans::emmeans(.x,"clarity",data=.x$data)),
across(models:p_cont, setNames, .$cut))
Error: Problem with `mutate()` input `means`.
x undefined columns selected
ℹ Input `means` is `map(models, ~emmeans::emmeans(.x, "clarity", data = .x$data))`.
Run `rlang::last_error()` to see where the error occurred.
<error/dplyr:::mutate_error>
Problem with `mutate()` input `means`.
x undefined columns selected
ℹ Input `means` is `map(models, ~emmeans::emmeans(.x, "clarity", data = .x$data))`.
Backtrace:
1. `%>%`(...)
18. base::.handleSimpleError(...)
19. dplyr:::h(simpleError(msg, call))
<error/dplyr:::mutate_error>
Problem with `mutate()` input `means`.
x undefined columns selected
ℹ Input `means` is `map(models, ~emmeans::emmeans(.x, "clarity", data = .x$data))`.
Backtrace:
█
1. ├─`%>%`(...)
2. ├─dplyr::mutate(...)
3. ├─dplyr:::mutate.data.frame(...)
4. │ └─dplyr:::mutate_cols(.data, ...)
5. │ ├─base::withCallingHandlers(...)
6. │ └─mask$eval_all_mutate(dots[[i]])
7. ├─purrr::map(models, ~emmeans::emmeans(.x, "clarity", data = .x$data))
8. │ └─.f(.x[[i]], ...)
9. │ └─emmeans::emmeans(.x, "clarity", data = .x$data)
10. │ ├─base::do.call(ref_grid, args)
11. │ └─(function (object, at, cov.reduce = mean, cov.keep = get_emm_option("cov.keep"), ...
12. │ ├─emmeans::recover_data(object, data = as.data.frame(data), ...)
13. │ └─emmeans:::recover_data.gls(...)
14. │ └─emmeans:::recover_data.call(...)
15. │ ├─tbl[, vars, drop = FALSE]
16. │ └─base::`[.data.frame`(tbl, , vars, drop = FALSE)
17. │ └─base::stop("undefined columns selected")
18. └─base::.handleSimpleError(...)
19. └─dplyr:::h(simpleError(msg, call))
代码在这一步运行正常。
diamonds_emm <- diamonds %>%
group_by(cut) %>% nest() %>%
mutate(models=map(data,~gls(price ~ x + y + z + clarity,
weights = varIdent(form = ~ 1|color),
data =.x)))
我该如何解决这个问题?谢谢你。
更新:map2
Ronak 回答的函数在means
步骤中解决了问题,但它不会进行成对对比。我错过了什么?
diamonds %>%
group_by(cut) %>%
nest() %>%
mutate(models=map(data,~gls(price ~ x + y + z + clarity,
weights = varIdent(form = ~ 1|color),
data =.x)),
means = map2(data, models,~emmeans::emmeans(.y,"clarity",data=.x)),
p_cont = map2(means, ~emmeans::contrast(.y, "pairwise",infer = c(T,T)))) %>%
ungroup %>%
mutate(across(models:p_cont, setNames, .$cut)) -> result
Error: Problem with `mutate()` input `p_cont`.
x object '.z' not found
ℹ Input `p_cont` is `map(means, ~emmeans::contrast(.y, "pairwise", infer = c(T, T)))`.
ℹ The error occurred in group 1: cut = "Fair".
在步骤中为输入指定一个新名称p_cont
,例如 ~emmeans::contrast(.z, "pairwise", infer = c(T, T)))
没有解决问题。
解决方案
emmeans
在步骤中使用传递数据和模型map2
。对于contrasts
并且joint_tests
您可以使用map
.
library(tidyverse)
library(emmeans)
library(nlme)
diamonds %>%
group_by(cut) %>%
nest() %>%
mutate(models=map(data,~gls(price ~ x + y + z + clarity,
weights = varIdent(form = ~ 1|color),
data =.x))) %>%
ungroup %>%
mutate(means = map2(data, models,~emmeans(.y,"clarity",data=.x)),
p_cont = map(means, contrast, "pairwise"),
joint_tests = map(means, joint_tests),
across(models:joint_tests, setNames, .$cut)) -> result
result
# cut data models means p_cont joint_tests
# <ord> <list> <named lis> <named lis> <named li> <named list>
#1 Ideal <tibble [21,551 × … <gls> <emmGrid> <emmGrid> <summary_emm[,5] [1 ×…
#2 Premium <tibble [13,791 × … <gls> <emmGrid> <emmGrid> <summary_emm[,5] [1 ×…
#3 Good <tibble [4,906 × 9… <gls> <emmGrid> <emmGrid> <summary_emm[,5] [1 ×…
#4 Very Good <tibble [12,082 × … <gls> <emmGrid> <emmGrid> <summary_emm[,5] [1 ×…
#5 Fair <tibble [1,610 × 9… <gls> <emmGrid> <emmGrid> <summary_emm[,5] [1 ×…
推荐阅读
- mysql - 书架关联错误
- python - 将 pandas 数据框中的列相加会导致分类索引错误
- hadoop - 如何在没有 CDH 的情况下安装和配置 Hue 来管理 HDFS
- vue.js - vue 将数据挂载到多个 div 或 table
- ruby - double splat 在方法调用中的作用是什么?
- python - Selenium:当我打印()时单击()不起作用,当我注释掉打印()时调用错误
- angular - Angular 2 应用程序可在工作计算机上运行,而家用计算机则无法正常运行
- python-3.x - 发送消息并嵌入 1 个命令 (Discord.py)
- object - TensorFlow 对象检测 API 最低分数阈值
- python - 如何将字符串数据保存到 TFRecord?