data(sleep)

#看看sleep的結構

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 ...

#看看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
library(moments)
## Warning: 套件 'moments' 是用 R 版本 4.1.1 來建造的

##睡眠時數增減的平均數

mean(sleep$extra)
## [1] 1.54

##睡眠時數增減的標準差

sd(sleep$extra)
## [1] 2.01792

##睡眠時數增減的峰度

kurtosis(sleep$extra)
## [1] 2.120121

##睡眠時數增減的偏態

skewness(sleep$extra)
## [1] 0.4172363

#看看sleep的基本統計量

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
my_summary <- function(x) {
  require(moments)
  funs <- c(mean, sd, skewness, kurtosis)
  sapply(funs, function(f) f(x, na.rm = TRUE))
}
my_summary(sleep$extra)
## [1] 1.5400000 2.0179197 0.4172363 2.1201214

##睡眠時數增減與用藥之間的關係

aggregate(extra ~ group, data = sleep, mean)
##   group extra
## 1     1  0.75
## 2     2  2.33
aggregate(extra ~ group, data = sleep, sd)
##   group    extra
## 1     1 1.789010
## 2     2 2.002249
aggregate(extra ~ group, data = sleep, kurtosis)
##   group    extra
## 1     1 2.098282
## 2     2 1.650653
aggregate(extra ~ group, data = sleep, skewness)
##   group     extra
## 1     1 0.4898753
## 2     2 0.3253398

##製圖看看藥物對睡眠時數增減的關係

#1為控制組;2為實驗組

library(lattice)
plot(sleep$extra,sleep$group)

boxplot(formula = extra ~ group, data = sleep, xlab = "是否用藥", ylab = "增減時數", frame.plot = FALSE, border = "red" , boxfill = "maroon", whisklty = 1)

library(car)
## Warning: 套件 'car' 是用 R 版本 4.1.3 來建造的
## 載入需要的套件:carData
## Warning: 套件 'carData' 是用 R 版本 4.1.3 來建造的
densityplot( ~ extra, 
             groups = group,
             data = sleep,
             auto.key = TRUE,
             lty = c(1, 2),
             plot.points = F,
             type = "g",
             xlab = "增減時數",
             main = "用藥的睡眠時數差異")

qq(group ~ extra, 
   data = sleep,
   type = c("p", "g"),
   pch = 23,
   aspect = 1
)

histogram( ~ extra | group, 
           data = sleep,
           type = "count",
           layout = c(1,2)
)

從圖表中可以看出,使用藥物之後,實驗組的睡眠時數較控制組增加,但是有無顯著的差異,需要後續進行t-test檢定兩者的統計關係。

#使用T檢定用藥對睡眠時數的影響

t.test(extra ~ group, data = sleep)
## 
##  Welch Two Sample t-test
## 
## data:  extra by group
## t = -1.8608, df = 17.776, p-value = 0.07939
## alternative hypothesis: true difference in means between group 1 and group 2 is not equal to 0
## 95 percent confidence interval:
##  -3.3654832  0.2054832
## sample estimates:
## mean in group 1 mean in group 2 
##            0.75            2.33
t.test(extra ~ group, data = sleep, var.equal = T)
## 
##  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

在上述的T檢定當中可以看出,95%的信賴區間為 -3.3654832~0.2054832。信賴區間包含0,無法拒絕虛無假設,所以用不用藥其實對睡眠時數沒有顯著的影響。