R chi squared test (3x2 contingency table) for each row in a table

心不动则不痛 提交于 2019-12-11 10:27:12

问题


I have a dataframe, and want to perform for each row (3x2 contingency table) a chi squared test .

  • row 1 102 4998 105 3264 105 3636
  • row 2 210 4890 22 3347 20 3721
  • row 3 ...

So for the first row a chi squared test should be performed for the following contingency table;

  • group A 102 4998
  • group B 105 3264
  • group C 105 3636

I use the following code, but this does not calculate the correct p-value (all p-values are equal to zero while this is not the case when I calculate the chi-square test myself):

table <- read.delim("dataframe.txt")
apply(table, 1, function(x) chisq.test(matrix(x,nrow=3)))

Could anyone help me with this?

Thank you in advance

Wannes


回答1:


Use ncol instead of nrow:

apply(table, 1, function(x) chisq.test(matrix(x,ncol=3)))

Currently, you are building a matrix that looks like this for the first row:

102 3264
4998 105
105 3636

When R builds matrices from vectors, it builds them column-wise, so the second value goes into the second row, etc.

Also, if you want it to report just the p-value, you can do the following:

apply(table, 1, function(x) chisq.test(matrix(x,ncol=3))$p.value)


来源:https://stackoverflow.com/questions/34232869/r-chi-squared-test-3x2-contingency-table-for-each-row-in-a-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!