Modeling the relationship between a predictor (X) and a response (Y) using a straight line.
2026-04-13
Modeling the relationship between a predictor (X) and a response (Y) using a straight line.
A statistical method for describing the relationship between two variables
One predictor variable (X) is used to explain or predict one response variable (Y)
The relationship is modeled with a straight line
Example:
\[ y = \beta_0 + \beta_1 x + \epsilon \]
\[ \beta_1 = \frac{\Delta y}{\Delta x} \]
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point(size = 2) +
geom_smooth(method = "lm", se = FALSE) +
labs (
x = "Weight (1000 lbs)",
y = "Miles Per Gallon"
) +
theme_minimal()
model <- lm(mpg ~ wt, data = mtcars)
ggplot(mtcars, aes(x = wt, y = resid(model))) +
geom_point(size = 2) +
geom_hline(yintercept = 0, linetype = "dashed") +
labs(
x = "Weight (1000 lbs)",
y = "Residuals"
) +
theme_minimal()
# Create Linear Regression Model model <- lm(mpg ~ wt, data = mtcars) # View Model Summary coef(model)
## (Intercept) wt ## 37.285126 -5.344472
# Generate Predictions predicted_mpg <- predict(model) # Show First Few Predicted Values head(predicted_mpg)
## Mazda RX4 Mazda RX4 Wag Datsun 710 Hornet 4 Drive ## 23.28261 21.91977 24.88595 20.10265 ## Hornet Sportabout Valiant ## 18.90014 18.79325
p <- plot_ly( data = mtcars, x = ~wt, y = ~mpg, type = "scatter", mode = "markers" ) p