Skip to content

Instantly share code, notes, and snippets.

@doryokujin
Created May 20, 2012 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doryokujin/2758196 to your computer and use it in GitHub Desktop.
Save doryokujin/2758196 to your computer and use it in GitHub Desktop.
prop.test
# hand calculation での注意:二項分布の同等性検定は2×2の分割表によるχ^2検定に等しいのでそちらの計算方法を採用。
# Q1.はサンプル数が少ないのでイェツの補正を行っている。
######################################
# Q1.
# Head Tail S
# A 4 6 10
# B 6 4 10
# S 10 10 20
#
# hand calculation
# T = (20* (|4*4-6*6| - 20/2)^2 ) / (10*10*10*10) = 0.2 < χ^2(1, 0.05) = 3.84
# ∴ 「差があるとは言えない」
> prop.test(c(4,6),c(10,10))
2-sample test for equality of proportions with continuity correction
data: c(4, 6) out of c(10, 10)
X-squared = 0.2, df = 1, p-value = 0.6547
alternative hypothesis: two.sided
95 percent confidence interval:
-0.7294066 0.3294066
sample estimates:
prop 1 prop 2
0.4 0.6
######################################
# Q2.
# Head Tail S
# A 16 24 40
# B 24 16 40
# S 40 40 80
#
# hand calculation
# T = (80*(16*16-24*24)^2 ) / (40*40*40*40) = 3.2 < χ^2(1, 0.05) = 3.84
# ∴ 「差があるとは言えない」
> prop.test(c(16,24),c(40,40))
2-sample test for equality of proportions with continuity correction
data: c(16, 24) out of c(40, 40)
X-squared = 2.45, df = 1, p-value = 0.1175
alternative hypothesis: two.sided
95 percent confidence interval:
-0.4397033 0.0397033
sample estimates:
prop 1 prop 2
0.4 0.6
######################################
# Q3.
# Head Tail S
# A 40 60 100
# B 60 40 100
# S 100 100 200
#
# hand calculation
# T = (200*(40*40-60*60)^2 ) / (100*100*100*100) = 8 > χ^2(1, 0.05) = 3.84
# ∴ 「有意な差があると言える」
> prop.test(c(40,60),c(100,100))
2-sample test for equality of proportions with continuity correction
data: c(40, 60) out of c(100, 100)
X-squared = 7.22, df = 1, p-value = 0.00721
alternative hypothesis: two.sided
95 percent confidence interval:
-0.34579029 -0.05420971
sample estimates:
prop 1 prop 2
0.4 0.6
######################################
# Q4.
# Head Tail S
# A 400 600 1000
# B 600 400 1000
# S 1000 1000 2000
#
# hand calculation
# T = (2000*(400*400-600*600)^2 ) / (1000*1000*1000*1000) = 80 > χ^2(1, 0.05) = 3.84
# ∴ 「有意な差があると言える」
> prop.test(c(400,600),c(1000,1000))
2-sample test for equality of proportions with continuity correction
data: c(400, 600) out of c(1000, 1000)
X-squared = 79.202, df = 1, p-value < 2.2e-16
alternative hypothesis: two.sided
95 percent confidence interval:
-0.2439407 -0.1560593
sample estimates:
prop 1 prop 2
0.4 0.6
######################################
# Q5.
# Head Tail S
# A 480 520 1000
# B 520 480 1000
# S 1000 1000 2000
#
# hand calculation
# T = (2000*(480*480-520*520)^2 ) / (1000*1000*1000*1000) = 3.2 < χ^2(1, 0.05) = 3.84
# ∴ 「差があるとは言えない」
> prop.test(c(480,520),c(1000,1000))
2-sample test for equality of proportions with continuity correction
data: c(480, 520) out of c(1000, 1000)
X-squared = 3.042, df = 1, p-value = 0.08114
alternative hypothesis: two.sided
95 percent confidence interval:
-0.084791052 0.004791052
sample estimates:
prop 1 prop 2
0.48 0.52
######################################
# Q6.
# Head Tail S
# A 470 530 1000
# B 570 430 1000
# S 1000 1000 2000
#
# hand calculation
# T = (2000*(470*470-530*530)^2 ) / (1000*1000*1000*1000) = 7.2 > χ^2(1, 0.05) = 3.84
# ∴ 「有意な差があると言える」
> prop.test(c(470,530),c(1000,1000))
2-sample test for equality of proportions with continuity correction
data: c(470, 530) out of c(1000, 1000)
X-squared = 6.962, df = 1, p-value = 0.008326
alternative hypothesis: two.sided
95 percent confidence interval:
-0.10474717 -0.01525283
sample estimates:
prop 1 prop 2
0.47 0.53
######################################
# Q7.
# Head Tail S
# A 8 12 20
# B 48 32 80
# S 56 44 100
#
# hand calculation
# T = (100*(8*32-12*48)^2 ) / (20*80*56*44) = 2.60 < χ^2(1, 0.05) = 3.84
# ∴ 「差があるとは言えない」
> prop.test(c(8,48),c(20,80))
2-sample test for equality of proportions with continuity correction
data: c(8, 48) out of c(20, 80)
X-squared = 1.8491, df = 1, p-value = 0.1739
alternative hypothesis: two.sided
95 percent confidence interval:
-0.47129558 0.07129558
sample estimates:
prop 1 prop 2
0.4 0.6
######################################
# Q8.
# job non-job S
# last 95,100 4,900 100,000
# this 95,000 5,000 100,000
# S 190,100 9,900 200,000
#
# hand calculation
# T = (200000*(95100*5000-95000*4900)^2 ) / (100000*100000*191000*9900) = 1.06 < χ^2(1, 0.05) = 3.84
# ∴ 「差があるとは言えない」
> prop.test(c(4900,5000),c(100000,100000))
2-sample test for equality of proportions with continuity correction
data: c(4900, 5000) out of c(1e+05, 1e+05)
X-squared = 1.0416, df = 1, p-value = 0.3075
alternative hypothesis: two.sided
95 percent confidence interval:
-0.002911256 0.000911256
sample estimates:
prop 1 prop 2
0.049 0.050
######################################
# Q9.
# (say = "自分ではっきり言う")
# say not S
# 5M 53 247 300
# 20M 48 152 200
# S 101 399 500
#
# hand calculation
# T = (500*(53*152-48*247)^2 ) / (300*200*101*399) = 2.99 < χ^2(1, 0.05) = 3.84
# ∴ 「差があるとは言えない」
> prop.test(c(53,48),c(300,200))
2-sample test for equality of proportions with continuity correction
data: c(53, 48) out of c(300, 200)
X-squared = 2.606, df = 1, p-value = 0.1065
alternative hypothesis: two.sided
95 percent confidence interval:
-0.14075264 0.01408597
sample estimates:
prop 1 prop 2
0.1766667 0.2400000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment