Activity: Practice pivoting with the dog data

Instructions:

Dog data

On the first day of class, we discussed an experiment which investigated whether interacting with dogs can help exam stress for college students. After cleaning the raw measurements, the experiment data looks like this:

head(cleaned_data)
  RID GroupAssignment pa_pre pa_post happiness_pre happiness_post sc_pre
1   1         Control    3.2     3.8      2.333333       3.333333   3.90
2   2          Direct    3.0     3.2      3.333333       4.000000   5.15
3   3        Indirect    2.8     3.0      2.666667       3.333333   4.10
4   4         Control    4.2     3.8      3.000000       3.000000   4.65
5   5          Direct    3.4     4.0      2.666667       2.666667   3.65
6   6        Indirect    4.2     4.4      3.000000       3.333333   4.35
   sc_post fs_pre fs_post stress_pre stress_post homesick_pre homesick_post
1 3.800000  6.125   6.000          2           2            3             3
2 5.263158  5.250   6.000          2           1            4             2
3 4.150000  5.375   5.375          4           3            3             2
4 5.100000  6.375   6.375          2           2            1             1
5 3.600000  5.875   5.875          3           4            2             2
6 4.650000  6.125   5.625          4           4            4             3
  lonely_pre lonely_post na_pre na_post
1       2.25        1.70    1.0     1.2
2       1.90        1.60    1.8     1.0
3       2.25        2.25    1.6     1.6
4       1.75        2.05    1.8     1.6
5       2.85        2.70    2.2     1.6
6       2.70        2.40    2.0     1.4

Each row of the cleaned data represents one student, with columns for student ID (RID), their experimental group assignment (Control means handler-only contact, Indirect means a dog was present, Direct means direct contact with a dog), and their pre- and post-intervention scores for different wellbeing and illbeing measurements.

For example, happiness_pre is the student’s assessed happiness before the intervention, and happiness_post is the student’s assessed happiness after the intervention.

The data can be imported into R with the following code:

library(readr)

cleaned_data <- read_csv("https://sta279-f25.github.io/class_activities/cleaned_dog_data.csv")

Pivoting

  1. Reshape the cleaned_data in R so it looks like this (note that I am only displaying the first few rows):
  RID GroupAssignment measurement stage    score
1   1         Control          pa   pre 3.200000
2   1         Control          pa  post 3.800000
3   1         Control   happiness   pre 2.333333
4   1         Control   happiness  post 3.333333
5   1         Control          sc   pre 3.900000
6   1         Control          sc  post 3.800000

Reshape the cleaned_data in R so it looks like this (note that I am only displaying the first few rows):

  RID GroupAssignment stage  pa happiness       sc    fs stress homesick lonely
1   1         Control   pre 3.2  2.333333 3.900000 6.125      2        3   2.25
2   1         Control  post 3.8  3.333333 3.800000 6.000      2        3   1.70
3   2          Direct   pre 3.0  3.333333 5.150000 5.250      2        4   1.90
4   2          Direct  post 3.2  4.000000 5.263158 6.000      1        2   1.60
5   3        Indirect   pre 2.8  2.666667 4.100000 5.375      4        3   2.25
6   3        Indirect  post 3.0  3.333333 4.150000 5.375      3        2   2.25
   na
1 1.0
2 1.2
3 1.8
4 1.0
5 1.6
6 1.6
  1. Using your reshaped data from question 2, fit a linear model with happiness as the response variable and GroupAssignment and stage as the explanatory variables. Would you have been able to fit this model without pivoting the data?