library(Stat2Data)
library(dplyr)
library(ggplot2)
data(Perch)
ggplot(Perch, aes(x = Length, y = Weight)) +
geom_point() +
labs(
title = "Length vs. Weight for Perch",
x = "Length (cm)",
y = "Weight (grams)"
)
cor(Perch$Length, Perch$Weight)
## [1] 0.9595061
model <- lm(Weight ~ Length, data = Perch)
coef(model)
## (Intercept) Length
## -652.78714 35.00089
Question 1: What are r, the slope, and the intercept? Does the sign of the slope match the sign of r?
R = 0.960 Slope: m = -652.79 Intercept: b = 35.00
ggplot(Perch, aes(x = Length, y = Weight)) +
geom_point() +
geom_abline(intercept = 35.00, slope = -652.79,
color = "steelblue", linewidth = 1) +
labs(
title = "Length vs. Weight for Perch",
x = "Length (cm)",
y = "Weight (grams)"
)
Perch %>%
select(Obs, Length, Weight) %>%
head(1)
## Obs Length Weight
## 1 104 8.8 5.9
5.9 - (35.00 * 8.8 + (-652.79))
## [1] 350.69
Question 2: Is this fish above or below the line, and by how much? What weight does the line predict for this fish, and does that prediction make sense?
My fish is way over the line by 350.69.
Perch <- Perch %>%
mutate(predicted = fitted(model),
residual = residuals(model))
Perch %>%
select(Length, Weight, predicted, residual) %>%
head()
## Length Weight predicted residual
## 1 8.8 5.9 -344.779274 350.67927
## 2 14.7 32.0 -138.274002 170.27400
## 3 16.0 40.0 -92.772841 132.77284
## 4 17.2 51.5 -50.771769 102.27177
## 5 18.5 70.0 -5.270607 75.27061
## 6 19.2 100.0 19.230019 80.76998
ggplot(Perch, aes(x = Length, y = residual)) +
geom_point(alpha = 0.6) +
geom_hline(yintercept = 0, color = "steelblue", linewidth = 1) +
labs(
title = "Residual Plot: Length Model for Perch",
x = "Length (cm)",
y = "Residual (grams)"
)
Question 3: Is the residual plot boring, curved, or fanning? For which lengths does the line predict too high, and for which does it predict too low? Is a straight line a good model for these fish?
The residual plot is curved. For short and long lengths, it underpredicts, so a straight line is not a good model for the fish.
summary(model)
##
## Call:
## lm(formula = Weight ~ Length, data = Perch)
##
## Residuals:
## Min 1Q Median 3Q Max
## -146.25 -57.86 -23.99 45.00 350.68
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -652.787 43.407 -15.04 <2e-16 ***
## Length 35.001 1.398 25.03 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 98.82 on 54 degrees of freedom
## Multiple R-squared: 0.9207, Adjusted R-squared: 0.9192
## F-statistic: 626.5 on 1 and 54 DF, p-value: < 2.2e-16
Question 4: What are R-squared and the residual standard error? Interpret each in context. Square your r from Step 1: does it equal R-squared?
R - squared = 0.921 Residual Standard Error = 98.82 The largest residual from the line is 350.68. A fish ruler is very far off, and you should trust it with caution, if at all.