5. Week 5 - More on correlation - Assumptions, issues, an alternative and intercorrelation

Written by Margriet Groen

Overview

This week we focus on two strands of learning:

  1. More on correlation analysis
  2. Research project: Refine research question, decide on measures, pre-register hypothesis

Building on last week’s work on how hypothesis testing and significance testing, this week we will continue to look at correlation as a measure of association between two numerical variables. We will review assumptions associated with Pearson’s r, discuss some issues important to be aware of when interpreting correlation results and finally, we’ll talk about intercorrelation.

We will also start the group project in which you will investigate the question: Is mind wandering related to screen time?

The materials we have prepared to support your learning are arranged in four parts:

  • Lectures
  • Reading
  • Pre-lab activities
  • Lab activities

You can navigate to each in the side bar on the right

Lectures

Important

Please watch the lecture material, complete the essential reading and then the pre-lab activities before attending your lab. As a reminder, the lectures and pre-labs introduce the concepts and give you some initial practice, and the lab gives you the opportunity to apply these skills in-person with our support.

Questions?

Make a note of questions as you work through the material. These might be answered later in the recordings, or in your lab. Otherwise, please bring them to the live online Q&A session on Friday or post them on the Q&A forum on Moodle.

Section A: More on correlation

You can watch the lecture for the video using the video playback windows shown next. The lecture is split into different sections. You can download the slides for all sections together here: More on correlation: slides.

  1. Theory – Assumptions(~35 minutes) Watch this part before you complete the reading and the pre-lab activities. The video has captions, in case you find that helpful. You can download the slides and a transcript from the links below the video.

Assumptions: transcript.

  1. Theory – What happens if assumptions are violated(~20 minutes) You can download the slides and a transcript from the links below the video.

What happens when assumptions are violated: transcript.

  1. Theory – Issues(~35 minutes) You can download the slides and a transcript from the links below the video.

Issues: transcript.

Take a break!

Before continuing, we recommend you take a 15-minute break away from your computer. Remember to note down any questions to bring to the Q&A, lab or discussion forum whilst you’re watching the lectures.

Reading

Easy access to readings

All reading is accessible via the library. You can go to Moodle -> Library Resources -> Resource List or you can follow this link. Readings have one of the following tags:

  • Essential: We expect you to be familiar with these reading. You will be assessed on this.
  • Recommended: We highly recommend these readings. They often provide a different perspective, or explain things in a different way.
  • Optional: Further readings for those interested.

The reading list is divided into multiple sections:

  • Reading associated with statistics: this contains links to readings useful for the development of statistical concepts and skills. The main textbook Fundamental statistics for the behavioral sciences by Howell (Essential) lives here.
  • Reading associated with psychological research principles and design: this contains links to readings that provide a good basis for foundational knowledge around research design.
  • Reading associated with Open Science principles and practices: this contains links to key literature on the Open Science movement.
  • Week by week: these contain links to key reading for that week that are not listed on any of the more general lists above.

For week 5, the reading is:

  • Howell, D. C. (2017). Fundamental statistics for the behavioral sciences (9th ed.). Cengage Learning. Chapter 9. Essential
    • Focus on sections 9.5, 9.6, 9.7 and 9.12. These sections support the concepts introduced in the pre-recorded lecture on applying the hypothesis testing framework to correlation and reporting the results correctly.

Pre-lab activities

Pre-lab activity 1: Getting ready

To complete this week’s lab activities you will need several files:

  1. We’ll work with an example script called 4141_wk5_lab.qmd. This is a Quarto document.
  2. We need several data files.

You can download the files here: 4141_wk5_lab.zip. Usually they will be saved in your ‘Downloads’ folder.

Next, upload the zip-folder as it is to your Files space on the R-server (bottom right quadrant; no need to extract the files first).

Finally, open the example script by clicking on the file called 4141_wk5_lab.qmd in the Files space on the R-server.

You are now ready to start work on the lab activities.

Watch the ‘How to’ videos

If you haven’t done so already, this is a good time to watch the ‘How to’ videos on how to:

