R, T-test (R, T검정) Start.
BioinformaticsAndMe
1) one sample t-test
> bmi.ttest <- t.test(bmi, mu=30)
> bmi.ttest
One Sample t-test
data: bmi
t = 5.3291, df = 199, p-value = 2.661e-07
alternative hypothesis: true mean is not equal to 30
95 percent confidence interval:
31.45521 33.16479
sample estimates:
mean of x
32.31
> names(bmi.ttest)
[1] "statistic" "parameter" "p.value" "conf.int" "estimate" "null.value" "alternative" "method" "data.name"
statistic: 검정통계랑
parameter: 파라미터
p.value: p값
2) two sample t-test
- 집단 사이의 등분산 여부에 따라 분석방법이 달라지므로 등분산 검정 필요.
# 두 집단의 등분산 검정 / '~'는 type이라는 factor값에 의해 분류
> var.test(bmi ~ type)
F test to compare two variances
data: bmi by type
F = 1.7595, num df = 131, denom df = 67, p-value = 0.01115
alternative hypothesis: true ratio of variances is not equal to 1
95 percent confidence interval:
1.140466 2.637564
sample estimates:
ratio of variances
1.75945
--- F 검정 결과 귀무가설을 기각하므로 등분산이 아님
> t.test(bmi ~ type)
Welch Two Sample t-test
data: bmi by type
t = -4.512, df = 171.457, p-value = 1.188e-05
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-5.224615 -2.044547
sample estimates:
mean in group No mean in group Yes
31.07424 34.70882
--- 당뇨의 유무에 따라 bmi의 차이가 있다고 결론
3) Paired t-test
예제데이터: anorexia
Treat: Factor of three levels: "Cont" (control), "CBT" (Cognitive Behavior treatment) and "FT" (family treatment).
Prewt: Weight of patient before study period, in lbs.
Postwt: Weight of patient after study period, in lbs.
# 서로 독립적인 두 집단의 평균을 비교 (평균이 같다/ 같지않다)
> attach(anorexia)
> FT <- subset(anorexia, Treat=='FT')
> head(FT)
Treat Prewt Postwt
56 FT 83.8 95.2
57 FT 83.3 94.3
58 FT 86.0 91.5
59 FT 82.5 91.9
60 FT 86.7 100.3
61 FT 79.6 76.7
> shapiro.test(FT$Prewt - FT$Postwt)
Shapiro-Wilk normality test
data: FT$Prewt - FT$Postwt
W = 0.9536, p-value = 0.5156
--- 정규분포를 따른다
R, T-test (R, T검정) End.
BioinformaticsAndMe
'R' 카테고리의 다른 글
pathview (패스웨이 분석) (2) | 2019.05.22 |
---|---|
R, 파일 입출력 (FILE I/O) (0) | 2018.09.11 |
R plot (그래픽스) (0) | 2018.08.27 |
R 회귀분석 (R regression test) (0) | 2018.08.19 |
R apply 함수 (0) | 2018.08.15 |