两个定量变量之间的独立性

问题描述

我想测试两个定性变量之间是否存在依赖关系。 在使用任何测试之前,我绘制 geom_bar()。

Bar Chart

对我来说,这很明显,当因子变量等于 1 时,因变量比因子变量等于 0 时更常等于 3。当因子变量等于 0 时,与因子变量等于 1 时相比,因变量更常等于 2。

但是如果我执行 chisq.test 或 fisher.test,我会得到一个大于 0.3 的 p 值,这意味着两个定性变量是独立的。但我真的不明白为什么测试不重要。 为了执行测试,我使用了以下代码

chisq.test(table(variable1,variable2))

其中 variable1 和 variable2 是分类变量

预先感谢您的帮助,

C

解决方法

详细方法如下:

#function borrowed from https://stackoverflow.com/a/32544987/4938484
#to maintain the right sum of entries when rounding
smart.round <- function(x) {
  y <- floor(x)
  indices <- tail(order(x-y),round(sum(x)) - sum(y))
  y[indices] <- y[indices] + 1
  y
}

N = 100 #change to appropriate sample size
tab <- matrix(c(8.1,51.4,40.5,3.7,37.0,59.3),ncol=3,byrow=TRUE)
tab <- smart.round(tab/100 * N)
#values in tab were assigned from your bar chart
rownames(tab) <- c("0","1")
colnames(tab) <- c("1","2","3")
tab <- as.table(tab)
chisq.test(tab)
#which gives p-value = 0.03