data(airquality) head(airquality)
## 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
2026-02-05
airqualitydata(airquality) head(airquality)
## 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
airquality;Ozone vs Temperature
Model: \(\text{Ozone} = \beta_0 + \beta_1\cdot \text{Temp} + \varepsilon;
\quad \varepsilon \sim N(0,\sigma^2)\)
Fitted: \(\text{Ozone} = \hat{\beta}_0 + \hat{\beta}_1 \cdot \text{Temp}\) \(\hat{\beta}_0 = \text{estimate of }\beta_0;
\quad \hat{\beta}_1 = \text{estimate of }\beta_1\)
library(plotly)
library(ggplot2)
df = airquality[complete.cases(airquality[, c("Ozone","Temp")]), ]
df = df[order(df$Temp), ]
mod = lm(Ozone ~ Temp, data=df)
x = df$Temp; y = df$Ozone
fig = plot_ly(x=x, y=y, type="scatter", mode="markers", name="data",
width=800, height=430) %>%
add_lines(x=x, y=fitted(mod), name="fitted") %>%
layout(
xaxis = list(title = "Temperature (°F)"),
yaxis = list(title = "Ozone (ppb)"),
margin = list(l=80, r=30, b=80, t=40)
)
fig
Objective (Least Squares): \(\text{SSE} = \sum_{i=1}^{n} \big(\text{Ozone}_i - \hat{\text{Ozone}}_i\big)^2;
\quad \hat{\text{Ozone}}_i = \hat{\beta}_0 + \hat{\beta}_1 \cdot \text{Temp}_i\)
Total Variability: \(\text{SST} = \sum_{i=1}^{n} \big(\text{Ozone}_i - \bar{\text{Ozone}}\big)^2\) \(\bar{\text{Ozone}} = \text{sample mean ozone level}\)
Goodness of Fit: \(R^2 = 1 - \dfrac{\text{SSE}}{\text{SST}};
\quad R^2 = \text{fraction of variability explained by temperature}\)
Where SSE is the Sum of Squared Errors and SST is the Total Sum of Squares.
The residuals are centered around zero with no strong pattern, suggesting the linear model is reasonable.
summary(mod)## ## Call: ## lm(formula = Ozone ~ Temp, data = df) ## ## Residuals: ## Min 1Q Median 3Q Max ## -40.729 -17.409 -0.587 11.306 118.271 ## ## Coefficients: ## Estimate Std. Error t value Pr(>|t|) ## (Intercept) -146.9955 18.2872 -8.038 9.37e-13 *** ## Temp 2.4287 0.2331 10.418 < 2e-16 *** ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ## ## Residual standard error: 23.71 on 114 degrees of freedom ## Multiple R-squared: 0.4877, Adjusted R-squared: 0.4832 ## F-statistic: 108.5 on 1 and 114 DF, p-value: < 2.2e-16