I'm doing a contrast anova on a dataset, and I'm just not sure what to do to get the lsmeans estimates relating to one particular contrast (c1).

Here is my code:

bats$Site <- as.factor(bats$Site)
bats$logActivity <- log10(bats$AllBatsAct+1)
c1 <- c(0.5,0.5,0,-0.5,-0.5)    #AB vs DE
c2 <- c(0.5, -0.5, 0, 0, 0)     #A vs B
c3 <- c(0, 0, 0, 0.5, -0.5)     #D vs E
c4 <- c(0.2, 0.2, -0.8, 0.2, 0.2) #C vs the rest
model2a <- lmer(logActivity ~ Ecotone*Position + (1|Site), data=bats)

I've seen in other related questions that the line of code to see the estimates may be like:


But I don't know what the "???" would be. I really only need the means and SE estimates for the c1 contrast: A+B and D+E. Any suggestions?


R is very confusing on this, in that the contrasts operations define how to code factors in the model matrix; but unless those contrasts are orthonormal, the resulting regression coefficients will not be estimates of those contrasts!

Assuming that you do want estimates of the contrasts you show, I suggest you do this (having previously installed the lsmeans package):

# save your contrast coefficients as a list
my.contr <- list(c1 = c1, c2 = c2, c3 = c3, c4 = c4)

# get rid of those contrast specs
contrasts(bats$Position) <- NULL

# fit your model using the default factor coding
model2b <- lmer(logActivity ~ Ecotone*Position + (1|Site), data=bats)

# Obtain the LS means
model2b.lsm <- lsmeans(model2b, ~ Position | Ecotone)

# Estimate the contrasts you specified
contrast(model2b.lsm, my.contr)

The code above will do the LS means and contrasts separately at each level of Ecotone. You can do them marginally (averaged over Ecotone) by omitting the | Ecotone spec in the lsmeans() call. However, your model specifies that the two factors interact, and it is often not meaningful to examine marginal means unless the interaction is negligible.

Actually, you will get the same LS means and contrasts even if you apply these procedures to your model2a. I'm just emphasizing that estimating the contrasts of LS means has nothing to do with the contrasts() coding that is used.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.