R - 最小二乘均值对比单向方差分析
R - Least Squared Means Contrasts One Way ANOVA
我正在审查一种方差分析方法并尝试整合最小二乘均值。这是来自 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")
我的输出是这样的:
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
我正试图得到看起来像这样的东西(值不反映真实数据;它们是 https://rcompanion.org/handbook/G_06.html 对 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
我错过了什么?
这是tukeyHSD
函数的工作:
TukeyHSD(aov(model))
Tukey multiple comparisons of means
95% family-wise confidence level
Fit: aov(formula = model)
$cyl.chr
diff lwr upr p adj
B-A -6.920779 -10.769350 -3.0722086 0.0003424
C-A -11.563636 -14.770779 -8.3564942 0.0000000
C-B -4.642857 -8.327583 -0.9581313 0.0112287
'emmeans' 包是 'lsmeans' 的继承者。以下是如何使用它来解决您的问题:
library(emmeans)
model.emmeans <- emmeans(model, "cyl.chr")
pairs(model.emmeans)
但是对于只有一个因素的方差分析模型(单向方差分析),这给出了与 TukeyHSD
相同的结果。
我正在审查一种方差分析方法并尝试整合最小二乘均值。这是来自 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")
我的输出是这样的:
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
我正试图得到看起来像这样的东西(值不反映真实数据;它们是 https://rcompanion.org/handbook/G_06.html 对 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
我错过了什么?
这是tukeyHSD
函数的工作:
TukeyHSD(aov(model))
Tukey multiple comparisons of means
95% family-wise confidence level
Fit: aov(formula = model)
$cyl.chr
diff lwr upr p adj
B-A -6.920779 -10.769350 -3.0722086 0.0003424
C-A -11.563636 -14.770779 -8.3564942 0.0000000
C-B -4.642857 -8.327583 -0.9581313 0.0112287
'emmeans' 包是 'lsmeans' 的继承者。以下是如何使用它来解决您的问题:
library(emmeans)
model.emmeans <- emmeans(model, "cyl.chr")
pairs(model.emmeans)
但是对于只有一个因素的方差分析模型(单向方差分析),这给出了与 TukeyHSD
相同的结果。