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?