To see how the miles per gallon (mpg) changes based on the cars weight (wt).
This will be done by using linear regression to create a scatter plot to see the relationship between gas mileage and car weight.
2026-03-04
To see how the miles per gallon (mpg) changes based on the cars weight (wt).
This will be done by using linear regression to create a scatter plot to see the relationship between gas mileage and car weight.
For the simple linear regression:
y will be treated as the mpg,
x will be treated as the wt,
epsilon will be for any errors or outliers
\[mpg = \beta_0 + \beta_1 wt + \epsilon\]
This will show how the weight of the car can determine the gas mileage
The equation to estimate the linear regression based on the data set: \[\hat{mpg} = b_0 + b_1wt\] It uses the given points to create a line of best fit, showing how the data for the cars correlate and the relationship between them.
mpg cyl disp hp drat wt qsec vs am gear carb Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
`geom_smooth()` using formula = 'y ~ x'
This is the code that was used to make the graph on the previous slide. The only changes that have been made are making the code visible and adding height and width parameters so there is a small version of the graph visible on this slide.
mod = lm(mpg ~wt, data = mtcars)
p = plot_ly(data = mtcars, x = ~wt, y = ~mpg, type = 'scatter',
mode = 'markers')%>%
add_lines(x = ~wt, y = fitted(mod)) %>%
layout(width = 350, height = 150,
showlegend = FALSE,
xaxis = list(title = "Car Weight"),
yaxis = list(title = "Miles Per Gallon")
)
p
Heavier cars are more likely to have a lower mile per gallon.
Scatter plots provide a visual to this correlation by showing a negative slope. As the weight of a car increases, the gas mileage it has decreases.