## B.6 Two means (paired samples)

### Problem statement

Trace metals in drinking water affect the flavor and an unusually high concentration can pose a health hazard. Ten pairs of data were taken measuring zinc concentration in bottom water and surface water at 10 randomly selected locations on a stretch of river. Do the data suggest that the true average concentration in the surface water is smaller than that of bottom water? (Note that units are not given.) [Tweaked a bit from https://onlinecourses.science.psu.edu/stat500/node/51]

### B.6.1 Competing hypotheses

#### In words

• Null hypothesis: The mean concentration in the bottom water is the same as that of the surface water at different paired locations.
• Alternative hypothesis: The mean concentration in the surface water is smaller than that of the bottom water at different paired locations.

#### In symbols (with annotations)

• $$H_0: \mu_{diff} = 0$$, where $$\mu_{diff}$$ represents the mean difference in concentration for surface water minus bottom water.
• $$H_A: \mu_{diff} < 0$$

#### Set $$\alpha$$

It’s important to set the significance level before starting the testing using the data. Let’s set the significance level at 5% here.

### B.6.2 Exploring the sample data

zinc_tidy <- read_csv("https://moderndive.com/data/zinc_tidy.csv")

We want to look at the differences in surface - bottom for each location:

zinc_diff <- zinc_tidy %>%
group_by(loc_id) %>%
summarize(pair_diff = diff(concentration)) %>%
ungroup()

Next we calculate the mean difference as our observed statistic:

d_hat <- zinc_diff %>%
specify(response = pair_diff) %>%
calculate(stat = "mean")
d_hat
# A tibble: 1 x 1
stat
<dbl>
1 -0.0804

The histogram below also shows the distribution of pair_diff.

ggplot(zinc_diff, aes(x = pair_diff)) +
geom_histogram(binwidth = 0.04, color = "white")

We are looking to see if the sample paired mean difference of -0.08 is statistically less than 0. They seem to be quite close, but we have a small number of pairs here. Let’s guess that we will fail to reject the null hypothesis.

#### Bootstrapping for hypothesis test

In order to look to see if the observed sample mean difference $$\bar{x}_{diff} = -0.08$$ is statistically less than 0, we need to account for the number of pairs. We also need to determine a process that replicates how the paired data was selected in a way similar to how we calculated our original difference in sample means.

Treating the differences as our data of interest, we next use the process of bootstrapping to build other simulated samples and then calculate the mean of the bootstrap samples. We hypothesize that the mean difference is zero.

This process is similar to comparing the One Mean example seen above, but using the differences between the two groups as a single sample with a hypothesized mean difference of 0.

set.seed(2018)
null_distn_paired_means <- zinc_diff %>%
specify(response = pair_diff) %>%
hypothesize(null = "point", mu = 0) %>%
generate(reps = 10000) %>%
calculate(stat = "mean")
null_distn_paired_means %>% visualize()

We can next use this distribution to observe our $$p$$-value. Recall this is a left-tailed test so we will be looking for values that are less than or equal to 4960.477 for our $$p$$-value.

null_distn_paired_means %>%
visualize(obs_stat = d_hat, direction = "less")

##### Calculate $$p$$-value
pvalue <- null_distn_paired_means %>%
get_pvalue(obs_stat = d_hat, direction = "less")
pvalue
# A tibble: 1 x 1
p_value
<dbl>
1       0

So our $$p$$-value is essentially 0 and we reject the null hypothesis at the 5% level. You can also see this from the histogram above that we are far into the left tail of the null distribution.

#### Bootstrapping for confidence interval

We can also create a confidence interval for the unknown population parameter $$\mu_{diff}$$ using our sample data (the calculated differences) with bootstrapping. This is similar to the bootstrapping done in a one sample mean case, except now our data is differences instead of raw numerical data. Note that this code is identical to the pipeline shown in the hypothesis test above except the hypothesize() function is not called.

boot_distn_paired_means <- zinc_diff %>%
specify(response = pair_diff) %>%
generate(reps = 10000) %>%
calculate(stat = "mean")
ci <- boot_distn_paired_means %>%
get_ci()
ci
# A tibble: 1 x 2
2.5%    97.5%
<dbl>      <dbl>
1 -0.111600 -0.0501975
boot_distn_paired_means %>%
visualize(endpoints = ci, direction = "between")

We see that 0 is not contained in this confidence interval as a plausible value of $$\mu_{diff}$$ (the unknown population parameter). This matches with our hypothesis test results of rejecting the null hypothesis. Since zero is not a plausible value of the population parameter and since the entire confidence interval falls below zero, we have evidence that surface zinc concentration levels are lower, on average, than bottom level zinc concentrations.

Interpretation: We are 95% confident the true mean zinc concentration on the surface is between 0.11 units smaller to 0.05 units smaller than on the bottom.

#### Check conditions

Remember that in order to use the shortcut (formula-based, theoretical) approach, we need to check that some conditions are met.

1. Independent observations: The observations among pairs are independent.

The locations are selected independently through random sampling so this condition is met.

2. Approximately normal: The distribution of population of differences is normal or the number of pairs is at least 30.

The histogram above does show some skew so we have reason to doubt the population being normal based on this sample. We also only have 10 pairs which is fewer than the 30 needed. A theory-based test may not be valid here.

#### Test statistic

The test statistic is a random variable based on the sample data. Here, we want to look at a way to estimate the population mean difference $$\mu_{diff}$$. A good guess is the sample mean difference $$\bar{X}_{diff}$$. Recall that this sample mean is actually a random variable that will vary as different samples are (theoretically, would be) collected. We are looking to see how likely is it for us to have observed a sample mean of $$\bar{x}_{diff, obs} = 0.0804$$ or larger assuming that the population mean difference is 0 (assuming the null hypothesis is true). If the conditions are met and assuming $$H_0$$ is true, we can “standardize” this original test statistic of $$\bar{X}_{diff}$$ into a $$T$$ statistic that follows a $$t$$ distribution with degrees of freedom equal to $$df = n - 1$$:

$T =\dfrac{ \bar{X}_{diff} - 0}{ S_{diff} / \sqrt{n} } \sim t (df = n - 1)$

where $$S$$ represents the standard deviation of the sample differences and $$n$$ is the number of pairs.

##### Observed test statistic

While one could compute this observed test statistic by “hand”, the focus here is on the set-up of the problem and in understanding which formula for the test statistic applies. We can use the t_test function on the differences to perform this analysis for us.

t_test_results <- zinc_diff %>%
t_test(
formula = pair_diff ~ NULL,
alternative = "less",
mu = 0
)
t_test_results
# A tibble: 1 x 6
statistic  t_df     p_value alternative lower_ci   upper_ci
<dbl> <dbl>       <dbl> <chr>          <dbl>      <dbl>
1  -4.86381     9 0.000445558 less            -Inf -0.0500982

We see here that the $$t_{obs}$$ value is -4.864.

#### Compute $$p$$-value

The $$p$$-value—the probability of observing a $$t_{obs}$$ value of -4.864 or less in our null distribution of a $$t$$ with 9 degrees of freedom—is 0. This can also be calculated in R directly:

pt(-4.8638, df = nrow(zinc_diff) - 1, lower.tail = TRUE)
[1] 0.000446

#### State conclusion

We, therefore, have sufficient evidence to reject the null hypothesis. Our initial guess that our observed sample mean difference was not statistically less than the hypothesized mean of 0 has been invalidated here. Based on this sample, we have evidence that the mean concentration in the bottom water is greater than that of the surface water at different paired locations.

### B.6.5 Comparing results

Observing the bootstrap distribution and the null distribution that were created, it makes quite a bit of sense that the results are so similar for traditional and non-traditional methods in terms of the $$p$$-value and the confidence interval since these distributions look very similar to normal distributions. The conditions were not met since the number of pairs was small, but the sample data was not highly skewed. Using any of the methods whether they are traditional (formula-based) or non-traditional (computational-based) lead to similar results here.