library(tidyverse)
options(knitr.kable.NA = '')

1

(The code for the scatterplot is below.) Based on the the LOESS curve, sleep is positively related with performance from very few hours of sleep (about 3) through about 6 or so, then the relationship flattens, and it eventually becomes more negative after about 8.5 hours.

sleep <- read_csv("https://whlevine.hosted.uark.edu/psyc5143/sleep.csv")

sleep %>% ggplot(aes(x = Sleep, y = Performance)) +
    geom_point() +
    geom_smooth(method = 'loess', se = F)

2

Mean-centering seems like a good idea. No one in the sample had 0 hours of sleep, so it would generate an intercept that is about no one. Based on the code below, the intercept of 78.2 is predicted performance on the exam for someone who had an average number of hours of sleep (i.e., 6.63 hours). The slope is 2.96, which suggests an expectation of about 3 additional points on the exam for every additional hour of sleep.

sleep <- sleep %>% mutate(sleep.c = Sleep - mean(Sleep))

mean(sleep$Sleep) #6.63
mean(sleep$Performance) # 78.2

linearModel <- lm(Performance ~ sleep.c, sleep)
coef(linearModel)

3

The intercept of the quadratic model is 83.07, which is the predicted score for someone with an average number of hours of sleep. The linear slope is 2.91, which is the (point/instantaneous) slope of a line tangent to the quadratic at the mean number of hours of sleep. The quadratic sloe is -1.30, which indicates that the slope of the sleep-performance relationship decreases as sleep increases. The graph below shows this nicely.

sleep <- sleep %>% mutate(sleep.c2 = sleep.c^2)

quadModel <- lm(Performance ~ sleep.c + sleep.c2, sleep)
coef(quadModel)
sleep %>% ggplot(aes(x = Sleep, y = Performance)) +
  geom_point() +
  geom_smooth(method = "lm", formula = y ~ x + I(x^2), se = F) +
  theme_bw()

4

The easiest way I know to do this is to center sleep at 8 hours and refit the model, for which the linear slope will answer the question. The code below does this. The point slope at 8 hours of sleep is -0.637.

Another way to do this is to find the derivative of the regression equation and plug in 8. I’ll do this here. Rather than fuss with centered units of sleep, I’ll do this with raw sleep. Here’s the regression equation:

\(\hat{Y} = 6.729 + 20.111 \times sleep + (-1.297) \times sleep^2\)

The derivative is

\(20.111 - 1.297 \times 2 \times sleep\)

Substituting 8 for sleep gives

\(20.111 - 1.297 \times 2 \times 8\) = -0.641

I believe that the difference is due to rounding.

sleep <- sleep %>% mutate(sleep8 = Sleep - 8)
quad8model <- lm(Performance ~ sleep8 + I(sleep8^2), sleep)
coef(quad8model)

rawModel <- lm(Performance ~ Sleep + I(Sleep^2), sleep)
coef(rawModel)

For a rabbit-hole experience, take a look at the following R code, which shows how to find a derivative of an expression in R and how to evaluate it for a particular value.

# store the regression equation in an 'expression' object
f <- expression(6.729 + 20.111*x + -1.297*x^2)

# store the value of x that we'll evaluate
x <- 9

# find the derivative of the expression and evaluate it when x = 8
deriv(f, "x") %>% eval()

5

Since we know that the derivative gives the point slope, we can solve it when it equal 0:

\(0 = 20.111 - 1.297 \times 2 \times sleep\)

Simplifying

\(0 = 20.111 - 2.59 \times sleep\)

Subtract 20.11 from both sides

\(-20.111 = -2.59 \times sleep\)

Divide both sides by -2.59

\(7.76 = sleep\)

Verify by centering and finding the point slope:

sleep <- sleep %>% mutate(sleepMax = Sleep - 7.76)

lm(Performance ~ sleepMax + I(sleepMax^2), sleep) %>% coef()
# the point slope is -0.015; I think there are minor rounding discrepancies