R chi squared test (3x2 contingency table) for eac

2019-08-07 05:39发布

问题:

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)