0

I want to conduct a theoretical chi square goodness of fit test:

actual <- c(20,80)
expected <- c(10,90)
chisq.test(expected,actual) 

Sample size n=100, alpha=0.05, df=1. This gives a critical chi value of 3.84. By hand I can calculate the test statistic to be ((20-10)^2)/10 + ((80-90)^2)/90 = 100/9 > 3.84

However, the above code just yields

Pearson's Chi-squared test with Yates' continuity correction

data:  expected and actual 
X-squared = 0, df = 1, p-value = 1

Where is my mistake?

user3213255
  • 21
  • 1
  • 1
  • 3

1 Answers1

0

I don't think you're testing what you intend on testing. As the help at ?chisq.test states, Yates' continuity correction via the correct= argument is: "a logical indicating whether to apply continuity correction when computing the test statistic for 2 by 2 tables."

Instead, try:

chisq.test(x=actual,p=prop.table(expected))

#        Chi-squared test for given probabilities
# 
#data:  actual
#X-squared = 11.1111, df = 1, p-value = 0.0008581

You could use optim to find the right values which just give you a chi-square statistic above the critical value:

critchi <- function(par,actual=c(20,80),crit=3.84) {
  res <- chisq.test(actual,p=prop.table(c(par,100-par)))
  abs(crit - res$statistic)
}
optim(par = c(1), critchi, method="Brent", lower=1,upper=100)$par
#[1] 28.88106

You can confirm this is the case by substituting 29, as the rounded-up whole number of 28.88:

chisq.test(actual, p=prop.table(c(29,100-29)))
#X-squared = 3.9339, df = 1, p-value = 0.04732
thelatemail
  • 91,185
  • 12
  • 128
  • 188
  • Thanks! In fact, my goal is to figure out the minimum difference necessary for the distributions to be significantly different. That is, I want to know the value of x in 'actual <- c(x,100-x) such that X-Squared > 3.84. How do I do this? – user3213255 Feb 05 '14 at 23:33
  • @user3213255 - see my edit for an `optim` solution for finding the exact spot. – thelatemail Feb 06 '14 at 01:13