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?
Slope: 35.0089 Interceot: -652.78714 r: 0.9595061.
ggplot(Perch, aes(x = Length, y = Weight)) +
geom_point() +
geom_abline(intercept = -652.789714, slope = 35.0069,
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.0069 * 8.8 + (-652.789714))
## [1] 350.629
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?
Above the line (Positive), by 350.6088. It predicts 350 grams in wieght. It sits as an outlier from the rest of that data points but hypothetically could still make sense.
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, the lower flipper lengths and the higher flipper lengths both sat above the line as too high. The medium flipper lengths sat below the line as too low. A straight line is not a good model for these 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?
The R-squared is 0.9207, it does match step 1. 350.0069/98.82 = 3.54 Mass in grams is strongly correlated to flipper lenghth in centimeters. For the residal standard error, Data points are more scattered around the line; meaning the predictions are less precise. It will be off by an average of 350.0069.