list
자료형data.frame
과 거의 비슷하게 작동한다.)## $name
## [1] "Joe"
##
## $salary
## [1] 55000
##
## $union
## [1] TRUE
## [1] 55000
## [1] 55000
## [1] 55000
## $salary
## [1] 55000
## $salary
## [1] 55000
## $name
## [1] "Joe"
##
## $salary
## [1] 55000
## [1] 1 2 3 4
## $a
## [1] "abc"
##
## $b
## [1] 12
## $a
## [1] "abc"
##
## $b
## [1] 12
##
## $c
## [1] "sailing"
## [[1]]
## [1] "Joe"
##
## [[2]]
## [1] 55000
##
## [[3]]
## [1] TRUE
##
## [[4]]
## [1] 5
## [[1]]
## [1] 28
##
## [[2]]
## [1] FALSE
##
## [[3]]
## [1] TRUE
##
## [[4]]
## [1] TRUE
## $a
## [1] "abc"
##
## $c
## [1] "sailing"
## [1] 3
## [1] 4 4
## [1] 2
## a c
## "abc" "sailing"
list
형태로 반환한다.list
의 성분을 뽑아내어 함수에서의 결과값을 활용할 수 있다.df_ex <- data.frame(gr = rep(c(1,2), each = 5), x = 1:10)
test_ex <- t.test(x ~ gr, data = df_ex, var.equal = T)
test_ex$statistic
## t
## -5
## [1] 0.001052826
\[Z = \frac{\hat{p}-p}{\sqrt{\hat{p}(1-\hat{p})/n}}\approx \frac{\hat{p}-p}{\sqrt{p(1-p)/n}} \mathrel{\dot{\sim}}N(0,1)\]
\[Z = \frac{(\hat{p}_1 - \hat{p}_2)- (p_1 - p_2)}{\sqrt{\hat{p}_1(1-\hat{p}_1)/n_1 + \hat{p}_2(1-\hat{p}_2)/n_2}}\approx \frac{(\hat{p}_1 - \hat{p}_2)- (p_1 - p_2)}{\sqrt{p_1(1-p_1)/n_1 + p_2(1-p_2)/n_2}} \mathrel{\dot{\sim}}N(0,1)\]
모비율에 대한 귀무가설 \(H_0:~ p = p_0\)에 대한 표본비율을 이용한 검정은 다음과 같이 실시한다.
대립가설에 따른 기각역과 검정통계량 \(z_0\)에 대한 유의확률 계산 :
가설 | 기각역 | 유의확률 |
---|---|---|
\(H_1\): \(p\) > \(p_0\) | \(Z \geq z_{\alpha}\) | \(\mathbb{P}(Z \geq z_0)\) |
\(H_1\): \(p\) < \(p_0\) | \(Z \leq z_{\alpha}\) | \(\mathbb{P}(Z \leq z_0)\) |
\(H_1\): \(p \neq p_0\) | \(|Z| \geq z_{\alpha/2}\) | \(\mathbb{P}(|Z| \geq |z_0|)=2 \mathbb{P}(Z \geq |z_0|)\) |
두 모비율의 차에 대한 귀무가설 \(H_0:~ p_1 - p_2 = p_0\)에 대한 표본비율을 이용한 검정은 다음과 같이 실시한다.
대립가설에 따른 기각역과 검정통계량 \(z_0\)에 대한 유의확률 계산 :
가설 | 기각역 | 유의확률 |
---|---|---|
\(H_1\): \(p_1-p_2\) > \(p_0\) | \(Z \geq z_{\alpha}\) | \(\mathbb{P}(Z \geq z_0)\) |
\(H_1\): \(p_1-p_2\) < \(p_0\) | \(Z \leq z_{\alpha}\) | \(\mathbb{P}(Z \leq z_0)\) |
\(H_1\): \(p_1-p_2 \neq p_0\) | \(|Z| \geq z_{\alpha/2}\) | \(\mathbb{P}(|Z| \geq |z_0|)=2 \mathbb{P}(Z \geq |z_0|)\) |
\(p_0=0\)일 때는 다음과 같이 합동표본비율 \(\hat{p} = \frac{X_1 +X_2}{n_1+n_2}\)을 이용해 검정을 실시할 수도 있다.
대립가설에 따른 기각역과 검정통계량 \(z_0\)에 대한 유의확률 계산 :
가설 | 기각역 | 유의확률 |
---|---|---|
\(H_1\): \(p_1-p_2\) > \(p_0\) | \(Z \geq z_{\alpha}\) | \(\mathbb{P}(Z \geq z_0)\) |
\(H_1\): \(p_1-p_2\) < \(p_0\) | \(Z \leq z_{\alpha}\) | \(\mathbb{P}(Z \leq z_0)\) |
\(H_1\): \(p_1-p_2 \neq p_0\) | \(|Z| \geq z_{\alpha/2}\) | \(\mathbb{P}(|Z| \geq |z_0|)=2 \mathbb{P}(Z \geq |z_0|)\) |
\[H_0:~p_1 = p_2 = \cdots =p_r, \quad H_1:~\text{not }H_0\]
chisq.test()
함수를 통해 실시할 수 있다.##
## Pearson's Chi-squared test
##
## data: x
## X-squared = 36.048, df = 2, p-value = 1.487e-08
즉, 남학생과 여학생에 따른 과목의 선호도가 다르다고 할 만한 충분한 근거가 있다 말할 수 있다.
다음과 같이 동질성 검정을 실시하는 my_chisq_test()
함수를 직접 만들 수 있다.
my_chisq_test <- function(data){
N = sum(data)
r = nrow(data)
c = ncol(data)
expt = matrix(0, r, c)
for(i in 1:r){
for(j in 1:c){
expt[i, j] <- sum(data[i, ])*sum(data[, j]) / N
}
}
test_stat <- sum((data-expt)^2/expt)
df = (r-1)*(c-1)
p.value <- pchisq(test_stat,df,lower.tail=F)
return(list(statistic = test_stat, df = df,
p.value = p.value))
}
my_chisq_test(x)
## $statistic
## [1] 36.04756
##
## $df
## [1] 2
##
## $p.value
## [1] 1.48721e-08
\[H_0:~ p_{ij} = p_{i\cdot}~ p_{j\cdot}~ (i=1,2,\cdots,r, ~j = 1,2,\cdots, c), \quad H_1:~\text{not }H_0\]
chisq.test()
함수를 통해 실시할 수 있다.##
## Pearson's Chi-squared test
##
## data: x
## X-squared = 36.048, df = 2, p-value = 1.487e-08
성별에 따른 과목의 선호도는 차이가 없다 vs 성별과 과목의 선호도는 독립이다
table()
함수를 사용해 계산할 수 있다.## 경영대
## 0.6060606
무엇이 잘못되었을까?
sampling이 잘 되지 않았다.
correct=F
를 사용하여 꺼주자.## blood
## college A AB B O
## 경영대 5 6 4 5
## 공대 4 1 2 3
## 기타 1 0 1 1
## Warning in chisq.test(embl_table, correct = F): Chi-squared approximation
## may be incorrect
##
## Pearson's Chi-squared test
##
## data: embl_table
## X-squared = 0.71429, df = 1, p-value = 0.398
동질성 검정? 두 모비율의 차에 대한 검정?
prop.test()
함수를 사용하면 된다.## Warning in prop.test(as.matrix(embl_table), alternative = "two.sided",
## correct = F): Chi-squared approximation may be incorrect
##
## 2-sample test for equality of proportions without continuity
## correction
##
## data: as.matrix(embl_table)
## X-squared = 0.71429, df = 1, p-value = 0.398
## alternative hypothesis: two.sided
## 95 percent confidence interval:
## -0.5080624 0.2080624
## sample estimates:
## prop 1 prop 2
## 0.25 0.40
## manually
n_m <- sum(embl_table[1,])
n_e <- sum(embl_table[2,])
p_m <- embl_table[1, 1]/n_m
p_e <- embl_table[2, 1]/n_e
p_tot <- sum(embl_table[,1])/sum(embl_table)
sdme <- sqrt(p_tot * (1-p_tot) * (1/n_e + 1/n_m))
z <- (p_m - p_e) / sdme
z^2
## [1] 0.7142857
## Warning in chisq.test(table(lec_df[, 2:3]), correct = F): Chi-squared
## approximation may be incorrect
##
## Pearson's Chi-squared test
##
## data: table(lec_df[, 2:3])
## X-squared = 0.059441, df = 3, p-value = 0.9962
조장님들은 안내셔도 됩니다.
prob_z
, 부먹 학생들의 비율을 prob_b
변수에 저장하여라.## z
## 0.6969697
## b
## 0.3030303
stat_zb
변수에 저장하여라.## z
## 2.462104
## z
## 2.26301
p_zb
변수에 저장하여라.stat_zb
는 정규분포를 따르므로 유의확률은 다음과 같이 계산할 수 있다.## z
## 0.006906229
(참고) 이 문제를 “두 모비율의 차에 대한 검정”으로 풀어서는 안된다. 전체 표본의 수를 \(n\), 찍먹 학생들의 수를 \(n_1\), 부먹 학생들의 수를 \(n_2\)라 하자. 이때, \[Cov(n_1,n_2) = Cov(n_1,n-n_1) = -Var(n_1) \neq 0\]이므로 표본비율 \(\hat{p}_1\), \(\hat{p}_2\)는 절대 독립이 될 수 없다. 따라서, 표본 \(n_1\), \(n_2\)가 서로 독립인 두 이항분포에서의 표본이라 말할 수 없고, 두 모비율의 차에 대한 검정을 적용할 수 없다.
stat_abc
변수에 저장하여라.chisq.test()
의 검정통계량을 추출할 수 있다.## Warning in chisq.test(table_abc): Chi-squared approximation may be
## incorrect
## X-squared
## 2.79627
p_abc
변수에 저장하여라.chisq.test()
의 유의확률을 추출할 수 있다.## [1] 0.8339483