gpt4 book ai didi

r - gls 模型的 `emmeans` 没有 't run inside ` map `

转载 作者:行者123 更新时间:2023-12-02 16:21:41 29 4
gpt4 key购买 nike

这个问题的灵感来自 can't use emmeans inside map , 并与 Map `joint_tests` to a list after fitting a `gls` model 有关和 `group_by` and keep grouping levels as nested data frame's name

我想将多个测试打包到一个工作流中。

这适用于 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() 这一步停止了。最终,我希望在 mutate 步骤中使用 joint_testsemmeanscontrast

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)))

我该如何解决这个问题?谢谢。

更新:Ronak 的答案中的 map2 函数解决了 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))) 没有解决问题。

最佳答案

使用 map2emmeans 步骤中传递数据和模型。对于 contrastsjoint_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 ×…

关于r - gls 模型的 `emmeans` 没有 't run inside ` map `,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65347058/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com