Alison Meeth
Alison Meeth

Reputation: 95

Defining contrasts in emmeans

I need to use emmeans to calculate the estimated marginal means of each combination of nutrient level and food web treatment (i.e., H + A, H + G, H + P, L + A, L + G, L + P).

Then, I need to define contrasts to test (1) whether G is different from A in the H treatment, (2) whether G is different from A in the L treatment, and (3) whether G is different from A, averaging over the L and H treatments.

This is what I have:

library(emmeans)
library(magrittr)

contmod=emmeans(modlog, specs = ~ NutrientLevel + FoodWeb)
contmod

HA = c(1,0,0,0,0,0)
LA = c(0,1,0,0,0,0)
HG = c(0,0,1,0,0,0)
LG = c(0,0,0,1,0,0)
HP = c(0,0,0,0,1,0)
LP = c(0,0,0,0,0,1)

cont=contrast(contmod, method = list("HG - HA", "LG - LA"))

However, I am getting errors saying:

Error in contrast.emmGrid(contmod, method = list("HG - HA", "LG - LA")) 
: Nonconforming number of contrast coefficients

And I can't figure out how to set up the third contrast.

Here is an example of my data:

NutrientLevel   ShadeCloth  FoodWeb Tank    Zoop_Chao1  Phyto#taxa  Phyto_Chao1 Block
H   N   A   15  0   9   9   1
H   N   A   115 0   8   11  4
H   Y   G   30  11  14  17  1
H   Y   G   60  18  12  18  2
H   Y   P   76  9   10  11  2
H   Y   P   88  13  8   9.5 3
L   N   A   16  0   15  15  1
L   N   A   24  0   8   8   2
L   N   G   10  8   17  17  1
L   N   G   82  10  18  20  3

Upvotes: 0

Views: 1039

Answers (1)

cazman
cazman

Reputation: 1492

That is not how contrasts are defined with emmeans. You are also providing a string rather than the variables you created.

fit <- lm(mpg ~ factor(cyl), data = mtcars)
means <- emmeans(fit, specs = ~ cyl)
contr <- list("6 vs 4" = c(1, -1, 0),
              "8 vs 4" = c(1, 0, -1))
contrast(means, method = contr)

 contrast estimate   SE df t.ratio p.value
 6 vs 4       6.92 1.56 29 4.441   0.0001 
 8 vs 4      11.56 1.30 29 8.905   <.0001 

Upvotes: 1

Related Questions