ranovalsmeans

R - Least Squared Means Contrasts One Way ANOVA


I am reviewing one way ANOVAs and trying to integrate least squared means. Here is an example from mtcars.

mtcars.mod <- mutate(mtcars, cyl.chr = case_when(
  cyl == 4 ~ "A",
  cyl == 6 ~ "B",
  cyl == 8 ~ "C"
))

library(lsmeans)

model <- lm(mpg ~ cyl.chr, data = mtcars.mod)
lsmeans(model, 
             ~ cyl.chr,
             adjust = "sidak")

My output is this:

 cyl.chr lsmean    SE df lower.CL upper.CL
 A         26.7 0.972 29     24.2     29.1
 B         19.7 1.218 29     16.7     22.8
 C         15.1 0.861 29     12.9     17.3

I am trying to get to something that looks like this (values not reflective of true data; they are filler from https://rcompanion.org/handbook/G_06.html for filler/example):

$contrasts
 contrast         estimate        SE df    z.ratio p.value
 A - B            4.943822 1.3764706 NA  3.5916658  0.0010
 A - C            0.633731 0.9055691 NA  0.6998152  0.7636
 B - C           -4.310091 1.3173294 NA -3.2718403  0.0031

P value adjustment: tukey method for comparing a family of 3 estimates

   ### Remember to ignore “estimate” and “SE” of differences with CLM,
   ###   as well as “lsmeans” in the output not shown here

What am I missing?


Solution

  • The 'emmeans' package is the successor of 'lsmeans'. Here is how to use it for your question:

    library(emmeans)
    model.emmeans <- emmeans(model, "cyl.chr")
    pairs(model.emmeans)
    

    But for an ANOVA model with only one factor (one-way ANOVA), this gives the same results as TukeyHSD.