59doit
[ R ] ch 13 연습문제 본문
1. 교육 방법에 따라 시험성적에 차이가 있는지 검정하시오( 두 집단 평균 차이 검정)
#1) 데이터셋: twomethod.csv
#2) 변수: method(교육방법), score(시험성적)
#3) 모델: 교육방법(명목) -> 시험성적(비율)
#4) 전처리, 결측치 제거
(1) 파일불러오기
data <- read.csv("C:/twomethod.csv", header = TRUE) head(data) # id method score # 1 1 1 27 # 2 2 1 5 # 3 3 1 21 # 4 4 1 NA # 5 5 1 14 # 6 6 1 23 summary(data) # id method score # Min. : 1.0 Min. :1.000 Min. : 5.00 # 1st Qu.:16.5 1st Qu.:1.000 1st Qu.:18.00 # Median :32.0 Median :2.000 Median :26.00 # Mean :32.0 Mean :1.619 Mean :24.28 # 3rd Qu.:47.5 3rd Qu.:2.000 3rd Qu.:31.00 # Max. :63.0 Max. :2.000 Max. :45.00 # NA's :6 |
(2) subset , 데이터 전처리
result <- subset(data,!is.na(score),c(method,score)) result # method score # 1 1 27 # 2 1 5 # 3 1 21 # 5 1 14 # 6 1 23 # 7 1 20 # 8 1 9 # 9 1 28 # 10 1 15 # 11 1 29 # 13 1 9 # 14 1 5 # 15 1 19 # 16 1 13 # 17 1 10 # 18 1 29 # 19 1 5 # 20 1 28 # 21 1 6 # 22 1 20 # 23 1 17 # 24 1 9 # 25 2 19 # 26 2 45 # 27 2 21 # 28 2 37 # 29 2 26 # 30 2 26 # 31 2 24 # 32 2 37 # 33 2 34 # 34 2 44 # 35 2 39 # 36 2 20 # 38 2 15 # 39 2 41 # 40 2 38 # 41 2 21 # 42 2 26 # 43 2 26 # 44 2 22 # 46 2 31 # 47 2 34 # 48 2 27 # 49 2 21 # 50 2 32 # 51 2 35 # 52 2 35 # 53 2 29 # 54 2 43 # 55 2 18 # 56 2 15 # 58 2 30 # 59 2 23 # 60 2 32 # 62 2 28 # 63 2 29 |
(3) 데이터분리 교육방법&시험성적
a <- subset(result, method == 1) ; a # method score # 1 1 27 # 2 1 5 # 3 1 21 # 5 1 14 # 6 1 23 # 7 1 20 # 8 1 9 # 9 1 28 # 10 1 15 # 11 1 29 # 13 1 9 # 14 1 5 # 15 1 19 # 16 1 13 # 17 1 10 # 18 1 29 # 19 1 5 # 20 1 28 # 21 1 6 # 22 1 20 # 23 1 17 # 24 1 9 b <- subset(result, method == 2) ; b # method score # 25 2 19 # 26 2 45 # 27 2 21 # 28 2 37 # 29 2 26 # 30 2 26 # 31 2 24 # 32 2 37 # 33 2 34 # 34 2 44 # 35 2 39 # 36 2 20 # 38 2 15 # 39 2 41 # 40 2 38 # 41 2 21 # 42 2 26 # 43 2 26 # 44 2 22 # 46 2 31 # 47 2 34 # 48 2 27 # 49 2 21 # 50 2 32 # 51 2 35 # 52 2 35 # 53 2 29 # 54 2 43 # 55 2 18 # 56 2 15 # 58 2 30 # 59 2 23 # 60 2 32 # 62 2 28 # 63 2 29 a1 <- a$score ;a1 # [1] 27 5 21 14 23 20 9 28 15 29 9 5 19 13 10 29 5 28 6 20 17 9 b1 <- b$score ;b1 # [1] 19 45 21 37 26 26 24 37 34 44 39 20 15 41 38 21 26 26 22 31 34 27 21 # [24] 32 35 35 29 43 18 15 30 23 32 28 29 |
- 동질성 검정 (등분산성검정)
var.test(a1, b1) # p-value = 0.8494 분포와 차이가 없다. # F test to compare two variances # # data: a1 and b1 # F = 1.0648, num df = 21, denom df = 34, p-value = 0.8494 # alternative hypothesis: true ratio of variances is not equal to 1 # 95 percent confidence interval: # 0.502791 2.427170 # sample estimates: # ratio of variances # 1.06479 |
p-value=0.8494 > 0.05 이므로 귀무가설(H0:분산 차이가 없다)을 채택
# ratio of variances 는 1.06479임을 확인 할 수 있다.
- 평균차이검정 t검정
t.test( a1, b2) # p-value = 1.303e-06 # p-value가 0.05보다 작으니 통계적으로 유의하다 # Welch Two Sample t-test # # data: a1 and b2 # t = -5.6056, df = 43.705, p-value = 1.303e-06 # alternative hypothesis: true difference in means is not equal to 0 # 95 percent confidence interval: # -17.429294 -8.209667 # sample estimates: # mean of x mean of y # 16.40909 29.22857 t.test(a1, b2, alter="greater", conf.int=TRUE, conf.level=0.95) #p-value = 1 # Welch Two Sample t-test # # data: a1 and b2 # t = -5.6056, df = 43.705, p-value = 1 # alternative hypothesis: true difference in means is greater than 0 # 95 percent confidence interval: # -16.66255 Inf # sample estimates: # mean of x mean of y # 16.40909 29.22857 t.test(b2, a1, alter="greater", conf.int=TRUE, conf.level=0.95) #p-value=6.513e-07 # Welch Two Sample t-test # # data: b2 and a1 # t = 5.6056, df = 43.705, p-value = 6.513e-07 # alternative hypothesis: true difference in means is greater than 0 # 95 percent confidence interval: # 8.976413 Inf # sample estimates: # mean of x mean of y # 29.22857 16.40909 |
모수검정 방법 수행 -> t검정
## 결론 ; 검정통계량 / 유의확률
# t = -5.6056, df = 43.705, / p-value = 1.303e-06
#유의수준 0.05보다작다 -> 귀무가설이 기각 -> 교육방법에 따른 평균에 차이가 있다.
### b2 교육 방법이 a1 교육방법 보다 시험성적이 더 좋다.
2. 대학에 진학한 남학생과 여학생을 대상으로 진학한 대학에 대해서 만족도에 차이가 있는가를 검정하시오.
(두 집단 비율 차이 검정)
# 귀무가설 : 만족도차이가 없다.
# 연구가설 : 만족도차이가 있다.
#1) 데이터셋: two_sample.csv
#2) 변수: gender(1,2), survey(0, 1)
(1)파일불러오기
data <- read.csv("C:/two_sample.csv", header = TRUE) head(data) # no gender method survey score # 1 1 1 1 1 5.1 # 2 2 1 1 0 5.2 # 3 3 1 1 1 4.7 # 4 4 2 1 0 4.8 # 5 5 1 1 1 5.0 # 6 6 1 1 1 5.4 |
(2) subset , 데이터 전처리
data <- subset(data, !is.na(data$survey)) gender <- data$gender survey <- data$survey |
(3) 교차테이블확인
table(gender) table(survey) table(gender,survey, useNA="ifany") |
(4) 비율차이검증
prop.test(c(138,107),c(174,126)) #p-value = 0.2765 |
prop.test(c(138,107),c(174,126), alternative="two.sided", conf.level=0.95) |
# 2-sample test for equality of proportions with continuity correction # # data: c(138, 107) out of c(174, 126) # X-squared = 1.1845, df = 1, p-value = 0.2765 # alternative hypothesis: two.sided # 95 percent confidence interval: # -0.14970179 0.03749599 # sample estimates: # prop 1 prop 2 # 0.7931034 0.8492063 |
# 결론: p-value = 0.2765
## 유의수준 0.05보다크다 -> 귀무가설 채택 -> 남학생과 여학생의 만족도 차이가 없다.
3. 우리나라 전체 중학교 2 학년 여학생 평균 키가 148.5cm 로 알려진 상태에서 A 중학교 2 학년 전체 500 명을 대상으로 10%인 50 명을 표본으로 선정하여 표본평균 신장을 계산하고 모집단의 평균과 차이가 있는 지를 단계별로 분석을 수행하여 검정하시오.
(1) 데이터가져오기
stheight <- read.csv("C:/student_height.csv", header = TRUE) head(stheight) # sudent.id height # 1 1 148 # 2 2 150 # 3 3 149 # 4 4 144 # 5 5 152 # 6 6 150 |
(2) height <- stheight$height
height <- stheight$height head(height) # [1] 148 150 149 144 152 150 |
(3) 기술통계량 평균 계산
summary(height) # Min. 1st Qu. Median Mean 3rd Qu. Max. # 140.0 147.0 150.0 149.4 151.0 165.0 x <- na.omit(height) mean(x) # 149.4 |
(4) 정규성 검정
shapiro.test(x) # p-value = 0.0001853 -> 정규분포 X # Shapiro-Wilk normality test # # data: x # W = 0.88711, p-value = 0.0001853 |
# 귀무가설(H0:정규분포를 따른다)기각
# 정규 분포(모수검정) - t.test()
# 비정규 분포(비모수검정) - wilcox.test()
따라서 정규분포가 아니므로 비모수 검정인 wilcox.text 시행
(5) 가설 검정
wilcox.test(x, mu = 148.5) #p-value = 0.067 |
wilcox.test(x, mu=148.5, alter ="two.side", conf.level=0.95) #p-value = 0.067 |
# Wilcoxon signed rank test with continuity correction # # data: x # V = 826, p-value = 0.067 # alternative hypothesis: true location is not equal to 148.5 |
#결론 : p-value = 0.067
##유의수준 0.05보다크다 -> 귀무가설 채택 -> 표본평균과 모집단의 평균148.5와 차이가 없다
4. 중소기업에서 생산한 HDTV 판매율을 높이기 위해서 프로모션을 진행한 결과 기존 구매비율보다 15% 향상되었는지를 단계별로 분석을 수행하여 검정하시오.
#귀무가설(H0): 기존구매 비율과 차이가 없다.
#연구가설(H1): 기존구매 비율과 차이가 있다
#1) 구매여부 변수: buy (1: 구매하지 않음, 2: 구매)
#2) 데이터셋: hdtv.csv
#3) 빈도수와 비율 계산
(1) 데이터 가져오기
data <- read.csv("C:/hdtv.csv", header = TRUE) head(data) # user.id buy # 1 1 2 # 2 2 1 # 3 3 1 # 4 4 1 # 5 5 2 # 6 6 2 |
(2) 구매여부 변수
data$buy2 <- factor(data$buy, levels = c(1, 2), labels = c('구매하지 않음', '구매')) data$buy2 # [1] 구매 구매하지 않음 구매하지 않음 구매하지 않음 구매 # [6] 구매 구매하지 않음 구매하지 않음 구매하지 않음 구매하지 않음 # [11] 구매하지 않음 구매하지 않음 구매하지 않음 구매하지 않음 구매 # [16] 구매하지 않음 구매하지 않음 구매하지 않음 구매하지 않음 구매하지 않음 # [21] 구매하지 않음 구매하지 않음 구매하지 않음 구매하지 않음 구매하지 않음 # [26] 구매 구매하지 않음 구매하지 않음 구매하지 않음 구매하지 않음 # [31] 구매 구매하지 않음 구매 구매 구매하지 않음 # [36] 구매하지 않음 구매하지 않음 구매하지 않음 구매하지 않음 구매하지 않음 # [41] 구매하지 않음 구매하지 않음 구매 구매하지 않음 구매하지 않음 # [46] 구매하지 않음 구매하지 않음 구매 구매하지 않음 구매하지 않음 # Levels: 구매하지 않음 구매 |
(3) 빈도수와 비율 계산
x <- data$buy2 freq(x) # Frequencies for x # 구매하지 않음 구매 NA # 40 10 0 # % 80 20 0 # %!NA 80 20 table(x) # x # 구매하지 않음 구매 # 40 10 |
(4) 가설 검정
- 양측검정
binom.test(10,40, p=0.15, conf.level = 0.95) # p-value = 0.1158 binom.test(10,40, p=0.15, alternative="two.sided", conf.level=0.95) # p-value = 0.1158 |
Exact binomial test data: 10 and 40 number of successes = 10, number of trials = 40, p-value = 0.1158 alternative hypothesis: true probability of success is not equal to 0.15 95 percent confidence interval: 0.1269148 0.4119620 sample estimates: probability of success 0.25 |
- 단측검정
binom.test(10,40, p=0.15, alternative = c('greater'), conf.level = 0.95) # p-value = 0.06722 # Exact binomial test # # data: 10 and 40 # number of successes = 10, number of trials = 40, p-value = 0.06722 # alternative hypothesis: true probability of success is greater than 0.15 # 95 percent confidence interval: # 0.1423699 1.0000000 # sample estimates: # probability of success # 0.25 binom.test(10,40, p=0.15, alternative = c('less'), conf.level = 0.95) # p-value = 0.9701 # Exact binomial test # # data: 10 and 40 # number of successes = 10, number of trials = 40, p-value = 0.9701 # alternative hypothesis: true probability of success is less than 0.15 # 95 percent confidence interval: # 0.0000000 0.3870602 # sample estimates: # probability of success # 0.25 |
#결론
## 방향성 단측가설은 모두 p-value가 0.05보다 크므로 기각된다.
## 유의수준 0.05보다크다 -> 귀무가설 채택 -> 기존구매 비율과 차이가 없다.
*** review ; 양측검정에서 크다고 나왔으므로 단측검정 할 필요는 없다.!
'Q.' 카테고리의 다른 글
[ R ] 탐색적데이터 예제 평가(6) (0) | 2022.11.25 |
---|---|
[ R ] 교차분석과 카이제곱 연습문제 ch12 (0) | 2022.11.24 |
[ R ] 연습문제 (6) 6-> ch7 (0) | 2022.11.22 |
[ R ]연습문제 (5) 5->6 (0) | 2022.11.20 |
[ R ] 연습문제 (4) (0) | 2022.11.18 |