Академический Документы
Профессиональный Документы
Культура Документы
Example (Chi-square test by hand) Compare with 95th percentile of chisquare distribution with 5 degrees of
H0 : pi = 1/6, i = 1, . . . , 6 versus H1 : H0 is not true freedom, denoted by q ∗ = χ20.05 (5).
> alpha = 0.05
> obs <- c(20, 22, 13, 22, 10, 13)
> q.lim <- qchisq(0.05, k - 1, lower.tail = F)
> n <- sum(obs)
> q.lim
> k <- length(obs)
> ei <- n * rep(1/k, k) [1] 11.07
> qstat <- sum((obs - ei)^2/ei)
qstat = 8.36 vs q0 = 11.07
> qstat
[1] 8.36 Conclusion:
Don't reject H0 at alpha=0.05
Done by chisq.test
> out = chisq.test(obs)
> out
P-value
Chi-squared test for given probabilities
Or we can look at Pr (χ2 (k − 1) ≥ qstat)
> 1 - pchisq(qstat, k - 1) data: obs
X-squared = 8.36, df = 5, p-value = 0.1375
[1] 0.1375
> q.lim <- qchisq(0.05, k - 1, lower.tail = F)
Conclusion:
> q.lim
[1] 11.07
Conclusion:
Don't reject H0 at alpha=0.05
Pi-Wen Tsai (Math, NTNU) May 19, 2008 3 / 16 Pi-Wen Tsai (Math, NTNU) May 19, 2008 4 / 16
Example (8.5-1) Example (8.5-2)
> obs = c(0, 8, 42) > obs = c(7, 18, 40, 31, 4)
> pi = c(0.1, 0.2, 0.7) > pi = dbinom(0:4, 4, 0.5)
> chisq.test(obs, p = pi) > chisq.test(obs, p = pi)
Chi-squared test for given probabilities Chi-squared test for given probabilities
Conclusion: Conclusion:
reject H0 at alpha=0.05 Don't reject H0 at alpha=0.05
Pi-Wen Tsai (Math, NTNU) May 19, 2008 5 / 16 Pi-Wen Tsai (Math, NTNU) May 19, 2008 6 / 16
Pi-Wen Tsai (Math, NTNU) May 19, 2008 9 / 16 Pi-Wen Tsai (Math, NTNU) May 19, 2008 10 / 16
Pi-Wen Tsai (Math, NTNU) May 19, 2008 11 / 16 Pi-Wen Tsai (Math, NTNU) May 19, 2008 12 / 16
By chisq.test
Test for Independent
> out = chisq.test(obs)
> out Independent between two different attributes, such as height and weight
Pi-Wen Tsai (Math, NTNU) May 19, 2008 13 / 16 Pi-Wen Tsai (Math, NTNU) May 19, 2008 14 / 16
Example (8.6-5)
> obs <- rbind(c(6, 11, 16, 14, 13), c(5, 9, 8, 6, 2))
> out = chisq.test(obs) Example
> out
> admit = c(22, 24)
Pearson's Chi-squared test > reject = c(351, 317)
> chisq.test(rbind(admit, reject))
Pearson's Chi-squared test with Yates' continuity correction
data: obs
X-squared = 4.752, df = 4, p-value = 0.3137 data: rbind(admit, reject)
X-squared = 0.2182, df = 1, p-value = 0.6404
> out$expected
[,1] [,2] [,3] [,4] [,5]
[1,] 7.333 13.333 16 13.333 10
[2,] 3.667 6.667 8 6.667 5