데이터 불러오기
library(MASS)
data(anorexia)
데이터 확인하기
summary(anorexia)
## Treat Prewt Postwt
## CBT :29 Min. :70.00 Min. : 71.30
## Cont:26 1st Qu.:79.60 1st Qu.: 79.33
## FT :17 Median :82.30 Median : 84.05
## Mean :82.41 Mean : 85.17
## 3rd Qu.:86.00 3rd Qu.: 91.55
## Max. :94.90 Max. :103.60
str(anorexia)
## 'data.frame': 72 obs. of 3 variables:
## $ Treat : Factor w/ 3 levels "CBT","Cont","FT": 2 2 2 2 2 2 2 2 2 2 ...
## $ Prewt : num 80.7 89.4 91.8 74 78.1 88.3 87.3 75.1 80.6 78.4 ...
## $ Postwt: num 80.2 80.1 86.4 86.3 76.1 78.1 75.1 86.7 73.5 84.6 ...
The anorexia dataset has information on 72 patients who got different treatments for anorexia. Some were in a control group with no special treatment, some had cognitive behavioral therapy (CBT), and others had family therapy (FT). The dataset includes each person’s weight before and after treatment. Most patients gained weight after treatment, and their starting weights were usually between 70 and 90 pounds.
데이터 형태 변경
ft_data <- subset(anorexia, Treat == "FT")
#확인
head(ft_data)
## 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
전후 체중 차이 계산
ft_data$weight_change <-ft_data$Postwt-ft_data$Prewt
히스토그램 그리기
library(ggplot2)
ggplot(ft_data, aes(x = weight_change)) +
geom_histogram(binwidth = 1, fill = "blue", alpha = 0.6, color = "black") +
labs(title = "Weight Change Distribution (FT Group)", x = "Weight Change", y = "Frequency") +
theme_minimal()
Treatment 전과 후의 weight 에 대한 정규성 검정을 수행
#관심 있는 변수는 "weight change" (Postwt - Prewt) 이므로, 정규성 검정은 weight_change에 대해서만 수행하면 됩니다.
shapiro.test(ft_data$weight_change)
##
## Shapiro-Wilk normality test
##
## data: ft_data$weight_change
## W = 0.95358, p-value = 0.5156
정규성 검정 결과 p = 0.5156 이므로 0.05보다 크기 때문에 귀무가설을 기각하지 않습니다. 즉, 데이터가 정규성을 따른다고 볼 수 있습니다.
또한, 같은 개체에서 치료 전후로 측정된 값이므로 대응표본(paired)입니다. 따라서, 대응표본 t-검정 (paired t-test)을 사용해야 합니다.
두 집단의 평균 비교에 대한 가설 검정을 5% 유의수준으로 진행
t.test(ft_data$Prewt, ft_data$Postwt, paired = TRUE, conf.level = 0.95)
##
## Paired t-test
##
## data: ft_data$Prewt and ft_data$Postwt
## t = -4.1849, df = 16, p-value = 0.0007003
## alternative hypothesis: true mean difference is not equal to 0
## 95 percent confidence interval:
## -10.94471 -3.58470
## sample estimates:
## mean difference
## -7.264706
Paired t-test 결과 p = 0.0007003, 즉 0.05보다 작기 때문에 null hypothesis을 기각합니다. 즉, alternative hypothesis을 채택하게 되므로, FT 치료 전후 체중 변화가 유의미하다고 해석할 수 있습니다.