Lab activities

Lab activity 1: Assumptions of Correlation Analysis

Question 1: Types of variables

Correlation would be an appropriate form of analysis for researchers interested in the relationship between:

  1. Dog (breed) and height (cm) of owner
  2. Speed of swimming (mph) and area of tank (cm)
  3. Number of cows sitting and rain fall (mm)
  4. Total llama saliva (ml) expelled and gender of visitors
  5. b and d
  6. b and c
Tip

For Pearson’s r, we hope the data look like a smooth, oval cloud: a straight-line relationship, equal spread of Y at every value of X, and a bell shape. We also watch for outliers: participants who don’t fit the overall pattern.

For each question below, look at the three plots (a histogram of X, a histogram of Y, and a scatterplot) and answer the questions. All the data are simulated for teaching.

Question 2: Revision and exam marks

Forty students reported how many hours per week they spent revising and then sat an exam.

Histogram of weekly revision hours, histogram of exam marks, and a scatterplot of exam mark against revision hours.

  1. Look at the two histograms only. Does any student look unusual? Note anything you see.
  2. Now look at the scatterplot. Which students do not fit the overall pattern? Describe each one, giving their approximate revision hours and exam mark.
  3. Why can the scatterplot reveal something the histograms cannot?
  4. Before running any analysis, what should you check about these students? Give two possible explanations for their scores.
  5. If you calculated Pearson’s r for all 40 students, and then again without these students, would r get higher, lower or stay the same? Explain your reasoning.

Question 3: Arousal and task performance

Sixty participants completed a memory task. Before the task, each was given a score for how aroused (alert and activated) they felt.

Histogram of arousal scores, histogram of task performance, and a scatterplot of performance against arousal.

  1. Describe the relationship between arousal and performance in words.
  2. Look at the histograms. Do they suggest any problem with using Pearson’s r?
  3. Without calculating anything, would you expect Pearson’s r to be strong and positive, strong and negative, or close to zero? Explain why.
  4. Which assumption is violated here? Would switching to Spearman’s rho solve the problem? Why or why not?
  5. What does this example tell you about reporting a correlation coefficient without looking at the scatterplot?

Question 4: Age and vocabulary

Eighty children aged between 6 and 16 took a vocabulary test.

Histogram of age, histogram of vocabulary size, and a scatterplot of vocabulary size against age.

  1. Compare the spread of vocabulary scores for children aged about 7 to 8 with those aged about 13 to 15. Give approximate ranges for each group.
  2. Which assumption is violated? Name it and describe what is wrong in your own words.
  3. Could you have spotted this problem from the two histograms alone? Explain.
  4. Why might this problem affect the p-value for Pearson’s r?
  5. The spread of vocabulary scores grows roughly in proportion to the average score. Suggest one thing you could try.

Question 5: Reaction time and social media use

Eighty participants completed a reaction-time task and reported how many hours per week they spend on social media. Figure 4a shows the raw data.

Histogram of reaction time, histogram of social media hours per week, and a scatterplot of social media hours against reaction time.

Figure 5a. Raw data.
  1. Describe the shape of each histogram.
  2. Does the scatterplot look like an oval, bell-shaped cloud? Which assumption(s) appear to be violated?
  3. In the lecture simulations, what happened to the false-positive rate when variables were skewed but unrelated? What would you still want to check in the scatterplot?

Figure 4b shows the same data after taking the natural log of both variables. Transforming the the data in this way is common approach for heavily skewed data.

Histograms and a scatterplot of the same data after a log transformation.

Figure 5b. The same data on a log scale.
  1. What has changed? Has the transformation solved every problem?
  2. Give one advantage and one disadvantage of log-transforming the data.

Lab activity 2: Finish lab activities from previous weeks (Recap - skip if completed successfully last week)

Background

Over the past few weeks, we worked with some publicly available data from a research project on mind wandering. You can have a look at the questionnaire here.

If you did not successfully finish the lab activities for weeks 1, 2, 3, or 4 you may want to complete these first.

