You are on page 1of 4

Critical value

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

Pi-Wen Tsai (Math, NTNU) May 19, 2008 2 / 16

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

data: obs data: obs


X-squared = 6.8, df = 2, p-value = 0.03337 X-squared = 4.467, df = 4, p-value = 0.3465
> q.lim = qchisq(0.05, 2, lower.tail = F) > q.lim = qchisq(0.05, 4, lower.tail = F)
> q.lim > q.lim
[1] 5.991 [1] 9.488

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

Example (8.5-3) Conclusion:


> obs = c(13, 9, 6, 5, 7, 10) Don't reject H0 at alpha=0.05
> n = sum(obs) p-value=1-pchisq(2.73, 4) = 0.604
> k = length(obs)
Test for Homogenity
> bar.x = 5.4
> pi = rep(NA, k)
Example (8.6-1)
> pi[1] = sum(dpois(0:3, lam = bar.x))
> pi[2:5] = dpois(4:7, lam = bar.x) > group = gl(2, 50)
> pi[6] = 1 - sum(pi[1:5]) > row1 = factor(rep(1:5, c(8, 13, 16, 10, 3)))
> ei = n * pi > row2 = factor(rep(1:5, c(4, 9, 14, 16, 7)))
> qstat = sum((obs - ei)^2/ei) > grade = c(row1, row2)
> qstat > obs = table(group, grade)
> obs
[1] 2.733
grade
> q.lim = qchisq(0.05, k - 2, lower.tail = F) group 1 2 3 4 5
> q.lim 1 8 13 16 10 3
2 4 9 14 16 7
[1] 9.488
Pi-Wen Tsai (Math, NTNU) May 19, 2008 7 / 16 Pi-Wen Tsai (Math, NTNU) May 19, 2008 8 / 16
done by hand: outer function for the expected values > qstat <- sum((obs - ei)^2/ei)
> qstat
> tot.row = apply(obs, 1, sum)
> tot.row [1] 5.179
1 2 > qchisq(0.05, (ncol(obs) - 1), lower.tail = F)
50 50 [1] 9.488
> tot.col = apply(obs, 2, sum)
> 1 - pchisq(qstat, (ncol(obs) - 1))
> tot.col
[1] 0.2695
1 2 3 4 5
12 22 30 26 10 chisq.test(obs) ex:8.6-1
> ei = outer(tot.row, tot.col)/sum(obs) > chisq.test(table(grade, group))
> ei
Pearson's Chi-squared test
1 2 3 4 5
1 6 11 15 13 5 data: table(grade, group)
2 6 11 15 13 5 X-squared = 5.179, df = 4, p-value = 0.2695

Pi-Wen Tsai (Math, NTNU) May 19, 2008 9 / 16 Pi-Wen Tsai (Math, NTNU) May 19, 2008 10 / 16

Done by hand: R function outer for expected values


ex:8.6-1 (cont) Another way to input the data
> tot.row <- apply(obs, 1, sum)
> obs = rbind(c(8, 13, 16, 10, 3), c(4, 9, 14, 16, 7)) > tot.col <- apply(obs, 2, sum)
> chisq.test(obs) > ei <- outer(tot.row, tot.col)/sum(obs)
> ei
Pearson's Chi-squared test A B Undecided
Science 27.14 18.88 12.98
Social sciences 22.08 15.36 10.56
data: obs Arts 17.48 12.16 8.36
X-squared = 5.179, df = 4, p-value = 0.2695 Administration 25.30 17.60 12.10
> qstat <- sum((obs - ei)^2/ei)
> print(obs) > qstat
[1] 6.685
A B Undecided
> qchisq(0.05, (nrow(obs) - 1) * (ncol(obs) - 1), lower.tail = F)
Science 24 23 12
[1] 12.59
Social sciences 24 14 10
Arts 17 8 13 > 1 - pchisq(qstat, (nrow(obs) - 1) * (ncol(obs) - 1))
Administration 27 19 9 [1] 0.351

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

Pearson's Chi-squared test Example (8.6-3)


> obs <- rbind(c(21, 16, 145, 2, 6), c(14, 4, 175, 13, 4))
data: obs
> rownames(obs) <- c("M", "F")
X-squared = 6.685, df = 6, p-value = 0.351
> colnames(obs) <- c("B", "E", "A", "N", "P")
> out$expected > chisq.test(obs)
Pearson's Chi-squared test
A B Undecided
Science 27.14 18.88 12.98
data: obs
Social sciences 22.08 15.36 10.56
X-squared = 18.93, df = 4, p-value = 0.0008125
Arts 17.48 12.16 8.36
Administration 25.30 17.60 12.10

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

Pi-Wen Tsai (Math, NTNU) May 19, 2008 15 / 16

You might also like