简体   繁体   English

R单向方差分析(仅获得1个DF并期望2个DF)

[英]R One-Way ANOVA (getting only 1 DF and expecting 2 DFs)

I'm working through the examples of One-Way ANOVA on the UCLA website http://www.ats.ucla.edu/stat/r/faq/posthoc.htm . 我正在研究UCLA网站http://www.ats.ucla.edu/stat/r/faq/posthoc.htm上的单向方差分析。 When I run the command a1 <-aov(write ~ ses) , my output differs from the example output. 当我运行命令a1 <-aov(write ~ ses) ,我的输出与示例输出不同。 I'm particularly bothered by the fact that when I run the command summary(a1) , my DF on ses is 1 and there are three ses categories (1,2,3) so I'm expecting 2 DFs which is what the example on the website shows. 当我运行命令summary(a1) ,我特别烦恼,我在ses上的DF是1,并且有三个ses类别(1,2,3),所以我期望有2个DF,这就是示例在网站上显示。 I've checked the data for the 'write' column and 'ses' column and the counts and averages seem to match with the example, but the result from aov(write ~ ses) doesn't. 我已经检查了“写”列和“ ses”列的数据,其计数和平均值似乎与示例匹配,但是aov(write ~ ses)的结果aov(write ~ ses)匹配。 Has something changed? 有什么改变吗? Why am I getting only 1 DF. 为什么我只得到1 DF。

hsb2 <- read.table("http://www.ats.ucla.edu/stat/data/hsb2.csv", sep=",", header=TRUE)
a1 <- aov(write ~ ses, data = hsb2)
summary(a1)
#              Df Sum Sq Mean Sq F value Pr(>F)   
# ses           1    770   769.8   8.908 0.0032 **
# Residuals   198  17109    86.4         

The page you are learning from has an error, in that it doesn't tell you how to enter the data correctly. 您正在学习的页面有错误,因为它没有告诉您如何正确输入数据。 The ses variable is supposed to be a factor, as we can see from the data they give us, it is read in as numeric: ses变量应该是一个因素,正如我们从它们提供给我们的数据中看到的那样,它被读为数字形式:

str(hsb2$ses)

If we convert it to a factor, we get the same answer as the example: 如果将其转换为因子,则得到与示例相同的答案:

hsb2$ses <- as.factor(hsb2$ses)
a1 <- aov(write ~ ses, data=hsb2)
summary(a1)

             Df Sum Sq Mean Sq F value  Pr(>F)   
ses           2    859   429.4    4.97 0.00784 **
Residuals   197  17020    86.4                   
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

In addition, using attach is highly discouraged by most R users. 此外,大多数R用户强烈建议不要使用attach。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM