penguins <- na.omit(penguins)
load("~/MAT104-Fall2024/Week12/parenthood.Rdata")
Let’s go back to the parenthood data we used last class:
ggplot(parenthood, aes(x=dan.sleep,y=dan.grump))+geom_point()
The data looks like there is a linear relationship between dan.sleep
and dan.grump
, so let’s add a line to our plot that represents this relationship.
To make an equation for a line we use the slope-intercept formula:
\[y=mx+b\] where \(m\) is the slope of the line and \(b\) is the \(y\)-intercept.
In statistics, we typically use different letters but the equation is the same: \[\hat{Y_i} = b_1 X_i + b_0\] where \(b_1\) is the slope and \(b_0\) is the \(y\)-intercept. The values \(b_1\) and \(b_0\) are called coefficients.
\(b_1=-10\) for every additional hour of sleep Danielle is 10 units less grumpy or (has 10 less grumps)
\(b_0 = 135\) when Danielle stays up all night she has 135 grumps
\(X_i\) number of hours Danielle slept on the i-th day
\(\hat{Y}_i\) is how grumpy we predict Danielle will be on the i-th day
Let’s add the line we predicted to the graph
ggplot(parenthood, aes(x=dan.sleep,y=dan.grump)) +
geom_point() +
geom_abline(intercept = 135, slope = -10, color="blue")
The graph below displays the error between our prediction and the true data in red:
# Use our slope and intercept guesses to predict the y-values
predictions <- data.frame(Y_hat = -10*(parenthood$dan.sleep) + 135)
# Plot segments joining the actual points and the predicted points
ggplot(parenthood, aes(x=dan.sleep,y=dan.grump)) +
geom_point() +
geom_abline(intercept = 135, slope = -10, color="blue") +
geom_segment(aes(xend = dan.sleep, yend = predictions$Y_hat, color = "residual"))
To lengths of these little red segments are called residuals. We can find how big the residuals are by finding the difference between the predicted value and the actual value:
\[\epsilon_i = Y_i - \hat{Y}_i\]
56 - 59.1
## [1] -3.1
Putting these ideas together, we can see that our real data is equal to our prediction plus the residuals:
\[Y_i = \hat{Y}_i + \epsilon_i = b_1X_i + b_0 + \epsilon_i\]
\[ \sum \epsilon_i^2 = \sum (Y_i - \hat{Y}_i)^2\]
#find the sum of the squared residuals
sum((parenthood$dan.grump - predictions$Y_hat)^2)
## [1] 2222.52
# 2222.52
# the R function to find the best linear model is lm()
lm(parenthood$dan.grump ~ parenthood$dan.sleep)
##
## Call:
## lm(formula = parenthood$dan.grump ~ parenthood$dan.sleep)
##
## Coefficients:
## (Intercept) parenthood$dan.sleep
## 125.956 -8.937
ggplot(parenthood, aes(x=dan.sleep,y=dan.grump)) +
geom_point() +
geom_abline(intercept = 135, slope = -10, color="blue") +
geom_abline(intercept = 125.956, slope = -8.937, color="green")
# new sum of the square residuals
predictions2 <- data.frame(Y_hat = -8.937*(parenthood$dan.sleep)+ 125.956)
sum((parenthood$dan.grump - predictions2$Y_hat)^2)
## [1] 1838.714
# this is much smaller than 2222, so this is a better model. IN fact, this is the best we can possibly do with a straight line.
The model we use for two predictors is: \[\hat{Y}_i = b_2 X_{i,2} + b_1 X_{i,1} + b_0\]
where \(X_{i,2}\) is the amount of sleep the baby got on day \(i\) and \(X_{i,1}\) is the amount of sleep Danielle got on day \(i\).
To find the best fitting regression line we use lm()
:
lm(parenthood$dan.grump ~ parenthood$dan.sleep + parenthood$baby.sleep)
##
## Call:
## lm(formula = parenthood$dan.grump ~ parenthood$dan.sleep + parenthood$baby.sleep)
##
## Coefficients:
## (Intercept) parenthood$dan.sleep parenthood$baby.sleep
## 125.96557 -8.95025 0.01052
# the linear model associated to this would be
# Y_hat = -8.95*X_1 + 0.0105 * X_2 + 125.96557
scatter3d(dan.grump ~ dan.sleep + baby.sleep, parenthood)
## Loading required namespace: mgcv
## Loading required namespace: MASS
rglwidget()
$_0 = $
$_1 = $
Find the sum of the square residuals for the model in exercise 1.
Use cor()
to find the strength of the correlation between bill length and body mass.
Use simple linear regression to model the relationship between flipper length and body mass for the penguin data. What values do you get for \(\hat{b}_0\) and \(\hat{b}_1\)? Plot a scatter plot for the data with the line you found.
Use multiple linear regression to model the body mass using the predictors flipper length and bill length for the penguin data. Assuming \(X_1\) is flipper length and \(X_2\) is bill length, what values do you get for \(\hat{b}_0\), \(\hat{b}_1\), and \(\hat{b}_2\)?