I'm using emmeans
to get adjusted means with a log transformation.
But unexpectedly, when I try to call this with a custom function, the behaviour is quite different with no explicit warning (except the one about the log transformation).
Here is a reproducible example:
db = mtcars %>% mutate(cyl=factor(cyl))
m = lm(log(mpg) ~ log(disp) + cyl, data = db)
print(m$call)
emm = emmeans(m, spec = "cyl", type = "response")
as.data.frame(emm)
f = function(formula){
m = lm(formula, data = db)
print(m$call)
emm = emmeans(m, spec = "cyl", type = "response")
as.data.frame(emm)
}
f(log(mpg) ~ log(disp) + cyl)
Here, the only difference between the models inside and outside the function is the call
object (tested with all.equal()
). They give exact same results otherwise.
This code is not mine and I'm only trying to automatize it, assuming the "outside function" output is correct.
Why is the output different? How can I automatize an emmeans
call?
As described here, this can also be done using do.call
.
db = dplyr::mutate(mtcars, cyl=factor(cyl))
f <- function(formula){
m = do.call(lm, list(formula = formula, data = db))
print(m$call$formula)
emm = emmeans::emmeans(m, spec = "cyl", type = "response")
as.data.frame(emm)}
f(log(mpg) ~ log(disp) + cyl)
#> log(mpg) ~ log(disp) + cyl
#> cyl response SE df lower.CL upper.CL
#> 1 4 18.59940 1.847377 28 15.17535 22.79603
#> 2 6 17.71003 1.011478 28 15.75472 19.90802
#> 3 8 17.71054 1.020718 28 15.73839 19.92981
Created on 2021-07-08 by the reprex package (v0.3.0)