Hypothesis Test: Proportional Difference
Aly Lamuri
Indonesia Medical Education and Research Institute
Overview
LetX∼B(n,p) Test the probability of having: P(X=6 | 10,0.5)
LetX∼B(n,p) Test the probability of having: P(X=6 | 10,0.5)
H0:P(X=6)=0.5Ha:P(X=6)≠0.5
| estimate | statistic | p.value | parameter | conf.low | conf.high | method | alternative |
|---|---|---|---|---|---|---|---|
| 0.6 | 6 | 0.754 | 10 | 0.262 | 0.878 | Exact binomial test | two.sided |
| estimate | statistic | p.value | parameter | conf.low | conf.high | method | alternative |
|---|---|---|---|---|---|---|---|
| 0.6 | 0.1 | 0.752 | 1 | 0.274 | 0.863 | 1-sample proportions test with continuity correction | two.sided |
LetX∼B(n,p) Test the probability of having: P(X=60 | 100,0.5)
LetX∼B(n,p) Test the probability of having: P(X=60 | 100,0.5)
H0:P(X=60)=0.5Ha:P(X=60)≠0.5
| estimate | statistic | p.value | parameter | conf.low | conf.high | method | alternative |
|---|---|---|---|---|---|---|---|
| 0.6 | 60 | 0.057 | 100 | 0.497 | 0.697 | Exact binomial test | two.sided |
| estimate | statistic | p.value | parameter | conf.low | conf.high | method | alternative |
|---|---|---|---|---|---|---|---|
| 0.6 | 3.61 | 0.057 | 1 | 0.497 | 0.695 | 1-sample proportions test with continuity correction | two.sided |
Fun fact: The contingency table is also called a cross tabulation
| Outcome 1 | Outcome 2 | |
|---|---|---|
| Exposure 1 | a | b |
| Exposure 2 | c | d |
Fun fact: The contingency table is also called a cross tabulation
We are conducting a market research in Jakarta, where we aim to see how people express their preferences in choosing chain store outlets. We categorized participants based on their place of residency, i.e. in suburban and urban area. The mini-market chain of our interest would be Indomaret and Alfamart. We observed 30 out of 50 respondents in suburban area choose Indomaret, compared to 20 out of 50 respondents in urban area.
| Indomaret | Alfamart | |
|---|---|---|
| Suburban | 30 | 20 |
| Urban | 20 | 30 |
| Indomaret | Alfamart | |
|---|---|---|
| Suburban | 30 | 20 |
| Urban | 20 | 30 |
How do we test for differences?
Overview
H0:^p1=^p2Ha:^p1≠^p2
P=(a+ba)(a+bb)(na+b)=(c+dc)(c+dd)(nc+d)=(a+b)! (c+d)! (a+c)! (b+d)!a! b! c! d! n!n=a+b+c+d(1)(2)(3)
You may choose any of those equations
fisher.eq <- function(abcd) { # abcd is a list of 4 elements a <- abcd[1]; b <- abcd[2]; c <- abcd[3]; d <- abcd[4] choose(a+b, a) * choose(a+b, b) / choose(a+b+c+d, a+b)}fisher.eq <- function(abcd) { # abcd is a list of 4 elements a <- abcd[1]; b <- abcd[2]; c <- abcd[3]; d <- abcd[4] choose(a+b, a) * choose(a+b, b) / choose(a+b+c+d, a+b)}
| Indomaret | Alfamart | |
|---|---|---|
| Suburban | 30 | 20 |
| Urban | 20 | 30 |
| a | b | c | d | probability | |
|---|---|---|---|---|---|
| 1 | 30 | 20 | 20 | 30 | 0.0220153934585849 |
| 2 | 31 | 19 | 19 | 31 | 0.00916353525851611 |
| 3 | 32 | 18 | 18 | 32 | 0.00323050412922296 |
| 4 | 33 | 17 | 17 | 33 | 0.000961141724396915 |
| 5 | 34 | 16 | 16 | 34 | 0.000240285431099229 |
| 6 | 35 | 15 | 15 | 35 | 0.0000502147513154306 |
| 7 | 36 | 14 | 14 | 36 | 0.00000871783877004004 |
| 8 | 37 | 13 | 13 | 37 | 0.00000124813469607586 |
| 9 | 38 | 12 | 12 | 38 | 1.46076706119681e-7 |
| 10 | 39 | 11 | 11 | 39 | 1.38297473249402e-8 |
sum(tbl$probability)
## [1] 0.0357sum(tbl$probability)
## [1] 0.0357sum(tbl$probability) * 2
## [1] 0.0713R do the hard stuff for usfisher.test(survey, alternative="greater")
## ## Fisher's Exact Test for Count Data## ## data: survey## p-value = 0.04## alternative hypothesis: true odds ratio is greater than 1## 95 percent confidence interval:## 1.06 Inf## sample estimates:## odds ratio ## 2.23R do the hard stuff for usfisher.test(survey, alternative="two.sided")
## ## Fisher's Exact Test for Count Data## ## data: survey## p-value = 0.07## alternative hypothesis: true odds ratio is not equal to 1## 95 percent confidence interval:## 0.94 5.41## sample estimates:## odds ratio ## 2.23numpy is allowed though)c(40, 15, 15, 20) %>% matrix(nrow=2) %>% fisher.test()
## ## Fisher's Exact Test for Count Data## ## data: .## p-value = 0.007## alternative hypothesis: true odds ratio is not equal to 1## 95 percent confidence interval:## 1.33 9.58## sample estimates:## odds ratio ## 3.5Overview
stat∼χ2(k)
stat∼χ2(k)
χ2=∑i,j(Oij−Eij)2EijEij=∑Oi⋅∑Oj∑Oi+Oj
O: Observed outcome
E: Expected outcome
i,j: Elements in the contingency table
| Outcome 1 | Outcome 2 | |
|---|---|---|
| Exposure 1 | a | b |
| Exposure 2 | c | d |
Eij=∑Oi⋅∑Oj∑Oi+Oj
E11=(a+b)⋅(a+c)a+b+c+d
| Outcome 1 | Outcome 2 | |
|---|---|---|
| Exposure 1 | a | b |
| Exposure 2 | c | d |
Eij=∑Oi⋅∑Oj∑Oi+Oj
E12=(a+b)⋅(b+d)a+b+c+d
| Outcome 1 | Outcome 2 | |
|---|---|---|
| Exposure 1 | a | b |
| Exposure 2 | c | d |
Eij=∑Oi⋅∑Oj∑Oi+Oj
E21=(c+d)⋅(a+c)a+b+c+d
| Outcome 1 | Outcome 2 | |
|---|---|---|
| Exposure 1 | a | b |
| Exposure 2 | c | d |
Eij=∑Oi⋅∑Oj∑Oi+Oj
E22=(c+d)⋅(b+d)a+b+c+d
| Indomaret | Alfamart | |
|---|---|---|
| Suburban | 30 | 20 |
| Urban | 20 | 30 |
Eij=∑Oi⋅∑Oj∑Oi+OjE11=25E12=25E21=25E22=25
| Indomaret | Alfamart | |
|---|---|---|
| Suburban | 30 | 20 |
| Urban | 20 | 30 |
χ2=∑i,j(Oij−Eij)2Eij=(30−25)225+(20−25)225+(30−25)225+(20−25)225=4
| Indomaret | Alfamart | |
|---|---|---|
| Suburban | 30 | 20 |
| Urban | 20 | 30 |
1 - pchisq(4, df=1)
## [1] 0.0455| Indomaret | Alfamart | |
|---|---|---|
| Suburban | 30 | 20 |
| Urban | 20 | 30 |
Rchisq.test(survey, correct=FALSE)$p.value
## [1] 0.0455Overview
Suppose we continue our market research, where we ask exactly same subjects three months later. We expected no changes in their preferences of chain-store outlets. It turned out, regardless of their area of residence, 25 people who previously preferred go to Indomaret now shop in Alfamart. Meanwhile, 20 people who used to visit Alfamart now prefer Indomaret.
| Indomaret | Alfamart | |
|---|---|---|
| Indomaret | 25 | 25 |
| Alfamart | 20 | 30 |
H0:^pt0=^pt1H1:^pt0≠^pt1
χ2=(b−c)2b+c
χ2=(b−c)2b+c
mcnemar.test(survey2)
## ## McNemar's Chi-squared test with continuity correction## ## data: survey2## McNemar's chi-squared = 0.4, df = 1, p-value = 0.6Overview
χ2=∑i,j(|Oij−Eij|−0.5)2Eij
Query?
Overview
Keyboard shortcuts
| ↑, ←, Pg Up, k | Go to previous slide |
| ↓, →, Pg Dn, Space, j | Go to next slide |
| Home | Go to first slide |
| End | Go to last slide |
| Number + Return | Go to specific slide |
| b / m / f | Toggle blackout / mirrored / fullscreen mode |
| c | Clone slideshow |
| p | Toggle presenter mode |
| t | Restart the presentation timer |
| ?, h | Toggle this help |
| Esc | Back to slideshow |