Topic 6: \(t\)-tests for two-sample hypothesis testing


In Topic 6 we extended our understanding of \(t\)-tests. We will now practice carrying out these new tests, using real data.


1 Visualising Data

For this question, we will once again be assessing the wonions data set on White Imperial Spanish onion plants in the sm R package (Bowman and Azzalini 2021).

In Computer Lab 6, we assessed this data set of size \(n=84\) by focusing on two variables:

  • the Yield (in grams per plant), and
  • the Density of planting (in plants per square metre).

However, the wonions data set also contains a third, integer variable, Locality, which denotes whether the onion planting occurred in Purnong Landing (1) or in Virginia (2). While we ignored the planting location in our previous analysis, it may be beneficial to reassess the data to see if the onions planted in the two locations have statistically significant differences with regards to yield. Therefore, in this computer lab, we will assess the variables:

  • Yield and
  • Locality

To do this, we can carry out an independent samples \(t\)-test.

1.1

Open up RStudio and create a new script file. You should already have the sm package installed. Run the code below to get started:

library(sm) # Load package
data(wonions) # Load onions data

1.2

Suppose that we want to check if there is a significant difference in the average yield between White Imperial Spanish (WIS) onions planted in Purnong Landing and Virginia.

  • Let \(\mu_1\) denote the average yield of WIS onions planted in Purnong Landing.
  • Let \(\mu_2\) denote the average yield of WIS onions planted in Virginia.

Using this notation, define the null and alternative hypotheses for our test.

Hint: There are actually two ways in which we can write the hypotheses - can you think of both ways? Check the Topic 6 readings if you are unsure.

1.3

Define the dependent and independent variables for our test.

1.4

As discussed in Computer Lab 6, it is generally a good idea to perform some exploratory data analysis, before proceeding with your hypothesis testing.

To begin, let’s compute summary statistics for the Yield for both groups. Use the subscripts 1 (for Purnong Landing) and 2 (for Virginia) in your notation to differentiate your answers (e.g. use \(n_1\) for the Purnong Landing sample size):

1.4.1

Compute the sample sizes for both groups.

Try using the table function.

1.4.2

Compute the means and standard deviations of the Yield values for both groups, using the tapply function (one of several functions from the apply family of functions).

Note: The tapply function can be very useful when we are assessing different categories within a data set. The function has three main arguments:

  • X: A vector of data - this is what we would like to assess
  • INDEX: A list of factors - this specifies the different categories within our data
  • FUN: A function to apply to the data that has been specified in X and grouped into different groups based on the categories in INDEX

Hint: Take a look at the R code below for an example use of the tapply function:

tapply(wonions$..., wonions$Locality, min)
# You will need to replace the `...` here with the appropriate variable

1.5

To visualise the Yield data, create separate histograms and box plots for each group.

Note: Using the R code below as a guide, try to plot both box plots in the one graph - you will have to fill in the ...s and add some additional details such as a title and axes labels.

Hint: You may be able to re-purpose some of your code from the previous computer lab for this question.

boxplot(... ~ wonions$Locality, #note that we are separating the observations by Locality
        col = c("orange","chartreuse3"))

1.6

Do you notice any important details from the results of 1.4 and 1.5? Comment on the differences and similarities in the histograms and box plots you have produced.

2 Conducting an independent samples \(t\)-test in R

In this question, we will continue assessing the Yield of the White Imperial Spanish onions in the two Locality regions.

Before we begin our \(t\)-test for the wonions data set, it is extremely important that we check that the assumptions of the independent samples \(t\)-test are satisfied.

Just as for the one-sample \(t\)-test, we need to check that the data are numeric, that observations are independent, and that the sample mean \(\overline{X}\) is normally distributed.

2.1

We know that the data are numeric, and that the observations are independent. Therefore, we need to next conduct the Shapiro-Wilk test.

Use the shapiro.test function to carry out the Shapiro-Wilk test on the Yield data. Remember though, we need to check for normality for both Locality regions individually. We can achieve this using subsetting.

Hint: Take a look at the R code below if you’d like some help getting started. Note that you will need to specify the variable being assessed.

