在拟合 `gls` 模型后将 `joint_tests` 映射到列表

Map `joint_tests` to a list after fitting a `gls` model

我正在尝试使用以下代码从列表中获取具有 emmeans::joint_tests() 的类型 3 方差分析 table。我不完全理解错误信息。 辅导我的代码来自 http://pages.stat.wisc.edu/~yandell/R_for_data_sciences/curate/tidyverse.html

library(dplyr)
library(nlme)
library(emmeans)
data("diamonds")
diamonds %>%
  split(.$cut) %>%
  map(~ gls(price ~ x + y + z,  
  weights = varIdent(form = ~ 1|color),
  data = .))%>%
map(summary)

错误消息似乎建议我以某种方式保存我的个人模型,然后应用 joint_tests。我不明白的是为什么工作流程适用于 summary 但不适用于 joint_tests。当我们拟合单个模型时,summary(model)joint_tests(model) 分别打印摘要 table 或方差分析 table。

data("diamonds")
diamonds %>%
  split(.$cut) %>%
  map(~ gls(price ~ x + y + z,  
  weights = varIdent(form = ~ 1|color),
  data = .))%>%
map(joint_tests)

Error in (function (object, at, cov.reduce = mean, cov.keep = get_emm_option("cov.keep"), : Perhaps a 'data' or 'params' argument is needed

使用 map(~ joint_tests) 给出了这样一个奇怪的列表

$Fair
function (object, by = NULL, show0df = FALSE, cov.reduce = range, 
    ...) 
{
    if (!inherits(object, "emmGrid")) {
        args = .zap.args(object = object, cov.reduce = cov.reduce, 
            ..., omit = "submodel")
        object = do.call(ref_grid, args)
    }
    facs = setdiff(names(object@levels), by)
    do.test = function(these, facs, result, ...) {
        if ((k <- length(these)) > 0) {
            emm = emmeans(object, these, by = by, ...)
            tst = test(contrast(emm, interaction = "consec"), 
                joint = TRUE, status = TRUE)
            tst = cbind(ord = k, `model term` = paste(these, 
                collapse = ":"), tst)
            result = rbind(result, tst)
            last = max(match(these, facs))
        }
        else last = 0
        if (last < (n <- length(facs))) 
            for (i in last + seq_len(n - last)) result = do.test(c(these, 
                facs[i]), facs, result, ...)
        result
    }
    result = suppressMessages(do.test(character(0), facs, NULL, 
        ...))
    result = result[order(result[[1]]), -1, drop = FALSE]
    if (!show0df) 
        result = result[result$df1 > 0, , drop = FALSE]
    class(result) = c("summary_emm", "data.frame")
    attr(result, "estName") = "F.ratio"
    attr(result, "by.vars") = by
    if (any(result$note != "")) {
        msg = character(0)
        if (any(result$note %in% c(" d", " d e"))) 
            msg = .dep.msg
        if (any(result$note %in% c("   e", " d e"))) 
            msg = c(msg, .est.msg)
        attr(result, "mesg") = msg
    }
    else result$note = NULL
    result
}
<bytecode: 0x7ff68eb4a0a8>
<environment: namespace:emmeans>

$Good
function (object, by = NULL, show0df = FALSE, cov.reduce = range, 
    ...) 
{

这是我在没有列表的情况下joint_tests的做法。

diamond.gls <-  gls(price ~ x + y + z,  
  weights = varIdent(form = ~ 1|color),
  data = diamonds)
joint_tests(diamond.gls)
model term df1      df2  F.ratio p.value
 x            1 14311.72 4898.859 <.0001 
 y            1 12964.08   46.231 <.0001 
 z            1  8380.71   24.576 <.0001

请看我如何修复它。谢谢。

我们可以使用有效的示例设置数据集:

dat = droplevels(subset(diamonds,cut %in% c("Ideal","Premium","Good")))
dat$X = cut(dat$z,c(-0.5,4,11))
dat$clarity = factor(dat$clarity,ordered=FALSE)
dat$color = factor(dat$color,ordered=FALSE)
fit = gls(price ~ X*clarity, weights = varIdent(form = ~ 1|color),
data=subset(dat,cut=="Ideal"))
joint_tests(fit)

 model term df1      df2   F.ratio p.value
 X            1 15002.85 12145.835 <.0001 
 clarity      7 11834.99   351.899 <.0001 
 X:clarity    7 11834.99   352.344 <.0001 

所以这对一个子集来说工作正常,我们需要让它工作。您 运行 进入错误的原因是 joint_tests() 再次查看您的环境以查找 data.frame,而在 map() 函数内部这是不可能的。

一种直接的方法是使用 for 循环并将结果存储在列表中:

fits = list()

for(i in unique(dat$cut)){

f  = gls(price ~ X*clarity,  
                weights = varIdent(form = ~ 1|color),
                data = subset(dat,cut==i))
res = joint_tests(f)
fits[[i]] = list(f=f,res=res)
}

由于我将调查的原因,joint_tests() 在它是 gls 模型时需要 data 参数,至少在函数体内调用时需要。为了克服这个问题,我们需要编写一个适合模型并运行 joint_tests() 的函数。这是平行图:

mod_jt = function(dat) {
  mod = gls(breaks ~ tension, data = dat)
  joint_tests(mod, data = dat)
}

warpbreaks %>% split(.$wool) %>% map(mod_jt) 

...我们得到结果:

$A
 model term df1 df2 F.ratio p.value
 tension      2  24   7.288 0.0034 


$B
 model term df1 df2 F.ratio p.value
 tension      2  24   4.059 0.0303 

我认为你的代码将适用于 lm 模型,至少适用于最新版本的 emmeans*