0

Let's say I have defined a variable (var) which is the name of a predictor in the model

libary(emmeans)
var <- "disp"
lm.mod <- lm(mpg ~ disp + hp, data=mtcars)

When I try to use the variable var in the at argument of emmeans, it gets ignored

emmeans(lm.mod, var, at=list(var=c(100,200,300,400)))
 disp emmean    SE df lower.CL upper.CL
  231   20.1 0.553 29       19     21.2

using disp instead, I get the expected output:

emmeans(lm.mod, var, at=list(disp=c(100,200,300,400)))
 disp emmean    SE df lower.CL upper.CL
  100   24.1 1.115 29     21.8     26.3
  200   21.0 0.598 29     19.8     22.2
  300   18.0 0.754 29     16.4     19.5
  400   15.0 1.370 29     12.2     17.8

Isn't it possible to use variables in place of the actual predictor names in the at argument of the emmeans call?

locus
  • 387
  • 2
  • 9
  • 1
    Try `emmeans(lm.mod, var, at=setNames(list(c(100,200,300,400)), var))`. – Ritchie Sacramento Mar 14 '23 at 23:32
  • By the way, I'm trying variations of the `emmeans` call and I noticed that `var<-c("disp","hp")` `emmeans(lm.mod, list(var))` isn't the same as `emmeans(lm.mod, list("disp","hp"))` even though `emmeans` appears to understand that `var` contains two predictors. Is this to be expected? – locus Mar 14 '23 at 23:42
  • 1
    @locus - compare the output of `list(var)` and `list("disp","hp")`. They are not the same. Use `as.list(var)`. – Ritchie Sacramento Mar 14 '23 at 23:44

0 Answers0