简体   繁体   English

如何在R中运行一种方差分析

[英]How to run one way ANOVA in R

I've a dataset looking like this: 我有一个像这样的数据集:

> print(mydata)
                col1                 col2                col3
1               0.819               0.851               0.874
2               0.972               0.703               0.821
3               0.891               0.790               0.951
4               0.839               0.799               0.819

I would like to know if there are significant differences between the three groups col1 , col2 and col3 . 我想知道col1col2col3这三个组之间是否存在显着差异。 For this matter, my guess is that the best way is to run an anova test. 对于这个问题,我猜测最好的方法是进行anova测试。

Please find below the script I used to produce the dataset, to run the test and the Error displayed by R: 请在下面找到我用于生成数据集,运行测试的脚本以及R显示的错误:


> mydata <- data.frame(col1, col2, col3)
> accuracymetrics <- as.vector(mydata)
> anova(accuracymetrics)

Error in UseMethod("anova") : no applicable method for 'anova' applied to an object of class "data.frame" UseMethod(“ anova”)中的错误:没有适用于适用于“ data.frame”类对象的'anova'方法

It's the first time I'm running such an analysis in R so bear with me if this question is not interesting for the forum. 这是我第一次在R中运行这样的分析,所以如果这个问题对论坛不感兴趣,请多多包涵。 Any input to solve this error is appreciated! 解决该错误的任何输入表示赞赏!

if I understood you correctly the three groups you are talking about are the three columns in your data. 如果我正确地理解了您的意思,那么您所说的三组就是数据中的三列。 If this is the case you need to do two things: 如果是这种情况,您需要做两件事:

First, reshape your data from wide to long format such that it looks like this 首先,将您的数据从宽格式更改为长格式,使其看起来像这样

group | value
------------
grp1  | 0.819
grp1  | 0.972

This can easily be done with the tidyr package 使用tidyr包可以轻松完成此tidyr

library(tidyr)
longdata <- gather(mydata, group, value)

Second: you have to use aov instead of anova : 第二:你必须使用aov来代替anova

res.aov <- aov(value ~ group, data = longdata)
summary(res.aov)

Here you can find even more details. 在这里您可以找到更多详细信息。 Hope this helps. 希望这可以帮助。

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

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