r语言 - 具有均值和标准差的双向方差分析

  • 本文关键字:方差 标准差 r语言 anova
  • 更新时间 :
  • 英文 :


是否有一种方法可以在R中使用双向ANOVA,同时提供被测试变量的平均值和标准差?

目前我正在使用代码

summary(aov(Tortuosity~Site*Species,data=BF.C))

但这只提供了F值、概率、残差等。

谢谢

是的,您可以使用model.tables()。尝试?model.tables来熟悉它。您想要的可能是(我将使用一个示例):

aov_out <- aov(yield ~ N*P, npk)
model.tables(aov_out, "means") # This will give you the means.
model.tables(aov_out, se = TRUE) # Will give you standard errors for the effects.
model.tables(aov_out, "means", se = TRUE) # Will give you the standard error for the differences of means.
但是,请注意,最后一个命令仅在模型中没有任何随机效应的情况下才有效。因此,对于如下模型:
aov_out_ran <- aov(yield ~ N*P + Error(block), npk) 

最后一个命令将无法工作,因为它尚未实现。但无论如何,你都会在警告消息中看到它。

你也可以简单地手工计算平均值。用对比编码重新设置模型:

aov_out_contr <- aov(yield ~ N*P, npk, contrasts = list (N = "contr.sum", P = "contr.sum"))

coef()得到模型系数:

coef_aov_out <- coef(aov_out)

由于我们使用了对比编码,coef_aov_out[1]位置的(Intercept)将是大均值,coef_aov_out中的进一步系数将是主效应或交互效应的影响,这些效应需要减去或添加到大均值中,以获得组的特定均值。您可以像这样手工计算它们:

# Compute mean of N0:
N0 <- coef_aov_out[1]+coef_aov_out[2]
# Compute mean of N1:
N1 <- coef_aov_out[1]-coef_aov_out[2]
# Compute mean of P0:
P0 <- coef_aov_out[1]+coef_aov_out[3]
# Compute mean of P1:
P1 <- coef_aov_out[1]-coef_aov_out[3]
# Compute mean of N0xP0:
NOP0 <- coef_aov_out[1]+coef_aov_out[2]+coef_aov_out[3]+coef_aov_out[4]
# Compute mean of N0xP1:
N0P1 <- coef_aov_out[1]+coef_aov_out[2]-coef_aov_out[3]-coef_aov_out[4]
# Compute mean of N1xP0:
N1P0 <- coef_aov_out[1]-coef_aov_out[2]+coef_aov_out[3]-coef_aov_out[4]
# Compute mean of N1xP1:
N1P1 <- coef_aov_out[1]-coef_aov_out[2]-coef_aov_out[3]+coef_aov_out[4]

可以将结果与model.tables(aov_out_contr, "means")进行比较。

这是一个很好的练习来理解发生了什么。

最新更新