반응형

/*******************************************************************************************************************
-- Title : [R.3.3] t-test를 통한 그룹간 평균 유의미 분석
-- Reference : http://www.dodomira.com/2016/04/02
-- Key word : t-test 독립표본 대응표본 단일표본 var.test p-value mtcars 
*******************************************************************************************************************/

-- Chart

-- R

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# ********************************************
# -- t-test 유형
# ********************************************
#    A. 독립 표본 t-test: 서로 다른 두개의 그룹 간의 평균 비교
#       (예: 남자와 여자 간 소득의 차이 비교)
#
#    B. 대응 표본 t-test : 하나의 집단에 대한 비교
#       (예: 과외를 하기 전과 후의 반 학생들의 성적 변화)
#
#    C. 단일 표본 t-test : 특정 집단의 평균이 어떤 숫자와 같은지 다른지를 비교
 
 
# ********************************************
# -- 독립 표본 t-test
# ********************************************
#    t-test를 실시하기 위해서 등분산성, 정규성이 만족되어야 함
#    이를 위해 var.test 검증 필요
 
# ------------------------------
# -- mtcars 데이터 확인
# ------------------------------
#    기어종류(am)에 따른 mpg의 차이가 통계적으로 유의한지 t-test 검증 
str(mtcars)
head(mtcars)
 
write.csv(mtcars,"e:\\mtcars.csv", row.names=TRUE)
 
# ------------------------------
# -- 등분산성 테스트
# ------------------------------
var.test(mtcars[mtcars$am==1,1], mtcars[mtcars$am==0,1])
 # F test to compare two variances
 
 # data:  mtcars[mtcars$am == 1, 1] and mtcars[mtcars$am == 0, 1]
 # F = 2.5869, num df = 12, denom df = 18, p-value = 0.06691
 # alternative hypothesis: true ratio of variances is not equal to 1
 # 95 percent confidence interval:
 #     0.934280 8.040391
 # sample estimates:
 #     ratio of variances 
 # 2.586911 
 # 해석) p-value(0.06691) < 0.05가 거짓이니 귀무가설 기각
 #       귀무가설(등분산성이 다르다.)
 
# ------------------------------
# -- 유형 1 문법: t.test(group 1의 관측치, group2의 관측치, t-test 유형, 신뢰범위) 
# ------------------------------
t.test(mtcars[mtcars$am==01], mtcars[mtcars$am==11],  
       paired = FALSE, var.equal = TRUE, conf.level = 0.95)
 # Two Sample t-test
 
 # data:  mtcars[mtcars$am == 0, 1] and mtcars[mtcars$am == 1, 1]
 # t = -4.1061, df = 30, p-value = 0.000285
 # alternative hypothesis: true difference in means is not equal to 0
 # 95 percent confidence interval:
 #     -10.84837  -3.64151
 # sample estimates:
 # mean of x mean of y 
 #     17.14737  24.39231 
 
# ------------------------------
# -- 유형 2 문법: t.test(관측치~집단 구분 기준, 데이터프레임, t-test 유형, 신뢰범위)
# ------------------------------
t.test(mpg ~ am, data=mtcars, var.equal=TRUE, conf.level = 0.95
 # 결과는 유형 1 문법과 동일
 
# ------------------------------
# -- 해석
# ------------------------------
#    두 집단별 mpg 평균은 오토 17.14, 수동 24.39 차이
#    p-value(0.000285) < 0.05가 참이므로 유의함(귀무가설 기각)
#    그럼 귀무가설은 '두 집단 평균이 유의미하지 않다' 인가?
 
 
# ********************************************
# -- 대응 표본 t-test
# ********************************************
mid = c(16202122232227252728)
final = c(19222424252526262832)
 
t.test(mid, final, paired=TRUE)
 #     Paired t-test
 
 # data:  mid and final
 # t = -4.4721, df = 9, p-value = 0.00155
 # alternative hypothesis: true difference in means is not equal to 0
 # 95 percent confidence interval:
 #  -3.0116674 -0.9883326
 # sample estimates:
 # mean of the differences 
 #                      -2 
 # 해석) p-value(0.00155) < 0.05가 참 이므로 유의함.
 
 
# ********************************************
# -- 단일 표본 t-test 
# ********************************************
 
# ------------------------------
# -- 문법
# ------------------------------
#    t.test(관측치, alternative = 판별 방향, mu=특정기준, conf.level = 신뢰수준)
#    alternative : greater(큰지), less(작은지), two.sided(같은지)
 
# -- 기말고사 평균
mean(final)                                           # 25.1
 
# -- 이 학생들의 기말고사 점수가 24점 보다 유의하게 높은가 테스트
t.test(final, alternative="greater", mu=24, conf.level = .95)
 #     One Sample t-test
 
 # data:  final
 # t = 1.0093, df = 9, p-value = 0.1696
 # alternative hypothesis: true mean is greater than 24
 # 95 percent confidence interval:
 #  23.10218      Inf
 # sample estimates:
 # mean of x 
 #      25.1 
 # 해석) p-value(0.1696) < 0.05가 거짓이므로 귀무가설 인정
 #       그럼 귀무가설은 "학생들 점수가 24점보다 높지 않다"?
 
 
 
 
 
 
cs

반응형

+ Recent posts