Source: http://www.r-bloggers.com/contingency-table-and-the-study-of-the-correlation-between-qualitative-variables-pearsons-chi-squared-test/
如果你有定性的变量(qualitative variable),可通过研究列联表(contingency table)R by C,并使用Pearson的chi-squared检验来验证相关性。
---------------------------------------------
一个赌场想要研究赌博玩法模式和不同年龄群组中赢家数目之间的相关性,想看看赢家的数目是否依赖于赌博游戏的类型,从经验来看。这里有如下数据:
Age | |||
Game | 20-30 | 31-40 | 41-50 |
Roulette | 44 | 56 | 55 |
Black-jack | 66 | 88 | 23 |
Poker | 15 | 29 | 45 |
在R中,你必须首先用收集的数据构建一个矩阵:
table <- matrix(c(44,56,55, 66,88,23, 15,29,45), nrow=3, byrow=TRUE)
现在我们能够计算chi-squared相关系数了:
chisq.test(table) Pearson's Chi-squared test data: table X-squared = 46.0767, df = 4, p-value = 2.374e-09
因为p-value < 0.05,于是我们拒绝null hypothesis H0,即拒绝相关系数为0:在玩家年龄和其赢的可能性之间有很强的相关性。