- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这个问题的灵感来自 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_tests
、emmeans
和 contrast
。
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)))
没有解决问题。
最佳答案
使用 map2
在 emmeans
步骤中传递数据和模型。对于 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 ×…
关于r - gls 模型的 `emmeans` 没有 't run inside ` map `,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65347058/
下面是我遇到的问题的 MWE。我使用的是 nlme 包中的 Orthodont 数据集,该数据集包含 27 名 child (16 名女孩,11 名男孩)的 4 次测量结果。为了对相关性进行建模,我通
我正在尝试在 R 中拟合线性时间序列模型。我的第一种方法是使用 lm: > m1 = lm(logp~logg, data = data) > summary(m1) Call: lm(formula
我想知道是否有人知道如何限制 lm 中的某些系数或 gls要积极? 例如,我想要 lm(y ~ x1 + x2 + ... + xn)或 gls(y ~ x1 + x2 + ... + xn)我想要,
我需要从 R 中的 gls 输出中获取系数以及它们的 SE、t 值和 p 值。 library(nlme) fm1 cs cs Value Std.
我不断收到这样的错误: Error in `coef<-.corARMA`(`*tmp*`, value = c(18.3113452983211, -1.56626248550284, : C
我正在尝试让 google 端点正常工作。我创建了一个端点服务,并且能够通过 Google API exploer 测试它是否正常工作。然后我生成了 android 客户端并尝试调用我的端点服务。我在
有谁知道如何解决这个错误的问题: library(nlme) gls(Number.of.Fish~Julian.Date+Temperature+Size.Class,na.action=na.om
升级到 Android SDK 8 后,我开始在模拟器中看到地理编码失败。但是,在设备上一切正常。 现在我的应用无法在 1.5 和 1.6 设备以及模拟器中进行地址编码。有没有人能够解决这个问题?我无
这个问题的灵感来自 can't use emmeans inside map , 并与 Map `joint_tests` to a list after fitting a `gls` model
这个问题的灵感来自 can't use emmeans inside map , 并与 Map `joint_tests` to a list after fitting a `gls` model
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwar
我使用 nlme 的 gls 来拟合这个模型: require(nlme) set.seed(101) mp summary(m01) Generalized least squares fit b
我有 Could not find class `'com.google.android.gms.location.internal.ParcelableGeofence', referenced f
我试图通过 R 获得与 sas 相同的结果,但似乎存在一些困难。 例如R: contras:2-1 AVISITN = 6: estimate is -1.81 and SE is 1.59 libr
我是一名优秀的程序员,十分优秀!