# read in the data
<- here::here("data/syn_data.csv") |> readr::read_csv() syn_data
Tutorial 05: T-test
Introduction
This week’s topic is comparing the means of two groups. We are building on the foundations of the t-distribution that we discussed in previous weeks to have a closer look at how to compare the means of two groups.
In today’s example, we’re going to be investigating the differences between two groups of people. This tutorial will walk you through the process of posing a research question, defining your hypotheses, examining and visualising the data, and finally testing and reporting the results. To do this, we will be making use of a very common statistical procedure called the t-test.
Everything you need to know in order to perform a t-test successfully will be covered in this tutorial, the Skills Lab, and the Lecture for this week.
Study Design
Today’s data comes from Mealor et al.’s (2016) paper developing the Sussex Cognitive Styles Questionnaire (SCSQ), which is a six-factor questionnaire on different aspects of how people think about and experience the world (their “cognitive style”). The paper validates the new questionnaire by comparing responses from people with and without synaesthesia.
Synaesthesia is a neuropsychological condition that usually involves automatic and lifelong associations between stimuli that aren’t usually connected. In Mealor et al.’s study, there were two different types of synaesthetes (people with synaesthesia): grapheme-colour synaesthetes and sequence-space synaesthetes.
Grapheme-colour synaesthetes experience colour when they see or think about written language, such as letters or numbers.
Sequence-space synaesthetes experience sequences - such as days of the week, months of the year, or numbers - arranged in space around them.
If you’re interested, you can learn more about synaesthesia by watching this short video by synaesthesia researcher Richard Cytowic, or by Sussex’s own Prof Jamie Ward, but it’s not necessary in order to complete the tutorial.
The SCSQ Subscales
The Sussex Cognitive Styles Questionnaire (SCSQ) has six subscales that measure different dimensions of cognitive styles. Today’s practice will focus on the Imagery Ability and Technical/Spatial subscales.
The six subscales are:
Imagery Ability, measuring the use of visual imagery on a day to day basis
Technical/Spatial, measuring spatial imagery, mathematical ability, and interest in technology
Language and Word Forms, measuring interest in and attention to language
Need for Organisation, measuring need for organisation
Global Bias, measuring preference for holistic vs detail-oriented thinking
Systemising Tendency, measuring interest in categorisation and systems
Scoring
On the SCSQ, all items are rated on a five-point Likert scale from 1 (strongly disagree) to 5 (strongly agree).
For each subscale, a person’s score is calculated as the mean rating on the items belonging to that scale. A higher score indicates a greater preference for the cognitive style that the subscale represents.
Right, enough background! Let’s dive in.
Task 1
Load the necessary packages and data.
- Load the
tidyverse
package. We’ll also be using the newggrain
package, so you can load that one as well. - Read in the data saved in
syn_data.csv
, which is in thedata
folder.
Codebook
Data Preparation
Task 2
Inspect your dataset, and compare it to the Codebook. Identify any discrepancies between the two, using the Codebook as a guide for what your dataset should look like and what variables it should contain.
Make a list below of the steps you must take to clean or change your dataset so that it matches the Codebook, then do them.
Research Question
Today we will be walking through the process of conducting an independent samples t-test. This test compares the means of two groups (“samples”) which each contain different entities/people (“independent”). We will test the null hypothesis that the sample means from our two independent groups are drawn from the same population.
To translate that into our current dataset, we will be comparing the means of overall imagery scores between groups of people with synaesthesia and people without. The difference in imagery between synaesthetes and non-synaesthetes is well-documented; see e.g., O’Dowd et al. (2019). These groups are also independent by definition: a person can either have synaesthesia or not, but they can’t be in both groups at once.
Stop and think about this before we go on. If you need a refresher, look back on the Lecture on Null Hypothesis Significance Testing (Week 4) for a reminder of how to define the null and alternative hypotheses.
Task 3
Using the lectures from Weeks 04 and 05, answer the following questions in your Quarto document.
What is the alternative hypothesis for this study? What pattern would we expect to see in the data if the alternative hypothesis were true?
What is the null hypothesis for this particular study? What pattern would we expect to see in the data if the null hypothesis were true?
Make a prediction: What do you think we will find? Do you think that there will be a difference in overall imagery ability?
What alpha level would you choose for this test?
Don’t just look at the solution! Take a moment to think it through and write down your thoughts in your Quarto document before you move on.
Now that we have our hypotheses and prediction nailed down, let’s visualise the data.
Visualisation
Let’s visualise the differences between group means to help us understand our data better. We’ll start with a basic plot and build it up from there to create a publication-worthy, report (or take-away-paper!) -quality plot. We’ll also suggest some optional extras to make your plots extra fancy, but you’ll have to do the legwork!
We’re going to try out a new type of plot today: the raincloud plot. Raincloud plots are named because they include a density plot (basically a smoothed histogram) with a scatter of the data points underneath, which kind of look like clouds with raindrops. They contain a ton of useful information all at once: the distribution of the data, the actual data, and often some summaries of the data (such as boxplots or means). Although they’re a bit more complex to read and interpret, they’re more efficient uses of space and allow quick comparison of multiple important measures.
Revision of {ggplot2}
This section contains revision of constructing data visualisations with the {ggplot2} package. It’s been a while since PAAS, so this section is included to help you refresh your memory.
The {ggplot} package is a bit of a universe in its own right. The “gg” stands for “grammar of graphics” - in other words, a language (“grammar”) for creating visualisations (“graphics”). We’ll see more examples each week of different kinds of plots you can build, and you may have already created some previously in PAAS.
Layers
Plots in {ggplot2} are built in layers. Each layer adds to or changes something about the plot; these can be big elements, like determining the type of plot to create, to small details like editing axis labels or changing colours. In {ggplot2}, each layer is a function.
If it helps, you can think of layers like different colours in a linocut print. Each additional layer of colour adds a bit more to the overall picture, building up from big blocks of colour to small details.
To build a visualisation in {ggplot2}, it’s a good idea to build your plot in the same way, from big picture to small detail. As we’ll see, we recommend building plot layers like this:
|>
dataset +
aesthetics_mapping +
choose_type_of_plot +
add_more_elements +
edit_labels_or_colours apply_a_theme
These are guidelines, but the general => specific flow is for a good reason: layers are evaluated from top to bottom. So, it’s best to get the big pieces in place first, then fine-tune, than to have those fiddly bits overwritten by a major change at the end of the code.
Notice as well that layers are added to a plot object with +
and NOT with |>
. This is specific to {ggplot2} (AFAIK!) and is easy to forget, but don’t worry - it’s such a common thing that {ggplot2} has a very friendly error message for fixing it.
mapping
must be created by aes()
The actual error that pops up when you use a pipe instead of +
isn’t super transparent. However, there is a very friendly reminder directly underneath to nudge you in the right direction. It’s a good reason to always read the error message in full!
|>
syn_data ggplot(aes(x = scsq_imagery)) |>
geom_histogram()
Error in `geom_histogram()`:
! `mapping` must be created by `aes()`.
ℹ Did you use `%>%` or `|>` instead of `+`?
Mapping
As we saw in the error just above, mapping is created with aes()
. This little function defines the aesthetics of the plot - in other words, this is how you tell R what data you want it to plot. We’ll use the following general format to set up a plot:
|>
dataset_name ggplot(aes(x = variable_on_x_axis, y = variable_on_y_axis, ...))
The ...
takes additional arguments to add things like colour and fill.
Geoms, etc.
So, how do we actually add layers? There are several common types of functions with shared prefixes that do particular things. We’ll meet lots of examples of them just below, but as a quick reference for some of the more common function types.
Note that in the names below, the *
is a wildcard placeholder. This means that, for example, geom_*()
stands in for a family of functions where the *
could be many different things: geom_point()
, geom_bar()
, geom_histogram()
, etc.
geom_*()
: Draw geometric objects to represent the data.stat_*()
: Add elements to the plot calculated with statistical functions.scale_*()
,labs()
, andlims()
: Adjust the appearance of the axes (labels, title, limits, etc.) or quickly adjust the labels or limits onlyguide_*()
: Make adjustments to the scales or to other interpretational elements of the plot (such as legends for categories)theme_*()
: Apply a pre-made theme to the entire plot
See the {ggplot} reference documentation for a comprehensive list and detailed guide to these functions and more.
Right, the best way to get a handle on these functions is to start building plots! So let’s jump in.
Task 4
Build a raincloud plot with group means and confidence intervals using the following steps.
- Create a base
ggplot
layer with synaesthete status on the x-axis and overall imagery ability on the y-axis. - Use the
geom_rain()
function to create the raincloud plot. - For a t-test, we’re primarily interested in the group means, but we don’t have them represented by default in the raincloud plot. Add in a layer with the means and CIs, as we did in the Skills Lab. To make sure we can see the means on top of the scatter of points, you should also add a
colour =
argument. - Format and theme your plot by adding axis labels, setting the limits on the y-axis to 1 and 5, and adding a theme.
Look through the hints below if you don’t remember how to do these steps!
Nicely done! This plot is great, and would work well for a take-away paper, for example. If you’re keen to move on, you can skip the optional section below, but if you’d like to make your raincloud plots even nicer, give it a try.
Want a bit more out of your plots? Here are some suggestions you can look into to make your plot unique. Remember, if you get stuck and can’t get them to work, ask us in a practical or drop-in!
Add Colour by Group
Check out the fill =
and colour =
arguments for aes()
in the very first line of ggplot()
. See if you can recreate this plot:
Transparency, Shape, and Colour
Those solid black dots and garish default colours are awful! Check out the alpha =
and shape =
arguments to adjust the transparency of the dots and the shape of the means. Then, try applying some colours and getting rid of the superfluous legend on the right-hand side.
Task 5
Interpret the plot and write down your thoughts in your Quarto doc. What can you learn from this visualisation of the data?
Now that we’ve had a look at our data, we can move on to actual statistical testing!
The t-test
So, we’ve made our predictions, and we’ve had a look at the data. Now we’re ready to conduct our t-test.
In order to find out whether people with and without synaesthesia differ in their imagery ability, the test follows this process:
We’ll need the mean overall imagery in each group. These two means represent our best guess about overall imagery ability for people who have have synaesthesia and who don’t.
Next, we’ll calculate the difference in the means between the two groups. This difference is a measure of the effect - the size of the impact on overall imagery associated with being a synaesthete or not.
Then, we will calculate an estimate of the standard error of the difference in the means. This is a measure of the error - how much variation there is among the differences in the means.
To calculate our test statistic, t, we then simply compare how big the effect is compared to the error. You can think of this as a signal-to-noise ratio - how big is the “signal” (the difference in imagery associated with synaesthesia) against the background of the “noise” (the variation in estimates of that difference in imagery)? In other words:
\[ t = \frac{\text{difference in means}}{\text{estimated standard error of the difference}} \]
From here, we’re in familiar territory. We have a test statistic, t, that we will calculate for our data. Since we’ve already practiced working with distributions, probabilities, and critical values, we know that all we need is the t-distribution with the right degrees of freedom. With that information, we can find the probability, p, of obtaining the t-value that we calculated, or a more extreme one, assuming the null hypothesis is true.
The good news is, all of the calculations and values we need will be calculated by R
for us, as we will see as we progress. Doing the calculation is R
’s bit; interpreting the results and understanding what they mean for our understanding of the world is ours!
Task 6
Complete the following steps:
- Write a formula that represents the relationship between our variables. This has the general form
outcome_variable ~ grouping_variable
. What would this be for our study comparing overall imagery score between synaesthetes and non-synaesthetes?
- Use the
t.test()
function to perform an independent samples t-test on thesyn_data
data to find out whether there is a difference inoverall_img
betweensyn
groups. To to do this, you need to use the formula you wrote in the previous step, and you’ll also need to tell thet.test()
function the name of the dataset where it can find the variables.
- The last argument in the
t.test()
function should bealternative = "two.sided"
. This tells R to run a two-tailed test, instead of a one-tailed test. - Save the result into an object called
syn_t
.
Interpretation
Let’s interpret the results, now that we finally have them. For statistical results, when we say “interpret” results, what we mean is to translate the statistical output, like we got from the t.test()
, back into the context of our original research question. The questions in the task below will help walk you through this process.
Task 7
Write down the answers to each question about interpreting the t-test results in your Quarto doc:
How big is the difference between the means?
Is a t of -8.51 a big difference? How do you know?
What is the probability of obtaining this value of t under the null hypothesis?
Putting everything together, what can we learn from this result? Write down what you understand from this test in your own words.
Reporting
Each well-known statistical test has a specific format for reporting the numerical results in formal situations, such as a report or journal article. Here’s the basic format for t-tests in APA style:
estimate_name(degrees_of_freedom) = estimate_value, p = exact_p, Mdiff = difference_in_means, 95% CI = [CI_lower, CI_upper]
In APA style, we’ll round all numbers to two decimal places where appropriate, except for p. We round p to three decimal places, unless it’s smaller than .001, in which case we simply report “p < .001”.
Everything we’ve reported above we’ve seen before, except for Mdiff. This represents the differences in the means of the two groups. This is a key element of the t-test, but it’s also important because the 95% CIs are an interval estimate around that mean difference. Without reporting Mdiff, the CIs are quite hard to interpret!
As a side note, you can subscript letters in Quarto by surrounding them before and after with ~
s like this: *M*~diff~
Task 8
- Use the APA format and the values from your output to type out the result of the t-test.
- Put all of this together into one report of your findings, both in plain language and including all of the relevant statistical results, such as the M and SD in both groups and the results of your statistical test. See the lecture for an example.
Together with the graph we already made, this is a thorough and well-reported analysis!
Recap
Well done conducting your t-test.
In sum, we’ve covered:
How to create professional plots representing the key relationship of interest
How to run the independent samples t-test and read the output
How to report and interpret the statistical results of these tests in real-world terms
Remember, if you get stuck or you have questions, post them on Discord, or bring them to practicals or to drop-ins.
If you’re feeling warmed up now, take on the ChallengR below to test out your new analysis skills.
ChallengR
This task is a ChallengR, which are always optional, and will never be assessed - they’re only there to inspire you to try new things! If you solve this task successfully, you can earn a bonus 2500 Kahoot Points. You can use those points to earn bragging rights and, more importantly, shiny stickers. (See the Games and Awards page on Canvas.)
There are no solutions in this document for this ChallengR task. If you get stuck, ask us for help in your practicals or at the Help Desk, and we’ll be happy to point you in the right direction.
The main part of this tutorial is an independent measures t-test, when the two groups being compared contain different (“independent”) people or entities. However, in the lecture we also covered a second type: the repeated-measures t-test, when the two groups being compared contain the same people or entities in different conditions, at different timepoints, etc. For this task, you will need to prepare for, run, and report a repeated-measures t-test.
Data and Design
The dataset for this task is an expanded version of the same kind of design described in the lecture about training synaesthesia, but this time for colours for musical notes. In this hypothetical study, participants were first tested on a Stroop-style task, where they were presented with a pure tone musical note and a block of colour on a screen, and were asked to respond to the colour as quickly as possible. Then, over the course of several weeks, participants underwent training where they heard different tones paired with different colour lights. Finally, the participants again completed the same Stroop-style task as the beginning. The researchers hypothesized that this training would lead to significantly quicker response times after training compared to before, indicating an automatic connection between music and colour.
The dataset contains the following variables:
id
: ID code of the participanttime
: Coding variable of the timepoint, either “pre” or “post” the trainingresponse_ms
: Response latency to tap the target colour, in milliseconds
Task 9
- Read in the data saved in
syn_train.csv
, which is in thedata
folder, and have a look at it - Generate a summary table presenting relevant descriptive statistics for this design. You can format it nicely if you like for practice.
- Perform the appropriate t-test and make sure you understand what the results mean.
Once you have your output, take the Week 5 ChallengR quiz on Canvas and use the output to answer the questions. Good luck, and well done again!