Correlation

Task 1: Lines

A regression line goes through the points (x = -1, y = -0.8) and (x = 4, y = -8.3). What are the y-intercept and slope of the line?

line_y_intercept <- -2.3
line_slope <- -1.5

Task 2: Construct a covariance matrix

You read a study that reports a correlation between X and Y of \(r = 0.2\), and the following descriptive statistics: mean of \(X = 14\) (\(SD = 4.3\)), mean of \(Y = 64\) (\(SD = 8.5\)). Construct a covariance matrix that you could potentially use to simulate data from this study.

my_cvmx <- matrix(c(18.49, 7.31, 7.31, 72.25), nrow = 2)

Now simulate 10 observations from that study.

# do not delete the following line
# needed for seeding the random number generator
set.seed(1451)
library(MASS)
simulated_obs <- MASS::mvrnorm(10, mu = c(14, 64), Sigma = my_cvmx)

Regression

Task 3: Belenky et al. data

The R package lme4 has a data object named sleepstudy that has data from a study by Belenky et al. looking at the effects of sleep deprivation on reaction time.

Please read the help page for the sleepstudy dataset before attempting the problems below. You can access the help page by typing help("sleepstudy", package="lme4") in the console window. (DO NOT put the call to help() in your R Markdown script!) Note that you will have to load the package into your session using library() to access the data.

IMPORTANT: The variable Days in the dataset is erroneously described as “Number of days of sleep deprivation”, but it is actually the number of days in the study for a given participant, with the first day coded as 0. The first night of sleep deprivation happened on Day 2 of the study, after the reaction time measurement. Days 0-2 were ‘training and adaptation’ to the lab environment. That means that on Day 3, reaction time was measured after 1 day of deprivation; on Day 4, after 2 days, etc.

The dataset has data for 18 different participants, but for all the questions below, you should limit your analysis to the data for a single participant: Subject 332.

Task 3.1: Create a scatterplot of this participant’s data using ggplot() from the ggplot2 package (part of the tidyverse).

library(ggplot2)
library(lme4)
## Loading required package: Matrix
data("sleepstudy")
subject_data <- subset(sleepstudy, Subject == 332)

ggplot(subject_data, aes(x = Days, y = Reaction)) +
  geom_point() +
  labs(title = "Reaction Time vs Days of Sleep Deprivation",
       x = "Days of Sleep Deprivation",
       y = "Reaction Time (ms)")

Task 3.2: Create a regression model predicting Reaction from the number of nights of sleep deprivation.

The variable ss_mod should contain the fitted model object (i.e., the result of the call to the modeling function).

ss_mod <- lm(Reaction ~ Days, data = subject_data)

Task 3.3: Print out a summary of the model object.

summary(ss_mod)
## 
## Call:
## lm(formula = Reaction ~ Days, data = subject_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -96.488 -24.665  -6.256  15.445 132.510 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  264.252     35.792   7.383 7.74e-05 ***
## Days           9.567      6.704   1.427    0.191    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 60.9 on 8 degrees of freedom
## Multiple R-squared:  0.2029, Adjusted R-squared:  0.1032 
## F-statistic: 2.036 on 1 and 8 DF,  p-value: 0.1914

Task 3.4: What would the model predict for Reaction if the participant was kept in the lab for an extra night of sleep deprivation?

reaction_extra_day <- predict(ss_mod, newdata = data.frame(Days = max(subject_data$Days) + 1))

Task 3.5: Model prediction

On what day (or days) of the study was the model’s predicted value farthest away from the observed value?

subject_data$residuals <- residuals(ss_mod)
day_furthest_pred <- subject_data$Days[which.max(abs(subject_data$residuals))]

On what day (or days) of the study was the model’s predicted value closest to the observed value?

day_closest_pred <- subject_data$Days[which.min(abs(subject_data$residuals))]

Task 4: Simulating regression data

Imagine that you just read a report about a psychology experiment that looks at the effect of alcohol consumption on response time. The researchers went to Sauchiehall street in Glasgow on a Saturday night, used a breathalyzer to measure 100 participants’ blood-alcohol level, and then had them perform a series of tests on a tablet computer, which allowed the measurement for each participant’s mean response time.

The researchers report a mean blood-alcohol content (BAC) of .10% with a standard deviation of .04%.

The research regressed mean response time (meanRT) on blood-alcohol level (BAL) and obtained the following output.

Call:
lm(formula = meanRT ~ BAC, data = sdat$dat)

Residuals:
    Min      1Q  Median      3Q     Max 
-97.205 -25.999  -3.229  28.469  81.532 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)   391.62      12.64  30.989  < 2e-16 ***
BAC           572.26     116.81   4.899 3.81e-06 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 42.16 on 98 degrees of freedom
Multiple R-squared:  0.1967,    Adjusted R-squared:  0.1885 
F-statistic:    24 on 1 and 98 DF,  p-value: 3.808e-06

Simulate data from this study. The variable simdata should be a data frame (i.e., “tibble” containing simulated data). The variable names should match the names in the output.

set.seed(1451)  # Seed for reproducibility


n <- 100  # Number of participants
mean_BAC <- 0.10  # Mean blood-alcohol content
sd_BAC <- 0.04  # Standard deviation of BAC
intercept <- 391.62  # Intercept from regression
slope <- 572.26  # Slope from regression
residual_sd <- 42.16  # Residual standard error


BAC <- rnorm(n, mean = mean_BAC, sd = sd_BAC)


meanRT <- intercept + slope * BAC + rnorm(n, mean = 0, sd = residual_sd)


simdata <- data.frame(BAC, meanRT)

Task 5: Compute regression from correlation statistics

A study reports the following descriptive statistics (knit to view):

parameter value
\(\bar{X}\) -21
\(S_X\) 5.7
\(\bar{Y}\) 78
\(S_Y\) 1.3
\(r_{XY}\) -0.6

Based on these values, calculate the intercept (int) and slope (slope) coefficients you would expect from a regression on the same data.

x_bar <- -21  # Mean of X
y_bar <- 78   # Mean of Y
sd_x <- 5.7   # Standard deviation of X
sd_y <- 1.3   # Standard deviation of Y
r_xy <- -0.6  # Correlation between X and Y


slope <- r_xy * (sd_y / sd_x)
int <- y_bar - slope * x_bar

Validation

You’re finished! No need to alter anything in this final section. It is here to help ensure that all the assessment variables have been defined properly. Knit your file and check the resulting table below.

Have all relevant variables been defined? yes

Task Test Result
1 Variable line_y_intercept is of type numeric yes
1 Variable line_slope is of type numeric yes
2 Variable my_cvmxis a matrix yes
3.4 Variable reaction_extra_day of type numeric yes
3.6 Variable day_furthest_pred of type numeric yes
3.6 Variable day_closest_pred of type numeric yes
4 Variable simdata is a data frame yes
5 Variable int is of type numeric yes
5 Variable slope is of type numeric yes