shapiro.test(wonions$...[wonions$Locality == 1])
# Here we perform the shapiro test on an unspecified variable
# - you will need to replace the `...`
# with the appropriate variable.
# We use the code [wonions$Locality == 1] 
# to specify that we are only 
# interested in those onions from Locality 1

2.2

The independent samples \(t\)-test also has a fourth assumption, namely that the variances between the groups are equal, which we should check now, using the Levene’s test for equality of variances.

You will need to load the car package in R to use this test. Fill out the missing details (the …) in the R code below to run this test.

library(car)
leveneTest(wonions$... ~ as.factor(wonions$...)

Based on this test, what is your conclusion?

2.3

Regardless of your answers to 2.1 and 2.2 above, for the remainder of this question we will proceed under the assumptions that the independent samples \(t\)-test assumptions are satisfied, as is the assumption of equal variances.

We can now carry out an independent samples \(t\)-test. In R, we can do this using the t.test function, just as for the one-sample \(t\)-test, although we need to be careful with how we specify our arguments for this function.

Run the R code below:

t.test(wonions$Yield ~ wonions$Locality, var.equal = TRUE)

Note here that we have included the argument var.equal = TRUE since we are conducting an independent samples \(t\)-test with samples that we assume have equal variances. If we had concluded that the variances were unequal, we would change this to var.equal = FALSE.

2.4

Interpret the output of the independent samples \(t\)-test, and note down the test statistic value, the \(p\)-value, the degrees of freedom, the sample means, and the \(95\%\) confidence interval for the difference.

2.5

Explain, in your own words, what the \(95\%\) confidence interval tells us.

2.6

Write a short conclusion summarising this test and your findings.

3 Conducting a paired \(t\)-test in R

For this question, we will consider data collected by Cornell Professor of Nutrition David Levitsky, on students’ weight gains over their first year of college (DASL 2021). A random sample of \(68\) students from varying backgrounds was selected, and their weights (in pounds) were measured at the start of semester, and 12 weeks later, at the end of semester. This data is available in the freshman-15.txt file on LMS.

3.1

Download the freshman-15.txt file from the LMS, and load the data into R using the following code (make sure you save the data to your current R working directory):

freshman <- read.table(file = "freshman-15.txt", header = T)

3.2

Suppose that we would like to know whether the average difference in the weights of students before and after a semester of college is statistically significant. To determine this, we can carry out a paired \(t\)-test.

  • Let \(\mu_D\) denote the true mean difference in before and after weights (in pounds).

Using this notation, define the null and alternative hypotheses for this paired \(t\)-test.

Hint: Check the Topic 6 readings if you are unsure.

3.3

What are the dependent and independent variables for this test?

3.4

As a first step to our analysis, we should visualise our data:

  • Create box plots of the initial and final weights.
  • Create histograms of the initial and final weights.

3.5

By comparing the histograms for the initial and final weights, what do you observe?

3.6

We should also look at some basic descriptive statistics. Using appropriate R functions, compute the sample means and standard deviations of the initial, final, and paired difference weights.

Comment on your findings.

3.7

Before we conduct our test, we need (as always) to check our test assumptions.

Remember, with a paired \(t\)-test, our variable of interest is not the before and after weights themselves, but rather the paired differences. Therefore, when checking the paired \(t\)-test normality assumption, we have to be very careful that we are assessing the right values.

In R, add an extra column of data to the freshman object, and store in this column the paired difference values between each individual’s initial and final weight. Label this column Paired.Difference.

Check the code chunk below if you’d like some help getting started.

freshman$Paired.Difference <- ... # Replace the ... with an appropriate expression

3.8

Create the following graphs for the paired difference data:

  • A histogram with a normal curve overlaid
  • A Normal Q-Q plot

What do you observe?

3.9

We can see that the data are numeric, and we are told that the observations are independent. Therefore, all that remains to confirm for our test assumptions is that the sample mean \(\overline{X}\) is normally distributed.

Carry out a Shapiro-Wilk test for our test variable. Based on the result of this test, and your previous findings, what do you conclude?

3.10

For the remainder of this question, regardless of your results in 3.8 and 3.9, we will proceed under the assumption that all the assumptions for the paired \(t\)-test have been met.

Carry out a paired \(t\)-test in R, using the code below as a guide. Note that we can still use the t.test function for this test (as long as we use the correct arguments). You will have to fill out the missing details (the …), just as in 2.2.

t.test(freshman$..., freshman$..., paired = TRUE)

3.11

Interpret the output of the test, and note down the test statistic, the degrees of freedom, the \(p\)-value, the mean of the differences, and the \(95\%\) confidence interval.

3.12

Explain, in your own words, what the \(95\%\) confidence interval tells us. Based on this confidence interval, would you reject your null hypothesis?

3.13

Write a short conclusion summarising this test and your findings.

3.14

Extension: Carry out a one-sample \(t\)-test for the freshman data, testing whether the paired differences are different from 0. What are your findings? Do you notice anything interesting about the \(t\)-test output?

4 Computing Effect Sizes

The effect size, or relative size of the difference between means, is a useful statistic that can be computed for any \(t\)-test. We can use the cohen.d function from the effsize R package to compute the effect sizes for our \(t\)-tests.

To begin, run the R code below:

install.packages("effsize")
library(effsize)
?cohen.d

Hint: It may be helpful to refer to Section 3 of the Topic 6 readings when answering these questions.

4.1

A help file should have appeared in the bottom right section of RStudio when you ran the code in 4. Let’s take a look at the details in this help file on the composition of the cohen.d function.

This function can take several arguments - we’ll cover the pertinent ones here:

  • The first argument: If we are conducting a one-sample \(t\)-test or an independent samples \(t\)-test, we need to specify the data we are assessing here. If we are conducting a paired \(t\)-test, we instead specify the treatment group data here.
  • The second argument: This will either be NA (for a one-sample \(t\)-test), a factor with two levels (for an independent samples \(t\)-test), or the control group data (for a paired \(t\)-test).
  • mu: The value of \(\mu\) under \(H_0\). Use if you are interested in a single sample effect size.
  • paired: If the data we are assessing is paired, we set paired == TRUE. Otherwise we use paired == FALSE.
  • within: This is only used for paired \(t\)-tests. Set this to within == FALSE when conducting paired \(t\)-tests, and don’t include this argument for other tests.

4.2

To begin, let’s consider a simple one-sample \(t\)-test. Suppose that we are just assessing the Yield variable of the wonions data set, and are not considering Locality. We run a one-sample \(t\)-test, where we have \(H_0: \mu = 110\) vs \(H_1: \mu \neq 110\). To check the effect size between the sample mean and the mean under \(H_0\), we can use the following code:

cohen.d(wonions$Yield, NA, mu = 110)

Run this code now. You should obtain an estimate of roughly \(0.1828\). This is considered a negligible, or very small effect.

Make sure you check over the arguments used here, and refer back to the details provided in 4.1 before continuing.

4.3

Now that you have been introduced to the cohen.d function, see if you can use it to compute the effect size for the independent sample \(t\)-test conducted in 2.3. Recall that for this test we are assessing Yield between the two Locality options. Note that Locality should be treated as a factor variable here.

Interpret the effect size.

Check the code chunk below if you’d like some help getting started. You will need to replace the ...s.

cohen.d(..., as.factor(...), mu = ...)

4.4

Compute the effect size for the paired \(t\)-test conducted in 3.10. Interpret this effect size.

Use the R code provided below as a starting point. You will have to fill in the ... sections.

cohen.d(..., ..., paired = ..., within = FALSE)


References

Bowman, A. W., and A. Azzalini. 2021. R Package sm: Nonparametric Smoothing Methods (Version 2.2-5.7). University of Glasgow, UK; Università di Padova, Italia. http://www.stats.gla.ac.uk/~adrian/sm/.
DASL. 2021. “Freshman 15 [.txt File].” 2021. https://dasl.datadescription.com/datafile/freshman-15/.


These notes have been prepared by Rupert Kuveke and Amanda Shaker. The copyright for the material in these notes resides with the authors named above, with the Department of Mathematics and Statistics and with La Trobe University. Copyright in this work is vested in La Trobe University including all La Trobe University branding and naming. Unless otherwise stated, material within this work is licensed under a Creative Commons Attribution-Non Commercial-Non Derivatives License BY-NC-ND.