# 1. 데이터 로드 (내장 데이터셋)
data(chickwts)
# 2. chickwts$weight 벡터 요약
summary(chickwts$weight)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 108.0 204.5 258.0 261.3 323.5 423.0
length(chickwts$weight) # 관측치 수 확인
## [1] 71
# 3. 일표본 t-검정: 평균이 260인지 검정 (양측 검정)
t.test(chickwts$weight, mu = 260)
##
## One Sample t-test
##
## data: chickwts$weight
## t = 0.14137, df = 70, p-value = 0.888
## alternative hypothesis: true mean is not equal to 260
## 95 percent confidence interval:
## 242.8301 279.7896
## sample estimates:
## mean of x
## 261.3099
# weight: 오른쪽 꼬리분포 아님 왼쪽 꼬리분포입니까?
# mean>median 성립하면 오른쪽 꼬리 분포
summary(chickwts$weight)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 108.0 204.5 258.0 261.3 323.5 423.0
length(chickwts$weight) # 관측치 수 확인
## [1] 71
t.test(chickwts$weight, mu = 260)
##
## One Sample t-test
##
## data: chickwts$weight
## t = 0.14137, df = 70, p-value = 0.888
## alternative hypothesis: true mean is not equal to 260
## 95 percent confidence interval:
## 242.8301 279.7896
## sample estimates:
## mean of x
## 261.3099
data(sleep)
head(sleep)
## extra group ID
## 1 0.7 1 1
## 2 -1.6 1 2
## 3 -0.2 1 3
## 4 -1.2 1 4
## 5 -0.1 1 5
## 6 3.4 1 6
summary(sleep)
## extra group ID
## Min. :-1.600 1:10 1 :2
## 1st Qu.:-0.025 2:10 2 :2
## Median : 0.950 3 :2
## Mean : 1.540 4 :2
## 3rd Qu.: 3.400 5 :2
## Max. : 5.500 6 :2
## (Other):8
# extra : mean>median 크기 때문에 오른쪽 꼬리 분포입니다.
# group : 범주형 변수입니다. 데이터 크기 20입니다.
# 2. group 1만 추출
group1 <- subset(sleep, group == 1)$extra
# 3. 단일표본 t-검정 수행 (모평균 1.5와 비교)
t.test(group1, mu = 1.5)
##
## One Sample t-test
##
## data: group1
## t = -1.3257, df = 9, p-value = 0.2176
## alternative hypothesis: true mean is not equal to 1.5
## 95 percent confidence interval:
## -0.5297804 2.0297804
## sample estimates:
## mean of x
## 0.75
group_ctrl <- subset(PlantGrowth, group == "ctrl")$weight
group_trt1 <- subset(PlantGrowth, group == "trt1")$weight
# 독립표본 t-검정
t.test(group_ctrl, group_trt1, var.equal = TRUE)
##
## Two Sample t-test
##
## data: group_ctrl and group_trt1
## t = 1.1913, df = 18, p-value = 0.249
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.2833003 1.0253003
## sample estimates:
## mean of x mean of y
## 5.032 4.661
# 1. 데이터 구조 확인
str(sleep)
## 'data.frame': 20 obs. of 3 variables:
## $ extra: num 0.7 -1.6 -0.2 -1.2 -0.1 3.4 3.7 0.8 0 2 ...
## $ group: Factor w/ 2 levels "1","2": 1 1 1 1 1 1 1 1 1 1 ...
## $ ID : Factor w/ 10 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9 10 ...
# 'data.frame': 20 obs. of 3 variables:
# $ extra : num 0.7 -1.6 -0.2 -1.2 -0.1 3.4 3.7 0.8 0 2 ...
# $ group : Factor w/ 2 levels "1","2": 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2
# $ ID : int 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10
# 2. 독립표본 t-검정 수행
t_test_result <- t.test(extra ~ group, data = sleep, var.equal = TRUE)
# 3. 결과 출력
print(t_test_result)
##
## Two Sample t-test
##
## data: extra by group
## t = -1.8608, df = 18, p-value = 0.07919
## alternative hypothesis: true difference in means between group 1 and group 2 is not equal to 0
## 95 percent confidence interval:
## -3.363874 0.203874
## sample estimates:
## mean in group 1 mean in group 2
## 0.75 2.33
# sleep 데이터: group 1, group 2 비교
group1 <- subset(sleep, group == 1)$extra
group2 <- subset(sleep, group == 2)$extra
# 대응표본 t-검정 (paired = TRUE)
t.test(group1, group2, paired = TRUE)
##
## Paired t-test
##
## data: group1 and group2
## t = -4.0621, df = 9, p-value = 0.002833
## alternative hypothesis: true mean difference is not equal to 0
## 95 percent confidence interval:
## -2.4598858 -0.7001142
## sample estimates:
## mean difference
## -1.58
# 전체 관측치 수는 20이다"*라고 말할
# 수 있습니다, 단 “쌍으로 이루어진 대응표본
# t-검정”이라는 전제가 명확할 때만입니다.
# 왜 20이라고 할 수 있는가? 실험 참가자 수:
# n = df + 1 = 9 + 1 = 10
# 각 참가자는 두 조건에서 각각 한 번씩
# 측정됨 (예: group 1 vs group 2)