aov() 中的因素

问题描述 投票:0回答:1

我按照 R Guide 中的示例 1 遇到了接线问题。 这是例子

> datafilename="http://personality-project.org/r/datasets/R.appendix1.data"
> data.ex1 = read.table(datafilename,header=T)   #read the data into a table 
> aov.ex1 = aov(Alertness~Dosage,data=data.ex1)  #do the analysis of variance
> summary(aov.ex1)                                    #show the summary table

但是,当我对自己的数据应用 aov 时,情况发生了变化。

> test.data <- data.frame(fac=letters[c(1:3,1:3)], x=1:6)
> test.result <- aov(fac~x, data=test.data)
Error in storage.mode(y) <- "double" :
  invalid to change the storage mode of a factor
In addition: Warning message:
In model.response(mf, "numeric") :
  using type="numeric" with a factor response will be ignored

我完全困惑了。 R指南示例中的test.data和data.ex1有什么区别?

> str(test.data)
'data.frame':   6 obs. of  2 variables:
 $ fac: Factor w/ 3 levels "a","b","c": 1 2 3 1 2 3
 $ x  : int  1 2 3 4 5 6
> str(data.ex1)
'data.frame':   18 obs. of  2 variables:
 $ Dosage   : Factor w/ 3 levels "a","b","c": 1 1 1 1 1 1 2 2 2 2 ...
 $ Alertness: int  30 38 35 41 27 24 32 26 31 29 ...
r anova
1个回答
4
投票

应该是

aov(x ~ fac, data = test.data)
,有效。公式需要是响应〜因子,而不是因子〜响应。

© www.soinside.com 2019 - 2024. All rights reserved.