You can log into the R server, open your project (the .Rproj file) and open the Quarto file (ending with .qmd) to continue to work through these.

Aims of the activity

Ensure you are familiar with reading in a .csv-file, creating a histogram and calculating summary statistics.

Steps

  1. Login to the R-server
  2. Click on the R-project (the Rproj) file to open it
  3. In the Files tab (right bottom quadrant), navigate to the example script for that week’s lab activities. This is the Quarto file (ending with .qmd). Click on it to open it.
  4. Complete any remaining activities

Lab activity 3: Checking for missing data

Aims of the activity

We are going to:

  1. Setup the appropriate bits and pieces in RStudio
  2. Read in the data files
  3. Check for missing data
  4. Calculate and interpret Pearson’s r using correlation()
Where are the download links?

From now on, links to the files to be downloaded are in the Pre-lab activities section under Getting ready.

How to use the Quarto document?

Following feedback on the way we presented instructions and guidance across the handbook pages and the Quarto document, we’ve made the following change: All instructions and guidance (including hints and code snippets) for lab activities are now presented on the handbook page.

The Quarto document is largely empty, only containing a copy of the section headers and the actions to remind you of what you are meant to do. Sometimes there are questions associated with the code you’ve written or the output that follows from it. These can also be found on both the handbook page and in the Quarto document.

How to complete the lab activity and use the Quarto script:

  1. Open the handbook pages in a separate window or tab and navigate to Lab activities.
  2. Read the instructions on the handbook page for each step.
  3. Carry out the actions, by inserting a code chunk in the Quarto document, below each action.
  4. If there are any questions associated with that step, please try to answer them. You can write your answer in the Quarto document, as plain text. Answers to the questions will be shared after the last lab session has taken place (on Thursday afternoon).
  5. After having carried out an action, written a code chunk and answered any questions, go back to the handbook page and read the next bit of instruction, until you get to the next action.
  6. If you are stuck: Have a look at the ‘Hints’ and ‘Code’ snippets on the handbook page.
  7. If you are still stuck: Ask for help (your peers on your table, the GTA or lecturer in the lab session, via the Moodle Q&A forum, during Friday’s live online Q&A).

This is also demonstrated in this video

Step 1: Setup the appropriate bits and pieces in RStudio

Before we can do anything, we need tell R what packages we want to use. We use the function library() to do that. It is useful to have a section at the top of your script where you load all the relevant libraries.

Action: Insert a new code chunk by clicking on ‘Code’ at the top and selecting ‘Insert Chunk’.In the code chunk, write code to load the libraries by using the library() function. We need the packages here, tidyverse and easystats. After you’ve written the code, make sure the run the code chunk (click on the green arrow in the top corner of the code chunk).

We need to load several packages (here, tidyverse and easystats) using the library() function. First, inside the code chunk, write library() and put here inside the brackets. On the next line in the code chunk, write library() and put tidyverse inside the brackets. On a third line in the code chunk, write library() and put easystats inside the brackets. Make sure to run the code chunk by clicking on the green arrow in the top corner of the code chunk.

Step 2: Read in the data

For this lab activity, you’ll need the file named vocabulary_experience.csv.

Action: Insert a new code chunk by clicking on ‘Code’ at the top and selecting ‘Insert Chunk’. Inside the chunk, use read_csv() to read in the data file named vocabulary_experience.csv; tell R where to find the file and what it is called. Make sure you’ve run the code chunk. Then, check that you can see the ‘object’ (also called ‘data-frame’) in the Environment (top right quadrant) for the file you read in. I’ve called the data-frame data_thirty. If you can’t remember how to do this, look at the Hint below. Give it a go. If you are still stuck, look at the Code below. You may have to adapt the filepath to match your setup.

