Include your answers in this document in the sections below the rubric where I have point values indicated (1 p).

Answer the questions with the data examples.

The following data give the amount of sleep gained in hours from two sleep remedies, A and B, applied to 10 individuals who have trouble sleeping an adequate amount. Negative values imply sleep loss.

Let \(\mu_A\) = population mean sleep gain (among troubled sleepers) on remedy A, and \(\mu_B\) = population mean sleep gain (among troubled sleepers) on remedy B. Consider testing \(H_0: \mu_B - \mu_A = 0\) or equivalently \(\mu_D = 0\), where \(\mu_D = \mu_B - \mu_A\).

```
# Data
sleep <- read.table(text="
A B
0.7 1.9
-1.6 0.8
-0.2 1.1
-1.2 0.1
0.1 -0.1
3.4 4.4
3.7 5.5
0.8 1.6
0.0 4.6
2.0 3.0
", header = TRUE)
# calculate paired difference bewteen two remedies, D = B - A
sleep$D <- sleep$B - sleep$A
summary(sleep)
```

```
## A B D
## Min. :-1.60 Min. :-0.100 Min. :-0.200
## 1st Qu.:-0.15 1st Qu.: 0.875 1st Qu.: 1.000
## Median : 0.40 Median : 1.750 Median : 1.250
## Mean : 0.77 Mean : 2.290 Mean : 1.520
## 3rd Qu.: 1.70 3rd Qu.: 4.050 3rd Qu.: 1.675
## Max. : 3.70 Max. : 5.500 Max. : 4.600
```

A plot of both sleep remedies, with a 1-to-1 reference line to indicate which remedy is better for which people.

```
# determine range of each axis to use most extreme of each for square plot below
axis.lim <- range(c(sleep$A, sleep$B))
library(ggplot2)
# scatterplot of A and B sleep times, with 1:1 line
p <- ggplot(sleep, aes(x = A, y = B))
# draw a 1:1 line, dots above line indicate "B > A"
p <- p + geom_abline(intercept = 0, slope = 1, alpha = 0.4)
p <- p + geom_vline(xintercept = 0, alpha = 0.2)
p <- p + geom_hline(yintercept = 0, alpha = 0.2)
p <- p + geom_point()
p <- p + geom_rug()
# make the axes square so it's a fair visual comparison
p <- p + coord_equal()
p <- p + scale_x_continuous(limits=axis.lim)
p <- p + scale_y_continuous(limits=axis.lim)
p <- p + labs(title = "Sleep hours gained on two sleep remedies: A vs B")
print(p)
```

A plot of the paired differences.

```
library(ggplot2)
p1 <- ggplot(sleep, aes(x = D))
p1 <- p1 + scale_x_continuous(limits=c(-5,+5), breaks = seq(-6, 6, by=2))
p1 <- p1 + geom_histogram(aes(y=..density..), binwidth = 0.5)
p1 <- p1 + geom_density(alpha=0.1, fill="white", adjust = 2)
# vertical reference line at 0
p1 <- p1 + geom_vline(xintercept = 0, colour="red", linetype="dashed")
p1 <- p1 + geom_vline(xintercept = mean(sleep$D), colour="blue", alpha = 0.5)
p1 <- p1 + geom_rug()
p1 <- p1 + labs(title = "Difference of sleep hours gained: D = B - A")
print(p1)
```

**Hypothesis test**

- (2 p) ``’’
- $H_0: $ versus $H_A: $

- Let \(\alpha = 0.05\), the significance level of the test and the Type-I error probability if the null hypothesis is true.

```
# one-sample t-test of differences (paired t-test)
# t.summary <- t.test(sleep$D) # this gives the same result
t.summary <- t.test(sleep$B, sleep$A, paired = TRUE)
t.summary
```

```
##
## Paired t-test
##
## data: sleep$B and sleep$A
## t = 3.7796, df = 9, p-value = 0.004352
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## 0.610249 2.429751
## sample estimates:
## mean of the differences
## 1.52
```

(1 p) \(t_{s} = 99\).

(1 p) \(p = 99\), this is the observed significance of the test.

(2 p) Because \(p = 99\), …

For one- and two-sample \(t\)-tests, our assumptions are that we have a representative (random) sample from the population (not something that can be tested very well), the the sampling distributions of our test statistic \(t\) is normal.

Hidden in a code chunk are functions for one-sample and two-sample boostrap appoximations of the sampling distributions of the mean to compare with a normal distribution for the normality assumption.

**Check assumptions**of the test.

**Description of plots:** The top plot is of the data, a histogram with smoothed density curve. The bottom plot is a comparison of the boostrap appoximation of the sampling distributions of the mean. If the histogram and smooth density curve are *very* close (basically underneath) the red normal distribution, then we can say the sampling distribution is consistent with normality. If there is systematic bias (as below) where one side is over and the other side is under the red curve, then that’s evidence against normality, in which case we would pursue a nonparametric test (next week).

`bs.one.samp.dist(sleep$D)`

(2 p) Interpret the plots above.

Check the assumptions of the two-sample \(t\)-test for this example (explained in a previous worksheet).

`## No encoding supplied: defaulting to UTF-8.`

Here’s a plot of the data.

```
## If we create a summary data.frame with a similar structure as our data, then we
## can annotate our plot with those summaries.
# calculate the estimated mean and order M then F
est.mean <- as.numeric(by(dat.UN.Africa$UN_Percentage, dat.UN.Africa$PrimingNumber, mean))
# combine true US mean with our estimated mean
mean.UN.Africa <- data.frame(PrimingNumber = unique(dat.UN.Africa$PrimingNumber)
, UN_Percentage = est.mean)
#mean.UN.Africa
# summary
by(dat.UN.Africa$UN_Percentage, dat.UN.Africa$PrimingNumber, summary)
```

```
## dat.UN.Africa$PrimingNumber: 10
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 2.00 8.00 15.00 15.32 20.00 35.00
## --------------------------------------------------------
## dat.UN.Africa$PrimingNumber: 65
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 5.00 16.50 25.00 26.19 34.00 58.00
```

```
# histogram using ggplot
p <- ggplot(dat.UN.Africa, aes(x = UN_Percentage))
p <- p + geom_histogram(binwidth = 4)
p <- p + geom_rug()
p <- p + geom_vline(data = mean.UN.Africa, aes(xintercept = UN_Percentage), colour = "red")
p <- p + facet_grid(PrimingNumber ~ .)
print(p)
```

*A priori*, before we observed the data, we hypothesized that those who were primed with a larger number (65) would provide a higher percentage (`UN_Percentage`

) than those with the lower number (10). Therefore, this is a one-sided test.

```
# two-sample t-test
t.summary.UN <- t.test(UN_Percentage ~ PrimingNumber, data = dat.UN.Africa
, alternative = "less")
t.summary.UN
```

```
##
## Welch Two Sample t-test
##
## data: UN_Percentage by PrimingNumber
## t = -3.4988, df = 46.26, p-value = 0.0005225
## alternative hypothesis: true difference in means is less than 0
## 95 percent confidence interval:
## -Inf -5.6528
## sample estimates:
## mean in group 10 mean in group 65
## 15.32000 26.18519
```

**Check assumptions**of the test.

```
bs.two.samp.diff.dist(subset(dat.UN.Africa, PrimingNumber == "10")$UN_Percentage
, subset(dat.UN.Africa, PrimingNumber == "65")$UN_Percentage)
```

(2 p) Interpret the plots above.