# H0: p=1/2 vs Ha: p is not 1/2
# 44 heads out of 100 trials
### Exact binomal test
Exact binomial test
data: 44 and 100
number of successes = 44, number of trials = 100, p-value = 0.2713
alternative hypothesis: true probability of success is not equal to 0.5
95 percent confidence interval:
0.3408360 0.5428125
sample estimates:
probability of success
0.44
### Normal approximation
> prop.test(44,100)
1-sample proportions test with continuity correction
data: 44 out of 100, null probability 0.5
X-squared = 1.21, df = 1, p-value = 0.2713
alternative hypothesis: true p is not equal to 0.5
95 percent confidence interval:
0.3420219 0.5426412
sample estimates:
p
0.44
# 연속성 수정을 안하려면
> prop.test(44,100,correct=FALSE)
1-sample proportions test without continuity correction
data: 44 out of 100, null probability 0.5
X-squared = 1.44, df = 1, p-value = 0.2301
alternative hypothesis: true p is not equal to 0.5
95 percent confidence interval:
0.3467203 0.5377190
sample estimates:
p
0.44
##### H0: p1 = p2 vs Ha: p1 is not p2
> prop.test(c(9,8),c(50,60))
2-sample test for equality of proportions with continuity correction
data: c(9, 8) out of c(50, 60)
X-squared = 0.1676, df = 1, p-value = 0.6823
alternative hypothesis: two.sided
95 percent confidence interval:
-0.1085548 0.2018881
sample estimates:
prop 1 prop 2
0.1800000 0.1333333
##### 적합성 검정(goodness of fit)
# Mendel's law; 9:3:3:1
> chisq.test(c(216,79,65,21), p=c(9,3,3,1)/16)
Chi-squared test for given probabilities
data: c(216, 79, 65, 21)
X-squared = 1.7262, df = 3, p-value = 0.6311
##### 두 비율의 차이
> prop.test(c(9,8),c(50,60))
2-sample test for equality of proportions with continuity correction
data: c(9, 8) out of c(50, 60)
X-squared = 0.1676, df = 1, p-value = 0.6823
alternative hypothesis: two.sided
95 percent confidence interval:
-0.1085548 0.2018881
sample estimates:
prop 1 prop 2
0.1800000 0.1333333
##### 셋 이상 비율의 차이
> prop.test(c(14,18,15),c(100,100,100))
3-sample test for equality of proportions without continuity
correction
data: c(14, 18, 15) out of c(100, 100, 100)
X-squared = 0.656, df = 2, p-value = 0.7204
alternative hypothesis: two.sided
sample estimates:
prop 1 prop 2 prop 3
0.14 0.18 0.15
##### Simulation
# 이항분포
> set.seed(1234)
> rbinom(n=10,size=1,p=0.5)
[1] 0 1 1 1 1 1 0 0 1 1
> set.seed(1234)
> rbinom(n=1,size=10,p=0.5)
[1] 3
> set.seed(1234)
> rbinom(n=5,size=10,p=0.5)
[1] 3 5 5 6 7
# 다항분포
> set.seed(1234)
> rmultinom(n=1, size=1, p=c(1,1,1,1,1,1)/6)
[,1]
[1,] 0
[2,] 0
[3,] 0
[4,] 0
[5,] 1
[6,] 0
> set.seed(1234)
> rmultinom(n=1, size=10, p=c(1,1,1,1,1,1)/6)
[,1]
[1,] 0
[2,] 2
[3,] 2
[4,] 2
[5,] 3
[6,] 1
> set.seed(1234)
> rmultinom(n=5, size=10, p=c(1,1,1,1,1,1)/6)
[,1] [,2] [,3] [,4] [,5]
[1,] 0 2 2 3 1
[2,] 2 0 2 1 1
[3,] 2 1 1 1 1
[4,] 2 3 3 1 0
[5,] 3 2 1 1 2
[6,] 1 2 1 3 5
# 멘델의 강낭콩 교배 실험
> set.seed(1234)
> rmultinom(n=1, size=381, p=c(9,3,3,1)/16)
[,1]
[1,] 222
[2,] 66
[3,] 68
[4,] 25