Simulate data for two experiments and compute test-retest reliability
Practice some tidyverse (pivot_longer, mutate, select, and add onto existing base ggplot skills (geom_point, geom_jitter, facet_wrap, geom_line)
Run a basic correlation (cor.test and interpret differences in observed reliability based on differences in the simulated data)
Import the libraries we need
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.4 ✔ readr 2.1.5
✔ forcats 1.0.0 ✔ stringr 1.5.1
✔ ggplot2 3.5.1 ✔ tibble 3.2.1
✔ lubridate 1.9.3 ✔ tidyr 1.3.1
✔ purrr 1.0.2
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggplot2) # plotting
Define the simulation function - same as before
This makes “tea data”, a tibble (dataframe) where there are a certain number of people in each condition (default = 48, i.e., n_total, with n_total/2 in each half)
The averages of the two conditions are separated by a known effect (“delta”) with some variance (“sigma”). You can change these around since we’re simulating data!
set.seed(123) # good practice to set a random seed, or else different runs get you different results
make_tea_data <-function(n_total =48,sigma =1.25,delta =1.5) { n_half <- n_total /2tibble(condition =c(rep("milk first", n_half), rep("tea first", n_half)),rating =c(round(rnorm(n_half, mean =3.5+ delta, sd = sigma) ),round(rnorm( n_half, mean =3.5, sd = sigma )))) |>mutate(rating =if_else(rating >10, 10, rating),# truncate if greater than max/min of rating scalerating =if_else(rating <1, 1, rating))}
Make a dataframe with our simulated data
Input more participants (50 per condition) with a bigger average difference between conditions (2 points), with variance between participants at 2 points (sigma)
Create a new colummn (sub_id) that is based on the row number in the dataframe Hint: rowname_to_column() is a useful function
Unselect the “rowname” column, cleaning up your dataframe
# carete a dataframe with 50 participantstea_data_50 <-make_tea_data(n_total =50,sigma =2,delta =2)# create a new columntea_data_50_new <-rownames_to_column(tea_data_50,var='sub_id')# unselect the "rowname" columntea_data_50_new <-remove_rownames(tea_data_50_new)
tea_data_50_new
# A tibble: 50 × 3
sub_id condition rating
<chr> <chr> <dbl>
1 1 milk first 4
2 2 milk first 5
3 3 milk first 9
4 4 milk first 6
5 5 milk first 6
6 6 milk first 9
7 7 milk first 6
8 8 milk first 3
9 9 milk first 4
10 10 milk first 5
# ℹ 40 more rows
Creating the second experiment
Now create a new column in your tibble for the second experiment.
Here, the rating of the simulated second experiment data is each participants first rating with some variance (people are likely to not say exactly the same thing)
TIPS:
Strongly recommend running rowwise() in your pipe before creating the new condition to force tidyverse to sample a new random value for each row
Make your next dataframe A NEW NAME so that you’re not rewriting old dataframes with new ones and getting confused
Hint: you can use to_sample = -3:3 with the sample function and specifies the possible values you want to sample from
# YOUR CODE HERE# create a sampling size rangeto_sample =-3:3# create a new simulation dataframenew_tea_data <- tea_data_50_new |>rowwise() |>rename(rating_exp_1 = rating) |>mutate(difference =sample(to_sample,1)) |>mutate(rating_exp_2 = rating_exp_1 + difference) |># make sure ratings can't go above/below limits of scalemutate(rating_exp_2 =if_else(rating_exp_2 >10, 10, rating_exp_2),# truncate if greater than max/min of rating scalerating_exp_2 =if_else(rating_exp_2 <1, 1, rating_exp_2))
Make a plot
Examine how the ratings are correlated across these simulations, by making a plot
Hint: 1. Try out geom_point which shows you the exact values 2. Then try out geom_jitter which shows you the same data with some jitter around height / width
Bonus: 3. Use alpha to make your dots transparent 4. Use ylab and xlab to make nice axes labels 5. Use geom_smooth() to look at the trend_line 6. Try making each dot different by condition
Now examine – how correlated are your responses? What is your test-retest reliability?
Hint: You’ll need a correlation function - cor.test Bonus: You can also find a ggplot function to overlay the correlation on top of your plot
Make another plot – like the one from the chapter, where each line should connect an individual subject
First, Use pivot_longer to make the dataframe longer
Use facet_wrap(~condition) to make two plots, one for milk_first and one for tea_first
Use geom_line – with grouping specification by sub_id – to connect individual datapoints for each condition
Hint aes(group = sub_id))
# YOUR CODE HERE# Make new, longer dataframe
# YOUR PLOTTING CODE HERE
OK, now go back and change things and test your intuition about how this works.
How does reliability change if you increase the variance between participants (sigma) in the first experiment simulated data?
How does reliability change if you change how much variation you allow between the first and second experiment?
How does reliability change if you increase sample size, holding those things constant?
Hint: copy the code from above where you made your new dataframe with experiment number 2, copy the correlation computation code, and just run this block over an over, modifying the code (command-shift-enter on Mac runs the block)