Goals for Review:

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:

I will first check the working directory and make sure the file is in my downloads.

getwd()
## [1] "/Users/owner/Downloads"

1) [5 pts] Correlation: car weight and fuel efficiency

How is a car’s weight related to its fuel efficiency?

1a. [1 pt] Make a scatterplot with wt on the x-axis and mpg on the y-axis. (If you’d like to load packages with library(), please do so in this chunk.) In one sentence, describe the pattern you see.

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.

1b. [1.5 pts] Use cor() to compute the correlation between wt and mpg. Describe the direction and strength of the relationship.

Creating the scatterplot:

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.

1c. [1.5 pts] Create a correlation matrix for mpg, wt, hp, and qsec, rounded to 2 decimals. Which pair of variables has the strongest relationship, and which has the weakest? Briefly explain how you decided.

Calculating the correlation between weight and mpg:

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.

1d. [1 pt] Create z-scores for wt and mpg. Then use them to compute r by hand: add up the products of the z-scores and divide by n − 1. Does your answer match 1b?

Calculating the correlation using z-scores

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.

2) [5 pts] Simple linear regression: predicting fuel efficiency from weight

Can a car’s weight be used to predict its fuel efficiency?

2a. [1 pt] State the null and alternative hypotheses for the slope, in symbols and in words.

Your answer:

H0: β1 = 0 | The car weight does not significantly predict fuel efficiency.

HA: β1 != 0 | The car weight significantly predicts fuel efficiency.

2b. [1.5 pts] Fit a simple linear regression predicting mpg from wt and display the results with summary(). Interpret the slope in a sentence (include units), report b, t(df), and the p-value, and say whether you reject or fail to reject H0.

The fit regression model and slope interpretation:

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.

2c. [1.5 pts] Find the predicted mpg for cars that weigh 2,000, 3,000, and 4,000 lbs. Do it two ways: by hand using coef(), and with predict(). Do the two methods match? (Hint: check the units of wt.)

Predicting the fuel efficiency at three weights:

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.

2d. [1 pt] What is R² for this model, and what does it mean? Square your r from 1b. What do you notice? Finally, does the intercept have a meaningful interpretation here? Why or why not?

Interpreting R-squared value and intercept:

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.