# 2025-06-09

# 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
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
# 귀무가설: ctrl 그룹의 평균 무게는 5.0이다.
# 1. 데이터 불러오기
data("PlantGrowth")
# 2. ctrl 그룹 추출
ctrl <- subset(PlantGrowth, group == "ctrl")$weight
# 3. 일표본 t-검정 (mu = 5)
t.test(ctrl, mu = 5)
## 
##  One Sample t-test
## 
## data:  ctrl
## t = 0.17355, df = 9, p-value = 0.8661
## alternative hypothesis: true mean is not equal to 5
## 95 percent confidence interval:
##  4.614882 5.449118
## sample estimates:
## mean of x 
##     5.032
#1. 다음은 PlantGrowth 데이터에서 대조군(ctrl)과 실험군1(trt1)의 식물 무게에 대한 
#독립표본 t-검정 결과이다. 이 결과에 대한 해석으로 가장 적절한 것은?
  data("PlantGrowth")
# 대조군 vs 실험군1 비교
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. 데이터 불러오기
data(sleep)
# 2. 데이터 구조 확인
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
# 3. 독립표본 t-검정 수행
t_test_result <- t.test(extra ~ group, data = sleep, var.equal = TRUE)
# 4. 결과 출력
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
#p-value < 0.05 : 귀무가설 기각 → 평균 차이가 있다.
#p-value ≥ 0.05 : 귀무가설 채택 → 평균 차이가 없다
# sleep 데이터셋 사용
data(sleep)
# 대응표본 t-test 수행
# 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