1
$\begingroup$

I found out that the chi square test in R may not be working. I don't know where I am doing it wrong.

1) test two similar distribution

# create a sequence from 0 to 1000, seperated by 10
x <- seq(0,1000,10)

# create two similar gaussian distribution
r1<- rnorm(x, 1000,100)
r2 <- rnorm(x, 1000,100)

# run the chi square test
chisq.test(r1,r2,simulate.p.value = TRUE)

The result is:

Pearson's Chi-squared test with simulated p-value (based on 2000 replicates)

data:  r1 and r2
X-squared = 10100, df = NA, p-value = 0.0004998

2) Test two different distribution

# create two different gaussian distribution
r1<- rnorm(x, 500,100)
r2 <- rnorm(x, 1000,100)

# run the chi square test
chisq.test(r1,r2,simulate.p.value = TRUE)

Result:

Pearson's Chi-squared test with simulated p-value (based on 2000 replicates)

data:  r1 and r2
X-squared = 10100, df = NA, p-value = 0.0004998

This suggests that whether the distributions I tested are different or not, the chi sq test will say that they are not the same.

The result is the same whether of not simulate.p.value is TRUE.

$\endgroup$
4
  • 2
    $\begingroup$ The chi-square test is intended for categorical data, not continuous data. $\endgroup$
    – jbowman
    Jan 15, 2018 at 17:18
  • 1
    $\begingroup$ @jbowman is correct. This really ought to give an error. But the df = NA is a clue. $\endgroup$
    – Peter Flom
    Jan 16, 2018 at 12:12
  • $\begingroup$ I am voting to leave this open because, while it is about R, the underlying problem is statistical - don't use chisquare test on continuous variables. $\endgroup$
    – Peter Flom
    Jan 16, 2018 at 12:13
  • $\begingroup$ Thank you very much! Yes, the problem is that r1 and r2 is not categorical. ks.test(r1, r2) works out fine. $\endgroup$
    – Eki Ko
    Jan 17, 2018 at 9:16

1 Answer 1

1
$\begingroup$

Chances are, that you are using chisq.test wrong.

chisq.test(x = r1, y = r2, simulate.p.value = TRUE)

Is not a test, if r1 and r2 stem from the same distribution. Instead, the manual says

Otherwise, x and y must be vectors or factors of the same length; [...] the objects are coerced to factors, and the contingency table is computed from these. Then Pearson's chi-squared test is performed of the null hypothesis that the joint distribution of the cell counts in a 2-dimensional contingency table is the product of the row and column marginals.

You are basically producing a 100x100 contingency table consisting of mostly zeros and some ones.

So if I understand you right, you try to do a chisquare distribution test but asked R to do a chisquare test of independence.

$\endgroup$
3
  • $\begingroup$ Thank you very much for answering! Yes, indeed I think I phrased it wrong, I was testing independence of two datasets r1, r2. I tried testing the distribution in this way: chisq.test(r1,p=dnorm(x, 1000,100),simulate.p.value = TRUE,rescale.p = TRUE). Unfortunately, it says Chi-squared test for given probabilities with simulated p-value (based on 2000 replicates), but p-value = 0.0004998 $\endgroup$
    – Eki Ko
    Jan 15, 2018 at 16:00
  • $\begingroup$ This time r1 <- rnorm(x, 1000,100), and I am comparing it against the distribution dnorm(x, 1000,100). I don't see why still p value is so low. $\endgroup$
    – Eki Ko
    Jan 15, 2018 at 16:02
  • $\begingroup$ In chisq.test(x = r1, p = ...) where r1 is not integer you produced a case, which is not even mentioned in help(chisq.test) where it says >" if x is a vector and y is not given, then a goodness-of-fit test is performed (x is treated as a one-dimensional contingency table). The entries of x must be non-negative integers." Mind that last sentence! All I can say is that you will not learn R by random typing what you think should work but only by learning it's functions. Try r-bloggers.com/goodness-of-fit-test-in-r of google or bing or something. fortune(348) $\endgroup$
    – Bernhard
    Jan 16, 2018 at 7:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.