How to evaluate a string variable as factor in the emmeans() command in R?

Viewed 140

I would like to assign a variable with a custom factor from an ANOVA model to the emmeans() statement. Here I use the oranges dataset from R to make the code reproducible. This is my model and how I would usually calculate the emmmeans of the factor store:

library(emmeans) 
oranges$store<-as.factor(oranges$store)
model <- lm (sales1 ~ 1 + price1 + store ,data=oranges)
means<-emmeans(model, pairwise  ~ store, adjust="tukey")

Now I would like to assign a variable (lsmeanfact) defining the factor for which the lsmeans are calculated.

lsmeanfact<-"store"

However, when I want to evaluate this variable in the emmeans() function it returns an error, it basically does not find the variable lsmeanfact, so it does not evaluate this variable.

means<-emmeans(model, pairwise  ~ eval(parse(lsmeanfact)), adjust="tukey")
Error in emmeans(model, pairwise ~ eval(parse(lsmeanfact)), adjust = "tukey") : 
  No variable named lsmeanfact in the reference grid

How should I change my code to be able to evaluate the variable lsmeanfact so that the lsmeans for "plantcode" are correctly calculated?

2 Answers

You can make use of reformulate function.

library(emmeans)
lsmeanfact<-"store"

means <- emmeans(model, reformulate(lsmeanfact, 'pairwise'), adjust="tukey")

Or construct a formula with formula/as.formula.

means <- emmeans(model, formula(paste('pairwise', lsmeanfact, sep = '~')), adjust="tukey")

Here both reformulate(lsmeanfact, 'pairwise') and formula(paste('pairwise', lsmeanfact, sep = '~')) return pairwise ~ store.

You do not need to do anything special at all. The specs argument to emmeans() can be a character value. You can get the pairwise comparisons in a separate call, which is actually a better way to go anyway.

library(emmeans)

model <- lm(sales1 ~ price1 + store, data = oranges)

lsmeanfact <- "store"

( EMM <- emmeans(model, lsmeanfact) )
##  store emmean   SE df lower.CL upper.CL
##  1       8.01 2.61 29     2.67     13.3
##  2       9.60 2.30 29     4.89     14.3
##  3       7.84 2.30 29     3.13     12.6
##  4      10.44 2.35 29     5.63     15.2
##  5      10.19 2.28 29     5.53     14.9
##  6      15.22 2.28 29    10.56     19.9
## 
## Confidence level used: 0.95

pairs(EMM)
##  contrast estimate   SE df t.ratio p.value
##  1 - 2      -1.595 3.60 29 -0.443  0.9976 
##  1 - 3       0.165 3.60 29  0.046  1.0000 
##  1 - 4      -2.428 3.72 29 -0.653  0.9856 
##  1 - 5      -2.185 3.50 29 -0.625  0.9882 
##  1 - 6      -7.209 3.45 29 -2.089  0.3206 
##  2 - 3       1.761 3.22 29  0.546  0.9936 
##  2 - 4      -0.833 3.23 29 -0.258  0.9998 
##  2 - 5      -0.590 3.23 29 -0.182  1.0000 
##  2 - 6      -5.614 3.24 29 -1.730  0.5239 
##  3 - 4      -2.593 3.23 29 -0.802  0.9648 
##  3 - 5      -2.350 3.23 29 -0.727  0.9769 
##  3 - 6      -7.375 3.24 29 -2.273  0.2373 
##  4 - 5       0.243 3.26 29  0.075  1.0000 
##  4 - 6      -4.781 3.28 29 -1.457  0.6930 
##  5 - 6      -5.024 3.23 29 -1.558  0.6314 
## 
## P value adjustment: tukey method for comparing a family of 6 estimates

Created on 2021-06-29 by the reprex package (v2.0.0)

Moreover, in any case what is needed in specs are the name(s) of the factors involved, not the factors themselves. Note also that it was unnecessary to convert store to a factor before fitting the model

Related