dataframe <- read_csv(here("filepath to folder", "filename.extension))

Your code should include:

  • dataframe: the name of the object you want to save the data in (such as data_thirty)
  • <- : assignment arrow
  • read_csv() : the read_csv() function
  • here(): the here() function
  • “filepath to folder”: the folders that you need to point R to starting from whereever here starts
  • “filename.extension”: the name of the datafile, including the extension
data_thirty <- read_csv(here("4141_wk5_lab", "vocabulary_experience.csv"))

Questions Step 2

  1. How many participants does the data frame contain?
  2. How many degrees of freedom would you expect to see in the output if you were to run a correlation analysis with the variables vocabulary and experience using this data frame?

Step 3: Check for missing data

We can check whether there are any missing data in a data-frame in multiple ways:

  1. We can click on the data frame in the Environment (upper right quadrant). The data frame will then open in the Source quadrant (upper left quadrant). Any ‘empty’ cells will have an NA (which stands for ‘Not available’) in them.
  2. We can use the glimpse() function. This gives a compact overview of the data frame, showing the number of rows and columns, then each variable’s name, its type (such as <dbl> for numbers or <chr> for text) and its first few values, so you can quickly check that the data have been read in correctly. Missing data are shown as NA.
  3. We can use some code to check how many missing data points a data frame contains. For this we use the is.na() function inside the sum() function.
  4. We can use some code to check how many participants have complete data. For this we use the complete.cases() function inside the sum() function.

Actions: 1. Click on the data frame in the Environment and have a look at it in the Source quadrant. 2. Use the glimpse() function to have a look at the data frame. 3. Use the is.na() function in combination with the sum() function to check how many NAs there are. 4. Use the complete.cases() function in combination with the sum() function to check how many participants have complete data. You can use the code below to do this. Have a look at each line of code and make sure you understand what the different bits are doing.

# Have a look at the data frame
glimpse(data_thirty) 

# Check the number of NAs in the data frame
check_n_NA <- sum(is.na(data_thirty))                 
check_n_NA

# Check the number of participants with complete data
check_n_complete <- sum(complete.cases(data_thirty))
check_n_complete

Action: Run a correlation analysis with these data. If you can’t remember how to do this, look at the Hint below. Give it a go. If you are still stuck, look at the Code below.

Use the correlation() function we used last week. You need to specify the following information inside the correlation() function:

  • data: data = data_thirty
  • variables: select = c(“vocabulary”, “experience”)
  • method = “pearson”
  • p_adjust = “none”
results <- correlation(data = data_thirty,          # use the `correlation()` function and tell R what data to use
            select = c("vocabulary", "experience"), # specify the variables we want to correlate using `select()`
            method = "pearson",                     # specify which method to use; `"pearson"` is the default
            p_adjust = "none")                      # we'll come back to this in a couple of weeks
results

Questions Step 3

  1. How many data points are missing?
  2. How many participants have complete data?
  3. How many degrees of freedom are reported in the output from the correlation analysis. Explain how that number is derived. Does it match what you expected to see based on the number of participants?

Lab activity 4: Outliers

Aims of the activity

We are going to:

  1. Read in the data file
  2. Build scatterplots with geom_point(), and add a line of best fit using geom_smooth()
  3. Remove outliers using filter(), and create another scatterplot
  4. Calculate and interpret Pearson’s r using correlation() for data with and without outlier values

Step 1: Read in the data file

For this lab activity, you’ll need the file named revision_exam.csv.

Action: Insert a new code chunk and use read_csv() to read in the data file named revision_exam.csv; tell R where to find the file and what it is called. Make sure you’ve run the code chunk. Then, check that you can see the ‘object’ (also called ‘data-frame’) in the Environment (top right quadrant) for the file you read in.

Step 2: Build a scatterplot

Now we are ready to build a scatterplot to visualise the relationship between two of the variables. We are going to look at the relationship between revision time and exam marks.

Action: Insert a new code chunk and use ggplot() to tell R what data to plot and which variables tot use. Then, add layers to plot the datapoints (use geom_point()) and the line of best fit (use geom_smooth(method="lm", se=TRUE)). If you need more guidance on how to do this, look at the Hint below. If you are still stuck, you can look at the Code below.

Use the function ggplot(). Start by writing ggplot() on the next line of the code chunk. Inside the ggplot() function, we need to tell R which data-frame (data=) and which variables to use (mapping=aes(x= , y= ))).

Then, we need to tell R what type of plot we want. We do this by adding a + to the line on which you’ve written ggplot(), then on the next line you add a ‘geometric object’, in this case geom_point().

We also want to add a line of best fit. We do this by adding a + to the line on which you’ve written geom_point(), then on the next line you add another ‘geometric object’, in this case geom_smooth(). We want to specify what type of line of best fit, we do this by adding method="lm", se=TRUE inside the brackets of geom_smooth().

This will give you a basic scatterplot. Revisit the lab activity code from week 1 to remind yourself how to change any of the features (such as the colour, the background, add titles to the axes etc.).

ggplot(data = data_revision_exam, mapping = aes(x=revision_hours, y=exam_mark)) +
  geom_point() +
  geom_smooth(method = "lm", se=TRUE)

Step 3: Remove outliers using filter(), and create another scatterplot

Now,we’ll remove the two participants with outlier values. We’ll do this using the filter() function from the dplyr package (part of the tidyverse).

How does filter() work?

The filter() function allows you to select specific rows from a data frame based on one or more conditions.

For example, imagine we have a dataset containing information about participants in a psychology study, including their age and gender. We might want to select only participants who are over 18 years old.

We can do this using:

filter(data, age > 18)

Here: - data is the name of our data frame. - age > 18 is the condition we want to apply. - note these two bits of information are separated within the function by a comma. - only rows where the condition is TRUE are kept.

We can also apply multiple conditions. For example, to select participants who are over 18 and identify as a woman:

filter(data, age > 18, gender == "Woman")

When we provide multiple conditions separated by commas, filter() keeps only rows that satisfy all of the conditions.

Some useful operators for filtering data include:

Operator Meaning Example
== Equal to gender == "Woman"
!= Not equal to gender != "Woman"
> Greater than age > 18
< Less than age < 30
>= Greater than or equal to age >= 18
<= Less than or equal to age <= 30

We can also use & (AND) and | (OR) to combine conditions.

For example:

filter(data, age >= 18 & age <= 30)

This selects participants aged between 18 and 30 (inclusive).

Action: Use filter() to remove the two participants with outlier values. We can do this by specifying that we only want to keep data from participants who revised for more than 5, but less than 25 hours. The code on the handbook page does this. You can copy the code into your script. Please have a close look and, using the information above, make sure you understand what each part of the code does.

data_revision_exam_clean <- filter(data_revision_exam, revision_hours >5 & revision_hours < 25 )

Action: Now let make a scatterplot without the two participants with outlier values. As before, insert a new code chunk and use ggplot() to tell R what data to plot and which variables tot use. Make sure to use data_revison_exam_clean. Then, add layers to plot the datapoints (use geom_point()) and the line of best fit (use geom_smooth(method="lm", se=TRUE)).

Questions Step 3

  1. Looking at the line of best fit in the first scatterplot (which included the participants with outlier values), how would you describe the relationship between revision hours and exam mark?
  2. What do you estimate the value of the correlation coefficient to be?
  3. Looking at the line of best fit in the second scatterplot (without the participants with outlier values), how would you describe the relationship between revision hours and exam mark now?
  4. What do you estimate the value of the correlation coefficient to be now?

Step 4: Calculate and interpret Pearson’s r using correlation() for data with and without outlier values

Action: Run two correlation analyses: one with the data with (data_revision_exam) and one with the data without (data_revision_exam_clean) outlier values. If you can’t remember how to do this, look at the Hint below. Give it a go. If you are still stuck, look at the code you wrote for Lab activity 3, Step 3.

Use the correlation() function we used last week. You need to specify the following information inside the correlation() function:

  • data: data = data_revision_exam
  • variables: select = c(“revision_hours”, “exam_mark”)
  • method = “pearson”
  • p_adjust = “none”

Lab activity 5: Spearman’s rho

Aims of the activity

We are going to:

  1. Read in the data file
  2. Build a scatterplot with geom_point(), and add a line of best fit using geom_smooth()
  3. Calculate and interpret Pearson’s r and Spearman’s rho using correlation()

Step 1: Read in the data file

For this lab activity, you’ll need the file named reading_enjoyment_books.csv. It contains data from sixty adults who were asked how much they enjoy reading (variable name = reading_enjoyment), on a single item from 1 (not at all) to 5 (very much), and how many books they had read in the past 12 months (variable name = books_read).

Action: Insert a new code chunk and use read_csv() to read in the data file named reading_enjoyment_books.csv; tell R where to find the file and what it is called. Make sure you’ve run the code chunk. Then, check that you can see the ‘object’ (also called ‘data-frame’) in the Environment (top right quadrant) for the file you read in.

Step 2: Build a scatterplot using geom_jitter() instead of geom_point()

Now we are ready to build a scatterplot to visualise the relationship between reading enjoyment and number of books read.

Action: Insert a new code chunk and use ggplot() to tell R what data to plot and which variables tot use. Then, add layers to plot the datapoints (use geom_point()) and the line of best fit (use geom_smooth(method="lm", se=TRUE)). If you need more guidance on how to do this, look at the Hint below. If you are still stuck, you can look at the code you wrote for Lab activity 4, Step 2.

Use the function ggplot(). Start by writing ggplot() on the next line of the code chunk. Inside the ggplot() function, we need to tell R which data-frame (data=) and which variables to use (mapping=aes(x= , y= ))).

