- Goal: To understand how car weight affects fuel efficiency (MPG)
- Method: Simple Linear Regression (SLR) using the
mtcarsdataset - Tools used: R, ggplot2, and plotly
mtcars datasetWe represent the relationship as: \[ Y_i = \beta_0 + \beta_1 X_i + \varepsilon_i,\quad i=1,2,\dots,n. \] Here, \(Y\) = Miles per Gallon (MPG), \(X\) = Weight of car in 1000 lbs.
To test if a linear relationship exists: \[ H_0: \beta_1 = 0 \quad \text{vs.} \quad H_a: \beta_1 \neq 0 \] If we reject \(H_0\), we conclude that car weight significantly influences MPG.
ggplot(df, aes(Weight_1000lb, MilesPerGallon)) + geom_point() + geom_smooth(method = "lm", se = TRUE) + labs(x = "Weight (1000 lb)", y = "MPG", title = "Fuel Efficiency vs. Weight")
ggplot(aug, aes(.fitted, .resid)) + geom_hline(yintercept = 0, linetype = "dashed") + geom_point() + labs(x = "Fitted MPG", y = "Residuals", title = "Residuals vs Fitted Values")
ggplot(aug, aes(sample = .resid)) + stat_qq() + stat_qq_line() + labs(x = "Theoretical Quantiles", y = "Sample Quantiles", title = "Q-Q Plot of Residuals")
plot_ly(df,
x = ~Weight_1000lb,
y = ~Horsepower,
z = ~MilesPerGallon,
type = "scatter3d",
mode = "markers")