#install.packages

#install.packages("PairedData")

#library

library(PairedData)
## Loading required package: MASS
## Loading required package: gld
## Loading required package: mvtnorm
## Loading required package: lattice
## Loading required package: ggplot2
## 
## Attaching package: 'PairedData'
## The following object is masked from 'package:base':
## 
##     summary

#load data

data(Anorexia, package="PairedData")

#exam data #show structure of data

str(Anorexia)
## 'data.frame':    17 obs. of  2 variables:
##  $ Prior: num  83.8 83.3 86 82.5 86.7 79.6 76.9 94.2 73.4 80.5 ...
##  $ Post : num  95.2 94.3 91.5 91.9 100.3 ...

1 show first 6 lines

head(Anorexia)
##   Prior  Post
## 1  83.8  95.2
## 2  83.3  94.3
## 3  86.0  91.5
## 4  82.5  91.9
## 5  86.7 100.3
## 6  79.6  76.7

2 compute variable means

colMeans(Anorexia)
##    Prior     Post 
## 83.22941 90.49412

患者之平均體重治療後大於治療前。

3 compute variable sd

print(apply(Anorexia, 2, sd), 3)
## Prior  Post 
##  5.02  8.48

治療前體重標準差小於治療後體重的標準差。

#Covariance

cov(Anorexia)
##          Prior     Post
## Prior 25.16721 22.88268
## Post  22.88268 71.82684

#Correlation

cor(Anorexia)
##          Prior     Post
## Prior 1.000000 0.538203
## Post  0.538203 1.000000

#治療前後體重變化

with(Anorexia, plot(paired(Prior, Post), type="profile")) +
 labs(x="Therapy", y="Weight (in lb)") +
 theme_bw()

3.1 2-sample t-test

t.test(Anorexia$Post, Anorexia$Prior)
## 
##  Welch Two Sample t-test
## 
## data:  Anorexia$Post and Anorexia$Prior
## t = 3.0414, df = 25.986, p-value = 0.005324
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##   2.35469 12.17472
## sample estimates:
## mean of x mean of y 
##  90.49412  83.22941

p-value = 0.005324 < 0.05,Reject Null hypothesis.

#檢定藥品是否有效,也就是檢定治療前後,同一患者平均體重是否有所不同

#paired t-test (配對樣本)

t.test.paired(Anorexia)
## 
##  Paired t-test
## 
## data:  Prior and Post
## t = -4.1849, df = 16, p-value = 0.0007003
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -10.94471  -3.58470
## sample estimates:
## mean of the differences 
##               -7.264706

經成對樣本t檢定結果顯示,患者治療後平均體重顯著高於治療前(p-value<0.001),即治療有效。

4 turn wide format to long format

dtaL <- tidyr::gather(Anorexia, key = "Therapy", value = "weights", Prior, Post)
ggplot(dtaL, aes(Therapy, weights)) + 
  geom_point(shape = 1,
             colour = "coral") + 
  stat_summary(fun.data = mean_cl_boot, 
               geom = "pointrange",
               colour = "deepskyblue4", 
               width = 0.5) + 
  coord_flip() +
  labs(x = "Therapy", y = "weights") +
  theme_bw()
## Warning: Ignoring unknown parameters: width

#Without subject effects

summary(lm(formula = weights ~ Therapy, data = dtaL))
## 
## Call:
## lm(formula = weights ~ Therapy, data = dtaL)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -15.294  -2.454   1.106   4.004  11.106 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)    90.494      1.689  53.578  < 2e-16 ***
## TherapyPrior   -7.265      2.389  -3.041  0.00467 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 6.964 on 32 degrees of freedom
## Multiple R-squared:  0.2242, Adjusted R-squared:    0.2 
## F-statistic:  9.25 on 1 and 32 DF,  p-value: 0.004673

#The End