7
$\begingroup$

I am calculating chi-squared in R and manually, and getting two different answers. I believe that R is correct, but I am not 100% sure. Can someone please help me understand why?

In R:

test           <- matrix(c(4203, 4218, 786, 771), ncol=2)
dimnames(test) <- list(group = c("control","exp"), click = c("n","y"))
print(test)
print(Xsq      <- chisq.test(test, correct=F)) 

This gives me $\chi^2 = 0.1712$.

If I do this by hand, however, I get $0.339$. Here is my calculation: \begin{align} \frac{(E1 - O1)^2}{E1} &+ \frac{(E2 - O2)^2}{E2} & \\ \frac{(4203-4218)^2}{4203} &+ \frac{(771 - 786)^2}{786} &= .3398 \end{align}

$\endgroup$
  • $\begingroup$ In your hand calculation, how do you calculate E1 and E2? What's the actual hypothesis being tested here? $\endgroup$ – Glen_b -Reinstate Monica Feb 8 '14 at 5:42
7
$\begingroup$

Your R code thinks you have a 2x2 contingency table for the chi-squared test, whereas your 'by hand' version treats two of your values as the expected values with which to compare the first two values. You need to decide which setup is correct and use it consistently both times.

Here is your R version:

> test           <- matrix(c(4203, 4218, 786, 771), ncol=2)
> dimnames(test) <- list(group = c("control","exp"), click = c("n","y"))
> print(test)
         click
group        n   y
  control 4203 786
  exp     4218 771
> print(Xsq      <- chisq.test(test, correct=F))

    Pearson's Chi-squared test

data:  test 
X-squared = 0.1712, df = 1, p-value = 0.679

This is how you do it 'by hand':
\begin{array}{lrrr} & &\text{click} & \\ \rm group &\rm n &\rm y &\rm proportion \\ {\rm control} &4203 &786 &0.50 \\ {\rm exp} &4218 &771 &0.50 \\ \rm proportion &0.844 &0.156 \end{array} Notice that I added the row and column proportions. These are taken as estimates of the probability that an observation will fall in each row (column). The expected count in each cell under the assumption of independence is the row probability times the column probability times the total count. For your data, that gives:
\begin{array}{lrr} &\text{click} & \\ \rm group &\rm n &\rm y \\ {\rm control} &4210.5 &778.5 \\ {\rm exp} &4210.5 &778.5 \\ \end{array} Thus the calculation is:
$$ \frac{(4203-4210.5)^2}{4210.5} + \frac{(4218-4210.5)^2}{4210.5} + \frac{(786-778.5)^2}{778.5} + \frac{(771-778.5)^2}{778.5} = 0.1712, $$ Which is the same as what R gave.


If you take the control row counts as the expected counts, rather than the counts for another condition, you would have the following for your 'by hand' calculation:
$$ \frac{(4218-4203)^2}{4203} + \frac{(771 - 786)^2}{786} = 0.3398 $$ You can also run this version in R like so:

> probs <- test[1,]/sum(test[1,])
> probs
        n         y 
0.8424534 0.1575466 
> chisq.test(test[2,], correct=F, p=probs)

    Chi-squared test for given probabilities

data:  test[2, ] 
X-squared = 0.3398, df = 1, p-value = 0.5599

They key is that you specify the p argument with the expected probabilities (R will take care of calculating the expected counts). At any rate, you can see that the $\chi^2$ values are once again the same.

$\endgroup$

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.