Objectives
By the end of this assignment, you should:
- understand how to read in data using
read_csv
- understand how to derive information from data (
summarize
, mutate
, group_by
)
- understand how to make and modify a basic plot using ggplot
This assignment is due Thursday, September 23 at noon. Please turn your .html AND .Rmd files into Canvas. Your .Rmd file should knit without an error before turning in the assignment. Note that for all the questions requiring code, you should use tidyverse functions.
This assignment focuses on data from a recent paper examining the role that songs play in soothing infants (Bainbridge & Bertolo et al., 2021). To get started, you should give the paper a read and broadly understand the question the paper is trying to answer and the methods that they used (note that the methods are described in detail at the end of the paper). You’ll then need to make a .Rmd document. You can start by using the template from the previous assignment and modifying it as appropriate (including file name, title, your name, etc).
- [a] What is the main question that the experiment is designed to address?
[b] What did the experimenters manipulate? (the independent variable)
[c] What are three variables the researchers measured? (the dependent variables)
[d] The authors consider an alternative hypothesis for their heart rate results. What is this hypothesis?
In this assignment, we’ll focus on the heart rate data. You can download a lightly cleaned version of their heart rate data here:
bb2021 <- read_csv("https://raw.githubusercontent.com/mllewis/cumulative-science/master/static/data/bb_2021_hr_clean.csv")
(if you’re curious, you can explore all their raw data by going to the repository associated with the paper, here).
There are seven variables in the data and each variable is described below. The first six rows of the data frame are also displayed below.
- participant_id - Unique identifier for each infant.
- age - Age of infant as continuous variable in months.
- age_cat - Age of each participant as discrete variable in months.
- trial_type - Trial type (lullaby vs. non-lullaby). They also had “preference” trials in the experiment. Those trials are not included in this dataset.
- trial_id - Trial identifier. Note that the number of trials varies across participants. For some participants there are data for 6 trials, while for others there are data for only 4.
- obs_num - For each trial, they measured heart rate roughly every .4 seconds. This variable tells you which observation in a trial you’re looking at (.4 seconds after the trial started would be coded as
1
, .8 seconds after the trial started would be coded as 2
, etc.).
- zhr_pt - This is the heart rate at a given observation, normalized relative to the previous trial.
- Is this data tidy? What is the unit of observation?
- Let’s start by trying to understand the structure of the dataset. Calculate the following:
[a] The age of the youngest (minimum) child in the dataset.
[b] The age of the oldest (maximum) child in the dataset.
[c] The total number of observations represented in the data.
- Create a dataframe with only the first observation from the first trial for each participant. Uses this dataframe to answer the following questions:
[a] How many participants are present in the dataset?
[b] How many 7-month-olds are there?
[c] Arrange the dataframe from youngest to oldest. What’s the participant_id for the youngest infant?
[d] Arrange the dataframe from oldest to youngest. What’s the participant_id for the oldest infant?
- What is the mean number of observations per individual trial? (hint: you’ll need to use both
group_by
and ungroup
).
- How many observations are there in the lullaby condition and the non-lullaby condition?
- Next, let’s examine the dependent variable, heart rate. Create a new variable called
hr_round
that is the heart rate value rounded to the nearest hundredth (use the function round()
).
- Plot a histogram of
hr_round
using the geom, geom_histogram
. Be sure to add an appropriate title to your plot.
- Calculate the mean heart rate for each participant on each trial type. Save it to a new dataframe called
participant_means
.
- Use
participant_means
to create a violin plot showing the distribution of heart rates in the lullaby and non-lullaby conditions. Your plot should be a simplified version of Figure 2a in the paper with (a) two violins, (b) each violin a different color, and (c) points showing the underlying data. (hint: the order that you add geoms to your plot matters!).
- Use
participant_means
to calculate the overall means in the lullaby and non-lullaby conditions. Save this to a new dataframe called condition_means
, and plot the two means as a bar plot of different colors. (hint: use geom_bar(stat = "identity")
).
- Create the plot below showing the mean heart rate by condition across trials. For extra credit, change the point size such that it corresponds to the number of trials represented.
- Choose your own geom! Look at the geoms on the ggplot cheatsheet and choose one to use with this data (other than geom_line, geom_point, geom_violin, and geom_bar). Make a beautiful, clear plot. Make sure to include a descriptive title.