Search code examples
rcontrastposthocemmeans

specs argument in emmeans function with R


I'm trying to figure out to do posthoc test in R with emmeans function from emmeans package. However, I couldn't find out what should I put in specs argument. as far as I understand it is where I put the variables that I want to contrast (my independent variables). However, when I put my IV/IVs It gives errors. I'm putting my code and errors below:

X= as.factor(rep(c("A", "B", "C"), each= 50))
Y= as.factor(c("K", "L", "M"), times= 50)
Z= rnorm(150)
Model= lm(Z ~ X+Y+X:Y)
emmeans(Model, X)

`error no variable named a in reference grid.

I guess specs argument is something totally different from what I think. I want to contrast both main effects and interaction effects. How can I do it with emmeans function?
Best regards


Solution

  • From ?emmeans:

    spec A character vector specifying the names of the predictors over which EMMs are desired.

    Your spec argument is X which contains "A", "B" and "C" (repeated 50 times). But your variables in the regression are X, Y and their interaction. You need to include something like

    emmeans(Model, c("X", "Y"))
    

    to estimate marginal means for these variables.

    Short version: you are confusing the character "X" with the variable X.