简体   繁体   中英

analysis of variance in R

So I have a table with two columns; treatments in the first and responses in the second, called V1 and V2 (the default). I tried

aov.ex2 = aov(V2~V1, data=ex2)
summary(aov.ex2)

and got something really strange -- only 1 df for the sum of squares for treatments, despite the fact that there should be 30 - 1 = 29 df Doing V1~V2 gives me the same result. What am I doing wrong?

Try this one

  aov.ex2 = aov(V2~factor(V1), data=ex2)
    summary(aov.ex2)

To expand on @MYaseen208's response:

Back in the old days computer programs did not deal well with non-numeric data, so it was common to recode categorical variables as numeric variables to enter the data into the computer. These programs then needed the user to tell them that this variable that looks like a numeric variable is really representing categories. This can be done in either of 2 ways: as an attribute of the data, or an attribute of the analysis. R takes the approach that things like this are attributes of the data rather than the analysis (which makes a lot more sense to me) so the aov function does not have any arguments to specify which predictors are categorical but rather looks at the data to determine this. Since the aov function can also do analysis of covariance and more general linear models it can accept both categorical and numeric predictors (so will not assume everything is categorical). You did not tell us how you entered your data, but however you did it it looked like numeric data to R and you never told it otherwise so it keeps assuming that it is numeric. You need to tell R that it is categorical factor(V1) . It is possible to do this with every analysis, but it is better to do it once, either when creating/reading the data or once right after ex2$V2 <- factor(ex2$V2) so that every analysis/graph/summary on this data realizes that it is categorical and treats it appropriately.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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