简体   繁体   中英

Calculate emmeans using multiple values of the continuous predictor

This question relates to Emmeans continuous independant variable

I want to calculate EMM for at least three values of diameter , ie, min, mean, and max, with a one-liner. Specifying cov.reduce = range gives the estimates using min and max diameter only, removing cov.reduce = range gives the estimates using the mean 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

Combining cov.reduce = c(range, mean) gives the estimates on the mean diameter only.

> 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 

Specifying the numbers (not just the values within the range, but the actual min, mean, and max values) gives an error.

> 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

I know I can run two lines of code and then combine the outputs, but I want to know if there is a one-liner solution. Thank you.

This is easily done, since you can specify any function. So try

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

This puts the median instead of the mean, but you can write a function that returns whatever you want. It can either be an inline function as shown above, or the name of a separate function.

BTW, for specific values, use at rather than cov.reduce . For example,

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

See the documentation for ref_grid() for details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM