> dput(THSWP1_23)
structure(list(`Town District` = c(1, 2, 3, 4, 5, 6, 7, 8, 9),
`health score 1` = c(50, 236, 215, 277, 261, 333, 414, 385,
358), `Health score 2 and 3` = c(51, 238, 218, 281, 266,
339, 421, 393, 367)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -9L))
我想进行卡方检验,看看每个城镇地区的健康评分 1 和健康评分 2+3 之间是否存在显着差异。
我试过这些代码:
> chisq.test(THSWP1_23$`Town District`,THSWP1_23$`health score 1`)
> chisq.test(THSWP1_23$`Town District`,THSWP1_23$`Health score 2 and 3`)
> chisq.test(THSWP1_23$`health score 1`,THSWP1_23$`Health score 2 and 3`)
所有的图都给出相同的输出:
Pearson's Chi-squared test
data: THSWP1_23$`health score 1` and THSWP1_23$`Health score 2 and 3`
X-squared = 72, df = 64, p-value = 0.2303
Warning message:
In chisq.test(THSWP1_23$`health score 1`, THSWP1_23$`Health score 2 and 3`) :
Chi-squared approximation may be incorrect
我似乎无法弄清楚为什么当变量以 3 种不同的方式组合时给出相同的 X、df 和 p 值。
我不断得到近似误差。这是由于数据本身还是错误的代码?
chisq.test
函数根据您的两个参数构造一个列联表。但是你的数据框(在下面的代码中我称之为df
)已经是一个表,所以从它构建一个表是错误的。您正在测试的表格(您的第一个电话)如下所示:
with(df, table(`Town District`, `health score 1`))
health score 1
Town District 50 215 236 261 277 333 358 385 414
1 1 0 0 0 0 0 0 0 0
2 0 0 1 0 0 0 0 0 0
3 0 1 0 0 0 0 0 0 0
4 0 0 0 0 1 0 0 0 0
5 0 0 0 1 0 0 0 0 0
6 0 0 0 0 0 1 0 0 0
7 0 0 0 0 0 0 0 0 1
8 0 0 0 0 0 0 0 1 0
9 0 0 0 0 0 0 1 0 0
这是不是你想要的。随便做
chisq.test(df[,2:3])
Pearson's Chi-squared test
data: df[, 2:3]
X-squared = 0.024654, df = 8, p-value = 1
我认为这是你想要的。结论很明显
round(df[,2] / df[,3],2)
[1] 0.98 0.99 0.99 0.99 0.98 0.98 0.98 0.98 0.98