Linear regression is a simple way to see how one variable changes with another. We basically draw a straight line that best fits the data points.
2025-11-16
Linear regression is a simple way to see how one variable changes with another. We basically draw a straight line that best fits the data points.
The model looks like this:
\[ y = \beta_0 + \beta_1 x + \varepsilon \]
All this means is that y changes based on x plus some error.
The slope tells us how much y changes when x changes by 1 unit.
\[ \hat{\beta}_1 = \frac{\sum (x - \bar{x})(y - \bar{y})}{\sum (x - \bar{x})^2} \]
Here is the basic code I used to fit the model:
library(ggplot2) library(plotly) data(mtcars) # loads the car dataset model <- lm(mpg ~ wt, data = mtcars) # fits the regression model, with miles per gallon(mpg) against the weight model # prints the model results
## ## Call: ## lm(formula = mpg ~ wt, data = mtcars) ## ## Coefficients: ## (Intercept) wt ## 37.285 -5.344
ggplot(mtcars, aes(wt, mpg)) + geom_point() + labs(x = "Car Weight", y = "Miles Per Gallon")
ggplot(mtcars, aes(wt, mpg)) + geom_point() + geom_smooth(method = "lm", se = FALSE) + labs(x = "Car Weight",y = "Miles Per Gallon")
plot_ly(x = mtcars$wt, y = mtcars$mpg,
type = "scatter", mode = "markers") %>%
layout(xaxis = list(title = "Car Weight"),
yaxis = list(title = "Miles Per Gallon"))
From this example, we can see that heavier cars tend to have lower mpg. Linear regression helps us understand simple relationships between variables.