简体   繁体   English

如何在R中使用数据框进行方差分析

[英]How to use data frames to conduct ANOVAs in R

I am currently learning R and am playing around with a dataset that has four nominal variables (Hour.Of.Arrival, Mode, Unit, Weekday), and a continuous dependent variable (Overall). 我目前正在学习R,并且正在使用具有四个标称变量(Hour.Of.Arrival,Mode,Unit,Weekday)和一个连续因变量(Overall)的数据集。 This is all imported from a .csv in a data frame named basic. 这些都是从.csv导入到名为basic的数据框中的。 What I am trying to do is run an ANOVA just using this data frame, without creating separate vectors (eg Mode<-basic$Mode). 我想做的是仅使用此数据框运行ANOVA,而不创建单独的向量(例如Mode <-basic $ Mode)。 "Fit" holds the results of the ANOVA. “拟合”保存方差分析的结果。 Here is the code that I wrote: 这是我编写的代码:

Fit<-aov(basic["Overall"],basic["Unit"],data=basic)

However, I keep getting the error 但是,我不断收到错误

"Error in terms.default(formula, "Error", data = data) : no terms component nor attribute “术语错误。默认值(公式,“错误”,数据=数据):没有术语组件或属性

I hope this question isn't too basic!! 我希望这个问题不太基本!!

Thanks :) 谢谢 :)

I think you want something more like Fit<-aov(Overall ~ Unit,data=basic) . 我认为您想要更多类似Fit<-aov(Overall ~ Unit,data=basic) The Overall ~ Unit tells R to treat Overall as an outcome being predicted by Unit ; Overall ~ Unit告诉R将总体视为Unit预测的结果; you already specify that the dataframe to find these variables is basic. 您已经指定查找这些变量的数据框是基本的。

Here's an example to show you how it works: 这是一个示例,向您展示其工作方式:

> y <- rnorm(100)
> x <- factor(rep(c('A', 'B', 'C', 'D'), each = 25))
> dat <- data.frame(x, y)
> aov(y ~ x, data = dat)
Call:
   aov(formula = y ~ x, data = dat)

Terms:
                        x Residuals
Sum of Squares    2.72218 114.54631
Deg. of Freedom         3        96

Residual standard error: 1.092333
Estimated effects may be unbalanced

Note, you don't need to use the data argument, you could also use aov(dat$y ~ dat$x) , but the first argument to the function should be a formula. 注意,您不需要使用data参数,也可以使用aov(dat$y ~ dat$x) ,但是函数的第一个参数应该是公式。

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

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