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?
r is 0.96, the intercept is -652.79, and the slope is 35.
ggplot(Perch, aes(x = Length, y = Weight)) +
geom_point() +
geom_abline(intercept = -652.79, slope = 35,
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 * 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?
The fish is above the line by 350.69 grams. This makes sense because the line is far below the point of the first fish.
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 plot is very interesting with a weird and sharp curve. It starts linear and then disperses after the curve. For the lower lengths and the higher lengths, most residuals are above the line. However, for the mid-lengths, the residuals are below the line. A straight line would not be 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 is 0.92 and the standard error is 98.82. R-squared is equal to 0.96^2. R-squared being 0.92 expresses that about 92% of our data variance is explained. THe standard error tells us that the common prediction is usually above or below the line by 98.82.
Question 5: How many residual standard errors is the largest residual from the line? Is it surprising? Then answer our big question in your own words, using your residual plot, R-squared, and the residual standard error: how far off is a fish ruler, and how much should you trust it? 350.69/98.82 = 3.55 residual standard errors. It’s surprising because it’s just a big residual but it seems accurate. Based on our data, I would say I trust how far off a fish ruler is a decent amount.