使用连续预测变量的多个值计算 emmeans

Calculate emmeans using multiple values of the continuous predictor

此问题涉及

我想为 diameter 的至少三个值(即最小值、平均值和最大值)计算 EMM,并使用一行代码。指定 cov.reduce = range 仅使用最小值和最大值 diameter 给出估计值,删除 cov.reduce = range 给出使用平均值 diameter.

的估计值
mod = glm(log(strength) ~ machine + diameter, data = fiber)
emmeans(mod, ~machine*diameter, cov.reduce = range)
machine diameter emmean     SE  df asymp.LCL asymp.UCL
 A             15   3.48 0.0315 Inf      3.42      3.54
 B             15   3.50 0.0333 Inf      3.44      3.57
 C             15   3.43 0.0232 Inf      3.39      3.48
 A             32   3.88 0.0243 Inf      3.83      3.93
 B             32   3.90 0.0228 Inf      3.86      3.95
 C             32   3.83 0.0329 Inf      3.77      3.90

组合 cov.reduce = c(range, mean) 仅给出平均值 diameter 的估计值。

> emmeans(mod, ~machine*diameter, cov.reduce = c(range, mean))
 machine diameter emmean     SE  df asymp.LCL asymp.UCL
 A           24.1   3.69 0.0167 Inf      3.66      3.73
 B           24.1   3.72 0.0172 Inf      3.69      3.75
 C           24.1   3.65 0.0182 Inf      3.61      3.68

Results are given on the log (not the response) scale. 
Confidence level used: 0.95 

指定数字(不仅是范围内的值,还有实际的最小值、平均值和最大值)会出错。

> emmeans(mod, ~machine*diameter, cov.reduce = c(1, 15, 32))
Error in fix.cr(cov.reduce) : Invalid 'cov.reduce' argument

> emmeans(mod, ~machine*diameter, cov.reduce = c( 15, 24, 32))
Error in fix.cr(cov.reduce) : Invalid 'cov.reduce' argument

我知道我可以运行两行代码然后合并输出,但我想知道是否有一个线性解决方案。谢谢。

这很容易完成,因为您可以指定任何函数。所以试试

emmeans(..., cov.reduce = function(x) quantile(x, c(0, 0.5, 1)))

这里用的是中位数而不是均值,但是你可以写一个 returns 任何你想要的函数。它可以是如上所示的内联函数,也可以是单独函数的名称。

顺便说一句,对于特定值,请使用 at 而不是 cov.reduce。例如,

emmeans(..., at = list(diameter = c(15, 24, 32)))

有关详细信息,请参阅 ref_grid() 的文档。