2025-06-09

Introduction

Simple Linear Regression is a method for modeling the relationship between a dependent variable \(Y\) and an independent variable \(X\).

It assumes a linear relationship: \[ Y = \beta_0 + \beta_1 X + \varepsilon \]

Data: Air Quality

We will use R’s built-in airquality dataset, which includes daily air quality measurements in New York.

##   Ozone Solar.R Wind Temp Month Day
## 1    41     190  7.4   67     5   1
## 2    36     118  8.0   72     5   2
## 3    12     149 12.6   74     5   3
## 4    18     313 11.5   62     5   4
## 5    NA      NA 14.3   56     5   5
## 6    28      NA 14.9   66     5   6

We’ll model Ozone as a function of Temperature.

Scatterplot with ggplot2

Linear Model Fit (ggplot2)

This line represents the predicted values from the model: \[ \hat{Y} = \hat{\beta}_0 + \hat{\beta}_1 X \]

Estimating Coefficients

The formulas for estimating \(\hat{\beta}_0\) and \(\hat{\beta}_1\) are: \[ \hat{\beta}_1 = \frac{\sum (x_i - \bar{x})(y_i - \bar{y})}{\sum (x_i - \bar{x})^2}, \quad \hat{\beta}_0 = \bar{y} - \hat{\beta}_1 \bar{x} \]

3D Plotly Plot

R Code Summary

mod <- lm(Ozone ~ Temp, data = air)
summary(mod)
## 
## Call:
## lm(formula = Ozone ~ Temp, data = air)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -40.922 -17.459  -0.874  10.444 118.078 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -147.6461    18.7553  -7.872 2.76e-12 ***
## Temp           2.4391     0.2393  10.192  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 23.92 on 109 degrees of freedom
## Multiple R-squared:  0.488,  Adjusted R-squared:  0.4833 
## F-statistic: 103.9 on 1 and 109 DF,  p-value: < 2.2e-16
plot(air$Temp, air$Ozone)
abline(mod, col="blue")

Conclusion

We modeled Ozone as a linear function of Temperature.

The model: \[ \hat{Ozone} = \hat{\beta}_0 + \hat{\beta}_1 \cdot Temp \] can be used for prediction and understanding how temperature affects air quality.