Solutions to exercises on paired t-test found here

```
####################
# #
# Exercise 1 #
# #
####################
#Load package MASS
library(MASS)
#attach anorexia data so that variables are easily accessible
attach(anorexia)
#Inspect structure of the data
head(anorexia)
```

```
## Treat Prewt Postwt
## 1 Cont 80.7 80.2
## 2 Cont 89.4 80.1
## 3 Cont 91.8 86.4
## 4 Cont 74.0 86.3
## 5 Cont 78.1 76.1
## 6 Cont 88.3 78.1
```

```
####################
# #
# Exercise 2 #
# #
####################
#descriptive statistics on weight before treatment
#You need to install package psych
library(psych)
```

`## Warning: package 'psych' was built under R version 3.3.1`

`describe(anorexia$Prewt)`

```
## vars n mean sd median trimmed mad min max range skew kurtosis
## X1 1 72 82.41 5.18 82.3 82.47 5.49 70 94.9 24.9 -0.05 -0.16
## se
## X1 0.61
```

```
####################
# #
# Exercise 3 #
# #
####################
#descriptive statistics on weight after treatment
describe(anorexia$Postwt)
```

```
## vars n mean sd median trimmed mad min max range skew kurtosis
## X1 1 72 85.17 8.04 84.05 84.82 9.56 71.3 103.6 32.3 0.36 -0.81
## se
## X1 0.95
```

```
####################
# #
# Exercise 4 #
# #
####################
#create a new variable containing differences
weight.differences = Postwt - Prewt
####################
# #
# Exercise 5 #
# #
####################
#create a boxplot to identify any outliers in our data
boxplot(weight.differences,main = "Boxplot of weight differences before and after treatment",ylab = "weight differences",col = "green")
```

```
####################
# #
# Exercise 6 #
# #
####################
#Create a histogram to visually assess normality
#In exercise 5 we used base graphics to produce a boxplot
#A more flexible way of data visualization is using package ggplot
#Install package ggplot if you have not
#load ggplot
library(ggplot2)
```

`## Warning: package 'ggplot2' was built under R version 3.3.1`

```
##
## Attaching package: 'ggplot2'
```

```
## The following objects are masked from 'package:psych':
##
## %+%, alpha
```

```
#attach weight.differences to anorexia data frame
anorexia$weight.differences = weight.differences
#Create a histogram with a density curve to visually inspect normality
ggplot(anorexia,aes(x=weight.differences)) + geom_histogram(aes(y=..density..),binwidth = 0.9) + stat_function(fun = dnorm, colour = "blue",args = list(mean = mean(anorexia$weight.differences), sd = sd(anorexia$weight.differences))) + scale_x_continuous(name="Weight differences") + ggtitle("Histogram of weight differences before and after anorexia treatment")
```

```
####################
# #
# Exercise 7 #
# #
####################
#Test if the weight differences are normally distributed
shapiro.test(weight.differences)
```

```
##
## Shapiro-Wilk normality test
##
## data: weight.differences
## W = 0.97466, p-value = 0.1544
```

```
####################
# #
# Exercise 8 #
# #
####################
#Perform a power analysis to check the sample size has adequate power to detect a difference if it exists
#install package pwr and load it
library(pwr)
```

`## Warning: package 'pwr' was built under R version 3.3.1`

`pwr.t.test(n=72,d=0.5,sig.level = 0.05,type = c("paired"))`

```
##
## Paired t test power calculation
##
## n = 72
## d = 0.5
## sig.level = 0.05
## power = 0.9869471
## alternative = two.sided
##
## NOTE: n is number of *pairs*
```

```
####################
# #
# Exercise 9 #
# #
####################
#Perform a paired t test
t.test(Postwt,Prewt,paired = TRUE)
```

```
##
## Paired t-test
##
## data: Postwt and Prewt
## t = 2.9376, df = 71, p-value = 0.004458
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## 0.8878354 4.6399424
## sample estimates:
## mean of the differences
## 2.763889
```

```
####################
# #
# Exercise 10 #
# #
####################
#Interpret results
#All assumptions required were satisfied
#There were no outliers, data was normally distributed and the t test had adequate power
#The difference in weight before and after treatment was statistically significant at 5% LOs.
```

Matt says

I assumed that the analysis would be within Treatment, and also created the new variable in the existing dataframe…so here is my answer to Ex 6:

anorexia$wtDiff % ggplot(aes(wtDiff, group=Treat, fill=Treat)) +

geom_histogram(aes(y=..density..), binwidth=1, fill=’grey60′) +

geom_density(alpha=0.5) +

facet_grid(.~Treat) + theme_bw()+ theme(legend.position=”none”) +

xlab(“Weight Difference on Treatment (Stone)”)

*also assumed unit of measurement in Stones 😉

Matt says

my code keeps getting dropped. trying from Chrome instead of Explorer.

Ex 7: Test for normality within Treatment groups:

anorexia$wtDiff % group_by(Treat) %>% do(broom::tidy(shapiro.test(.$wtDiff)))

Matt says

i give up