r语言 - 如何使用转换后的参考网格从嵌套模型?



我一直在尝试使用对数变换的参考网格来获得与emmeans的两两平均比率(以下是对先前问题的建议解决方案)。

然而,我有一个嵌套模型,我无法弄清楚如何让函数confintpairs在从嵌套模型创建的对数转换参考网格上工作。下面是使用emmeans杂乱数据插图中的嵌套示例的示例:

cows <- data.frame (
route = factor(rep(c("injection", "oral"), c(5, 9))),
drug = factor(rep(c("Bovineumab", "Charloisazepam", 
"Angustatin", "Herefordmycin", "Mollycoddle"), c(3,2,  4,2,3))),
resp = c(34, 35, 34,   44, 43,      36, 33, 36, 32,   26, 25,   25, 24, 24)
)
cows.lm <- lm(resp ~ route + drug, data = cows)
cows.lrg <- ref_grid(cows.lm, transform="log")
#NOTE: A nesting structure was detected in the fitted model:
#    drug %in% route
confint(cows.lrg, type="response")
#Error in object@linfct[use.elts, , drop = FALSE] : 
#  (subscript) logical subscript too long
pairs(cows.lrg, type = "response", infer = c(TRUE, TRUE), adjust = "none")
#Error in x@linfct[i, , drop = FALSE] : subscript out of bounds

我做错了什么?

好的,我发现regrid()函数有一个bug。如果你甚至做summary(cows.lrg),你会得到一个错误。问题是嵌套结构可能涉及一些"幽灵"。参考网格中未被regrid()保存的行。这里有一种绕过它的方法,但它并不漂亮:

cows.lrg@linfct = matrix(0, nrow = 10, ncol = 5)
cows.lrg@linfct[cows.rg@misc$display, ] = diag(5)
cows.lrg@misc$display = as.logical(cows.lrg@grid$.wgt.)

现在我们有

> summary(cows.lrg)
route     drug           prediction SE df
oral      Angustatin           3.53 NA  9
injection Bovineumab           3.54 NA  9
injection Charloisazepam       3.77 NA  9
oral      Herefordmycin        3.24 NA  9
oral      Mollycoddle          3.19 NA  9
Results are given on the log (not the response) scale.

不幸的是,我们仍然没有有效的协方差值,使得SE都是NA

我将修复这个问题,并在GitHub存储库上更新emmeans。这可能需要一段时间;不幸的是,我今天早上刚刚上传了一个更新包到CRAN,根据墨菲的坏时机定律,没有解决这个问题。

最新更新