R-code 7
From Biocourse
## 카이제곱 검정
## 각 cell의 비율검정
## table 1
![]()
> freq = c(22,21,22,27,22,36)
# specify probabilities, (uniform, like this, is default though)
> probs = c(1,1,1,1,1,1)/6 # or use rep(1/6,6)
> chisq.test(freq,p=probs)
Chi-squared test for given probabilities
data: freq
X-squared = 6.72, df = 5, p-value = 0.2423
## table 1의 6개의 셀에 대한 비율이 동일한가를 카이제곱 검정을 하였다.
## p-value가 0.2423으로 셀에 대한 비율의 동일성을 기각할 수 없다.
## 동일 분포에 대한 검정

## 다음 table 2와 같이 5개의 변수에 대한 관찰치가 있다.
![]()
## table3은 위의 5개의 변수를 다른 조건으로 관찰하였을때 관측된 관찰치라고 할때
## 2개의 table의 변수들의 분포가 같은지를 검정을 한다.
> x = c(100,110,80,55,14)
> probs = c(29, 21, 17, 17, 16)/100
> chisq.test(x,p=probs)
Chi-squared test for given probabilities
data: x
X-squared = 55.3955, df = 4, p-value = 2.685e-11
## 두 table의 변수들의 분포에 검정은 p-value가 2.685e-11로 귀무가설을 기각한다.
## 두 변수의 독립성 검정

## table 4와 같이 두 변수들에 대한 관측치가 존재할때
## 두 변수들이 독립인지에 대한 검정을 카이제곱 검정을 이용한다.
> yesbelt = c(12813,647,359,42)
> nobelt = c(65963,4000,2642,303)
> chisq.test(data.frame(yesbelt,nobelt))
Pearson's Chi-squared test
data: data.frame(yesbelt, nobelt)
X-squared = 59.224, df = 3, p-value = 8.61e-13
## p-value가 8.61e-13로 두 변수들의 독립성을 기각할 수 있다.
## 즉 두 변수는 서로 연관성을 가진다고 할 수 있다.
