DAPR1 Workshops
  • 1: Exploratory Data Analysis
    • W1: Introductions
    • W2: Data & Variables
    • W3: Visualising Distributions
    • W4: Summarising Distributions I – Central Tendency
    • W5: Summarising Distributions II – Variability
  • 2: Introduction to Inference
    • W7: Z-scores & the Normal Distribution
  • 3: Common Statistical Tests
  • 4: The Move to Modelling

W4: Summarising Distributions I – Central Tendency

Pick someone new to be the driver this week. They will type the code. The rest of you are navigators, you need to tell the driver where to go (i.e., what to type!).

Driver instructions:

  1. Log in to the bigger screen at your table
  2. Open RStudio Server. See the first drop-down in the FAQs page for instructions
  3. Open a new .R script, and load the tidyverse package.
  4. Listen to your navigators

Navigator instructions:

  1. Tell your driver where to go (i.e., what to type)!
Question 1

We’re continuing to use the same dataset for the next couple of weeks (the survey that many of you completed in the first week). The data are stored at https://uoepsy.github.io/data/dapr1_2627_survey.csv (Check back to your work from the previous two weeks to remind yourself how to get the data into R).

What was the most common month in which survey respondents were born?

🗂️ See Measure central tendency > Mode flash card.

TipHint

The variable that holds this information is the one called birthmonth. You can check what different variables are in your data by quickly using names(data), replacing “data” with whatever name you used to store your dataset in R.

Solution 1.

library(tidyverse)
sdata <- read_csv("https://uoepsy.github.io/data/dapr1_2627_survey.csv")

sdata |> 
  count(birthmonth)
# A tibble: 12 × 2
   birthmonth     n
   <chr>      <int>
 1 april          8
 2 august         9
 3 december      10
 4 february      10
 5 january       11
 6 july          12
 7 june          14
 8 march         15
 9 may           15
10 november       4
11 october       16
12 september     10

You can also arrange the counts in descending order to see the most common month at the top. To do so, pipe the output to the arrange() function, and use the desc() function to sort the counts in descending order of the column n. If you just wrote arrange(n), it would sort the counts in ascending order.

sdata |> 
  count(birthmonth) |> 
  arrange(desc(n))
# A tibble: 12 × 2
   birthmonth     n
   <chr>      <int>
 1 october       16
 2 march         15
 3 may           15
 4 june          14
 5 july          12
 6 january       11
 7 december      10
 8 february      10
 9 september     10
10 august         9
11 april          8
12 november       4

Question 2

One of the questions in the survey asked about how spicy people liked their food.
The possible responses were:

  • Anti-spice: Mayonnaise is too spicy for me; I avoid any black pepper.
  • Mild: I like a tiny hint of warmth, but flavour comes first.
  • Medium: I enjoy a solid kick that makes the meal interesting.
  • Hot: I want to feel the burn.
  • Very Hot: I want to sweat and question my life choices.
  • Extreme: I want to transcend mortal existence and taste the fury of a thousand suns.

This has been recoded for you already so that the dataset contains both a variable with the descriptive word (“medium”, “hot” etc), and a variable with a numeric value (1 being assigned to “anti-spice” and 6 being assigned to “extreme”).

For what level of spicy-ness does half of the sample fall above and half fall below?

🗂️ See Measure central tendency > Median flash card.

Solution 2.

sdata |> 
  summarise(
    spicy_Med = median(spicy_num)
  )
# A tibble: 1 × 1
  spicy_Med
      <dbl>
1         3

Question 3

Another question asked respondents to rate the quality of their sleep (from 0 to 100). On average, how well did respondents sleep? (What was the average sleep quality rating?)

🗂️ See Measure central tendency > Mean flash card.

Solution 3.

sdata |>
  summarise(
    sleep_M = mean(sleepqual, na.rm = TRUE)
  )
# A tibble: 1 × 1
  sleep_M
    <dbl>
1    55.4

Question 4

The code below creates a visualisation of the “distance-from-Edinburgh” that respondents were born (you may need to replace the sdata with whatever you named your dataset in R):

ggplot(sdata, aes(x = distance_born)) +
  geom_histogram()

Do you think the median or the mean of this variable will be higher?

Calculate both the mean and median for this variable to see if you were right.

(Challenge: calculate both summary statistics within the same instance of summarise())

Solution 4.

sdata |>
  summarise(
    dist_M = mean(distance_born, na.rm = TRUE),
    dist_Med = median(distance_born, na.rm = TRUE)
  )
# A tibble: 1 × 2
  dist_M dist_Med
   <dbl>    <dbl>
1  3111.      657

Question 5

Choose any of the variables in the data that we haven’t used so far (excluding the pseudonym and threewords variables!).

Calculate an appropriate summary statistic of central tendency for your chosen variable.

Next, do the above separately for the people who responded “cat”, “dog”, “both” or “neither” to the question “Dog or Cat?” (this is the catdog variable in the dataset).

🗂️ See Measure central tendency flash card - it also covers how to calculate measures by-groups.

Solution 5.

sdata |>
  group_by(catdog) |>
  summarise(
    sleepqual = mean(sleepqual, na.rm = TRUE)
  )
# A tibble: 4 × 2
  catdog  sleepqual
  <chr>       <dbl>
1 both         54.7
2 cat          54.5
3 dog          55.4
4 neither      65.5

Finishing up

Once you’ve finished, don’t forget to:

  1. Save your R script
  2. Post the R script to the group discussion space on Learn, to ensure that everyone has access to it! (For step-by-step instructions on how to do this, click here)