y = c(100, 96, 98, 96, 92, 95, 76, 80, 84, 84, 78, 82, 108, 100, 101, 98, 102, 100) 
company = rep(c("A", "B", "C"), each = 6)
data = data.frame(company, y)
head(data)
##   company   y
## 1       A 100
## 2       A  96
## 3       A  98
## 4       A  96
## 5       A  92
## 6       A  95
data
##    company   y
## 1        A 100
## 2        A  96
## 3        A  98
## 4        A  96
## 5        A  92
## 6        A  95
## 7        B  76
## 8        B  80
## 9        B  84
## 10       B  84
## 11       B  78
## 12       B  82
## 13       C 108
## 14       C 100
## 15       C 101
## 16       C  98
## 17       C 102
## 18       C 100
#1
#주어진 실험 설계에 대한 통계적 모형은 일원배치분산분석(One-Way ANOVA)입니다. 이 모형은 회사(A, B, C)라는 하나의 요인에 따른 건전지 수명의 차이를 분석하는 데 사용됩니다.
#yij = m + ai + eij
#여기서 yij는 i번째 회사에서 j번째 건전기의 수명, m은 전체평균, ai는 i번째 회사에 대한 효과 eij는 오차항이다.
#가정 : 정규성(각회사에 선택된 건전지수명은 정규분포를 따르는가), 등분산성(각 회사의 건전지 수명의 분산이 동일한가), 독립성(각 회사에서 선택된 건전지들은 서로 독립적인가), 모형의 선형성(회사간의 평균수명은 선형적으로 다른가) 

#2
library(ggplot2)
ggplot(data, aes(x=company, y=y)) +
  geom_boxplot(fill="lightblue") +
  labs(title="건전지 수명에 따른 회사별 상자그림", x="회사", y="건전지 수명")

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
summary_stats <- data %>%
  group_by(company) %>%
  summarise(mean_y = mean(y), sd_y = sd(y))

print(summary_stats)
## # A tibble: 3 × 3
##   company mean_y  sd_y
##   <chr>    <dbl> <dbl>
## 1 A         96.2  2.71
## 2 B         80.7  3.27
## 3 C        102.   3.45
#A 회사의 평균 수명은 약 96.2이며, 표준편차는 약 2.71입니다.
#B 회사의 평균 수명은 약 80.7이며, 표준편차는 약 3.27입니다.
#C 회사의 평균 수명은 약 102.이며, 표준편차는 약 3.45입니다.
#상자그림을 통해 b의 평균수명이 제일 낮을 것을 볼 수 있다. 또한 c의 평균수명이 제일 높은것을 알 수 있다. 표준편차를 통해 b,c의 비해 a가 수명의 변동성이 큰것을 알 수 있다. 

#3
# h0 : 모든 회사의 평균 수명이 같다 
#h1 : 적어도 한 회사의 평균수명은 다르다.
anova_result <- aov(y ~ company, data=data)
print(summary(anova_result))
##             Df Sum Sq Mean Sq F value   Pr(>F)    
## company      2 1405.4   702.7   70.43 2.37e-08 ***
## Residuals   15  149.7    10.0                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#회사에 대한 f통계량은 70.43이며, 이에대한 p-값은 2,37e-0.8이다. 유의수준을 0.05라고 했을때 p-값이 훨씬 작으므로 대립가설을 채택해야한다. 즉, 적어도 한 회사의 평균수명이 다른회사들과 다르다는 결론을 내릴 수 있다. 

#4
pairwise_result <- pairwise.t.test(data$y, data$company, p.adjust.method = "bonferroni")
print(pairwise_result)
## 
##  Pairwise comparisons using t tests with pooled SD 
## 
## data:  data$y and data$company 
## 
##   A       B      
## B 1.2e-06 -      
## C 0.031   2.5e-08
## 
## P value adjustment method: bonferroni
#a와 b, b와 c에 비해 a와 c의 p-값은 0.05보다는 작지만 낮은 유의성을 보인다. 0.01을 유의수준으로 잡는다면 a와 c의 건전지 평균수명은 차이가 없다고 볼 수 있다. 

#5
fit <- lm(y ~ company, data=data)
data$y_hat <- predict(fit)
data$residuals <- data$y - data$y_hat
plot(data$y_hat, data$residuals, xlab = "forecast", ylab = "residaul", main = "residaul vs. forecast")

#잔차가 예측값 주변에 균일하게 흩어져 있음으로 모델이 데이터를 잘 설명하고 있다고 볼 수 있습니다.


shapiro_test <- shapiro.test(data$residuals)
print(shapiro_test)
## 
##  Shapiro-Wilk normality test
## 
## data:  data$residuals
## W = 0.97107, p-value = 0.8176
#Shapiro-Wilk 검정은 잔차의 정규성을 검정하는 방법이다. 검정의 귀무가설 :  "잔차가 정규분포를 따른다".  p-값이 0.8176으로 유의수존 0.05보다 크므로 잔차가 정규분포를 따른다고 할 수 있다. 

bartlett_test <- bartlett.test(data$residuals ~ data$company)
print(bartlett_test)
## 
##  Bartlett test of homogeneity of variances
## 
## data:  data$residuals by data$company
## Bartlett's K-squared = 0.27759, df = 2, p-value = 0.8704
#다음으로, 등분산성을 확인하기 위해 Bartlett 검정을 사용할 수 있다. 이 검정은 그룹 간의 잔차 분산이 동일한지를 검정합니다. 귀무가설 :  "그룹 간의 잔차 분산이 동일하다". p값이 0.8704임으로 유의수준 0,05보다 크다. 따라서 그룹간의 잔차분산이 동일하다고 볼 수 있다.

#6 

data_A <- data[data$company == "A", ]$y
data_B <- data[data$company == "B", ]$y
data_C <- data[data$company == "C", ]$y

mean_A <- mean(data_A)
mean_B <- mean(data_B)

new_variable <- (mean_A + mean_B) / 2

mean_C <- mean(data_C)

t_test <- t.test(data_C, mu = new_variable, alternative = "two.sided")

print(t_test)
## 
##  One Sample t-test
## 
## data:  data_C
## t = 9.2901, df = 5, p-value = 0.0002431
## alternative hypothesis: true mean is not equal to 88.41667
## 95 percent confidence interval:
##   97.87983 105.12017
## sample estimates:
## mean of x 
##     101.5
#p-값이 0.0002431임으로 유의수준 0.05보다 작다. 따라서 a와 b의 평균은 c회사와 다르다는 것을 알 수 있다. 

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.