2025-10-20

What is linear regression?

Linear regression is a statistical method that estimates the relationship between a dependent and independent variable.

It is useful to predict trends in simple sets of data

Mathematical model of linear regression

The mathematical formula for linear regression is \[ Y_i = \beta_0 + \beta_1 X_i + \varepsilon_i, \quad i = 1, 2, \dots, n \]

where:

  • \( Y_i \) is the response (dependent variable)

  • \( X_i \) is the predictor (independent variable)

  • \( \beta_0 \) is the intercept (value of \( Y \) when \( X = 0 \))

  • \( \beta_1 \) is the slope (change in \( Y \) for a one-unit change in \( X \))

  • \( \varepsilon_i \) are independent random errors ## Example with trees dataset we can show how linear regression works using the trees dataset that is built into RStudio

The data shows the girth and volume of various trees

data("trees")
summary(trees[, c("Girth","Volume")])
##      Girth           Volume     
##  Min.   : 8.30   Min.   :10.20  
##  1st Qu.:11.05   1st Qu.:19.40  
##  Median :12.90   Median :24.20  
##  Mean   :13.25   Mean   :30.17  
##  3rd Qu.:15.25   3rd Qu.:37.30  
##  Max.   :20.60   Max.   :77.00

Plot of girth and volume

Below is a plot of the girth and volume of the trees dataset

As you can see, there is a positive relationship between girth and volume

Perform linear regression using lm command

lm(Girth~Volume, data = trees)
## 
## Call:
## lm(formula = Girth ~ Volume, data = trees)
## 
## Coefficients:
## (Intercept)       Volume  
##      7.6779       0.1846

here, the intercept \( \beta_0 \) is 7.6779

and the slope \( \beta_1 \) is 0.1846

Plot of girth and volume with linear regression line included

## `geom_smooth()` using formula = 'y ~ x'

3D visualization of tree dataset

We can also create a plot that includes the height of the tree

Conclusion

Linear regression allows us to predict trends among data that closely related.