📌 적합성 검정(goodness of fit test)
- 범주형 변수가 하나일 때 범주별 비율 분포에 대한 가설 검정
- 관측한 빈도를 토대로 모집단에서의 집단별 비율 분포 검정
> chisq.test(c(60, 55, 35))
Chi-squared test for given probabilities
data: c(60, 55, 35)
X-squared = 7, df = 2, p-value = 0.0302
위와 같이 별도 비율을 설정하지 않으면 동일하다고 가정한다.
p-value=0.0302 이므로 귀무가설을 기각한다.
> oc <- c(60, 55, 35)
> null.p <- c(0.45, 0.30, 0.25) # 비율
> chisq.test(oc, p=null.p)
Chi-squared test for given probabilities
data: oc
X-squared = 3.2222, df = 2, p-value = 0.1997
p-value=0.1997 이므로 귀무가설을 기각할 수 없다.
> chisq.test(oc, p=c(45, 25, 15)/85)
Chi-squared test for given probabilities
data: oc
X-squared = 10.178, df = 2, p-value = 0.006165
p-value=0.006165 이므로 귀무가설을 기각한다.
다차원 테이블
> str(HairEyeColor)
'table' num [1:4, 1:4, 1:2] 32 53 10 3 11 50 10 30 10 25 ...
- attr(*, "dimnames")=List of 3
..$ Hair: chr [1:4] "Black" "Brown" "Red" "Blond"
..$ Eye : chr [1:4] "Brown" "Blue" "Hazel" "Green"
..$ Sex : chr [1:2] "Male" "Female"
> head(HairEyeColor)
, , Sex = Male
Eye
Hair Brown Blue Hazel Green
Black 32 11 10 3
Brown 53 50 25 15
Red 10 10 7 7
Blond 3 30 5 8
, , Sex = Female
Eye
Hair Brown Blue Hazel Green
Black 36 9 5 2
Brown 66 34 29 14
Red 16 7 7 7
Blond 4 64 5 8
> hairs <- margin.table(HairEyeColor, margin=1)
> hairs
Hair
Black Brown Red Blond
108 286 71 127
> chisq.test(hairs, p=c(0.25, 0.50, 0.10, 0.15))
Chi-squared test for given probabilities
data: hairs
X-squared = 29.934, df = 3, p-value = 1.425e-06
귀무가설을 기각한다.
DataFrame
> str(survey)
'data.frame': 237 obs. of 12 variables:
$ Sex : Factor w/ 2 levels "Female","Male": 1 2 2 2 2 1 2 1 2 2 ...
$ Wr.Hnd: num 18.5 19.5 18 18.8 20 18 17.7 17 20 18.5 ...
$ NW.Hnd: num 18 20.5 13.3 18.9 20 17.7 17.7 17.3 19.5 18.5 ...
$ W.Hnd : Factor w/ 2 levels "Left","Right": 2 1 2 2 2 2 2 2 2 2 ...
$ Fold : Factor w/ 3 levels "L on R","Neither",..: 3 3 1 3 2 1 1 3 3 3 ...
$ Pulse : int 92 104 87 NA 35 64 83 74 72 90 ...
$ Clap : Factor w/ 3 levels "Left","Neither",..: 1 1 2 2 3 3 3 3 3 3 ...
$ Exer : Factor w/ 3 levels "Freq","None",..: 3 2 2 2 3 3 1 1 3 3 ...
$ Smoke : Factor w/ 4 levels "Heavy","Never",..: 2 4 3 2 2 2 2 2 2 2 ...
$ Height: num 173 178 NA 160 165 ...
$ M.I : Factor w/ 2 levels "Imperial","Metric": 2 1 NA 2 2 1 1 2 2 2 ...
$ Age : num 18.2 17.6 16.9 20.3 23.7 ...
> smokers <- table(survey$Smoke)
> smokers
Heavy Never Occas Regul
11 189 19 17
> smokers
Heavy Never Occas Regul
11 189 19 17
> chisq.test(smokers, p=c(0.1, 0.7, 0.1, 0.1))
Chi-squared test for given probabilities
data: smokers
X-squared = 12.898, df = 3, p-value = 0.004862
귀무가설을 기각한다.