对数据帧进行R chisq.test测试

时间:2017-10-30 06:02:44

标签: r chi-squared

我试图在下面的数据框(称为“habitat.re”)上运行chi sqare分析但是我很难读取数据,但是当我提示时它会给出错误的结果如果应该有3个(每个站点一个),则预计会返回18个不同的列。

我能找到的所有参演者都有数据作为表格,但我自己无法正确转换。

data frame results

2 个答案:

答案 0 :(得分:4)

chisq.test函数用于处理两个变量或本例中的列。如果您想比较所有三列,那么我怀疑您希望比较1-22-33-3,例如

chisq.test(x=habitat.re$Gidgee, y=habitat.re$`Ian's Place`)
chisq.test(x=habitat.re$`Ian's Place`, y=habitat.re$`Saw Mulga`)
chisq.test(x=habitat.re$Gidgee, y=habitat.re$`Saw Mulga`)

实际上,输入上面内容应该直接向R控制台显示很多有用的信息,如下所示:

data:  habitat.re$Gidgee and y=habitat.re$`Ian's Place`
X-squared = 5.5569, df = 1, p-value = 0.01841

足够低p-value可能表示这两列实际上是相关的。

答案 1 :(得分:0)

Pearson的Chi-Squared测试要求将数据框制作成矩阵表,该表仅包含您需要的变量作为数值。注:我的数据框被称为" habitat.re"

habitat.df<-data.matrix(habitat.re, rownames.force = NA)# convert to matrix table
habitat.df<- habitat.df[,-c(1,2,3)] # delete first 3 columns
rownames(habitat.df) <- habitat.re$COMMON.NAME #pull names from original
chisq.test(habitat.df) #do chisquare test
chisq.test(habitat.df)$expected #return predicted values

以下是我的数据框图像

habitat.re

image1

habitat.df

image2