require(lsmeans)
### Covariance example (from Montgomery Design (8th ed.), p.656)
# Uses supplied dataset 'fiber'
fiber.lm <- lm(strength ~ diameter + machine, data = fiber)
# adjusted means and comparisons, treating machine C as control
lsmeans (fiber.lm, trt.vs.ctrlk ~ machine)
### Factorial experiment
warp.lm <- lm(breaks ~ wool * tension, data = warpbreaks)
#-- We only need to see the wool*tension means listed once ...
print(lsmeans (warp.lm, list(pairwise ~ wool | tension, poly ~ tension | wool)),
omit=3)
### Unbalanced split-plot example ###
#-- The imbalance is imposed deliberately to illustrate that
#-- the variance estimates become biased
require(nlme)
Oats.lme <- lme(yield ~ factor(nitro) + Variety, random = ~1 | Block/Variety,
subset = -c(1,2,3,5,8,13,21,34,55), data=Oats)
lsmeans(Oats.lme, list(poly ~ nitro, pairwise ~ Variety))
# Compare with lmer result (if pbkrtest installed, provides df, bias-adjusted SEs)
if (require(pbkrtest)) {
require(lme4)
Oats.lmer <- lmer(yield ~ factor(nitro) + Variety + (1 | Block/Variety),
subset = -c(1,2,3,5,8,13,21,34,55), data=Oats)
lsmeans(Oats.lmer, list(poly ~ nitro, pairwise ~ Variety))
}
# Use glht (multcomp) to do comparisons (note -- this does not use adjusted vcov)
if (require(multcomp)) {
lsmeans(Oats.lmer, pairwise ~ Variety, glhargs=list(df=9.5))
}
# Custom contrasts
lsmeans(Oats.lmer, my.own ~ Variety,
contr = list(my.own = list(G.vs.M = c(1,-1,0), GM.vs.V = c(.5,.5,-1))))
Run the code above in your browser using DataLab