This week’s assignment is short so you have time for the R Take-Home Exam. Rename this template as “Lab 13 Assignment [Last, First].Rmd”, fill out the author and date lines, and knit it once before you start. Submit both the knitted file and the .Rmd on Canvas. Use α = .05.
The built-in mtcars dataset (no download needed) describes 32 car models from a 1974 issue of Motor Trend magazine. We’ll use four variables:
getwd()
## [1] "/Users/owner/Downloads"
How is a car’s weight related to its fuel efficiency?
plot(mpg ~ wt, data = mtcars,
xlab = "Weight (1,000 lbs)",
ylab = "Miles per Gallon",
main = "Car Weight vs. Fuel Efficiency")
Your answer: ## From this plot it can be seen that as car weight increases, the fuel efficiency generally decreases, indicating a negative linear relationship.
cor(mtcars$wt, mtcars$mpg)
## [1] -0.8676594
Your answer: ## The correlation is found to be about -0.87, indicating a strong negative relationship between car weight and fuel efficiency.
round(cor(mtcars[, c("mpg", "wt", "hp", "qsec")]), 2)
## mpg wt hp qsec
## mpg 1.00 -0.87 -0.78 0.42
## wt -0.87 1.00 0.66 -0.17
## hp -0.78 0.66 1.00 -0.71
## qsec 0.42 -0.17 -0.71 1.00
Your answer: ## The results show that strongest relationship is between mpg and wt (r = -0.87). The weakest realtionship appears to be between wt and qsec (r = -0.17). I found this by comparing the absolute values of the correlations.
z_wt <- scale(mtcars$wt)
z_mpg <- scale(mtcars$mpg)
sum(z_wt * z_mpg) / (nrow(mtcars) - 1)
## [1] -0.8676594
Your answer: ## The result is about -0.87, matching the correlation calculated in 1b.
Can a car’s weight be used to predict its fuel efficiency?
Your answer:
model <- lm(mpg ~ wt, data = mtcars)
summary(model)
##
## Call:
## lm(formula = mpg ~ wt, data = mtcars)
##
## Residuals:
## Min 1Q Median 3Q Max
## -4.5432 -2.3647 -0.1252 1.4096 6.8727
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 37.2851 1.8776 19.858 < 2e-16 ***
## wt -5.3445 0.5591 -9.559 1.29e-10 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.046 on 30 degrees of freedom
## Multiple R-squared: 0.7528, Adjusted R-squared: 0.7446
## F-statistic: 91.38 on 1 and 30 DF, p-value: 1.294e-10
Your answer: ## For every additional 1,000 pounds of car weight, predicted fuel efficiency goes down by about 5.34 miles per gallon. The slope is b = -5.344, t(30) = -9.559, p = 1.29(10^-10). Because p is less than 0.05, I reject the null hypothesis.
coef(model)[1] + coef(model)[2] * c(2, 3, 4)
## [1] 26.59618 21.25171 15.90724
predict(model, newdata = data.frame(wt = c(2, 3, 4)))
## 1 2 3
## 26.59618 21.25171 15.90724
Your answer: ## The predicted mpg values are about 29.60, 24.89, and 29.55 for cars weighing 2,000, 3,000, and 4,000 pounds, respectively. Both methods should produce predictions that align.
summary(model)$r.squared
## [1] 0.7528328
cor(mtcars$wt, mtcars$mpg)^2
## [1] 0.7528328
coef(model)[1]
## (Intercept)
## 37.28513
Your answer: ## The R-squared value is about 0.753, meaning that about 75.3% of the variation in fuel efficiency is explained by car weight. Squaring the correlation from 1b results in the same R-squared value. The intercept is about 37.29, which represents predicted mpg when a car has a weight of 0 pounds. This is not meaningful since there is likely no 0-pound car.