Then, we need to tell R what type of plot we want. We do this by adding a + to the line on which you’ve written ggplot(), then on the next line you add a ‘geometric object’, in this case geom_point().

We also want to add a line of best fit. We do this by adding a + to the line on which you’ve written geom_point(), then on the next line you add another ‘geometric object’, in this case geom_smooth(). We want to specify what type of line of best fit, we do this by adding method="lm", se=TRUE inside the brackets of geom_smooth().

This will give you a basic scatterplot. Revisit the lab activity code from week 1 to remind yourself how to change any of the features (such as the colour, the background, add titles to the axes etc.).

Many of the participants gave the same rating and read the same number of books, so their points sit exactly on top of each other. geom_jitter() fixes this by moving each point a tiny random amount, so overlapping points spread out and you can see how many there are.

How does geom_jitter() work?

geom_jitter() moves each point a tiny random amount, so overlapping points spread out and you can see how many there are.

You can tell R how to jitter the data points:

  • width = 0.1 is the largest random nudge to the left or right. Ratings are whole numbers, so a point at 3 is moved to somewhere between 2.9 and 3.1. This is small enough that the five vertical stripes stay clearly separate.
  • height = 0 means the points are not moved up or down. The number of books is a real count, so nudging points vertically would make the plot show values that aren’t in the data.
  • alpha = 0.6 makes the points partly transparent (0 is invisible and 1 is solid). Where points overlap, the colour becomes darker, which gives a second clue about where the data are concentrated.

Jittering only changes how the plot looks. The data and the correlation are not affected.

Action: Instead of using geom_point() in the scatterplot, try geom_jitter(width = 0.1, height = 0, alpha = 0.6).

Step 3: Calculate and interpret Pearson’s r and Spearman’s rho using correlation()

Action: Run two correlation analyses: one with Pearson’s r, the other one with Spearman’s rho. If you can’t remember how to do this, look at the Hint below. Give it a go. If you are still stuck, look at the code you wrote for Lab activity 3, Step 3. When calculating Spearman’s rho, specify “spearman” under method instead of “pearson”.

Use the correlation() function we used last week. You need to specify the following information inside the correlation() function:

  • data: data = data_revision_exam
  • variables: select = c(“revision_hours”, “exam_mark”)
  • method = “pearson” or “spearman”
  • p_adjust = “none”

Questions Step 3

  1. What is the value for Pearson’s r, is it significant?
  2. What is the value for Spearman’s rho, is it significant?
  3. What is the correct correlation coefficient to use. Explain why.

Answers

Tip

Answers to the questions in lab activities will be shared after the final lab session of the week has concluded (Thursday afternoon)

Back to top