UC San Diego
COGS 137 - Fall 2026
2026-11-06
Due Dates:
# in your CS02 repo, the file is at "data/pm25_data.csv"
pm <- read_csv("OCS_data/data/raw/pm25_data.csv") |>
# these are ID codes, not measurements
mutate(across(c(id, fips, zcta), as.factor))Rows: 876 Columns: 50
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (3): state, county, city
dbl (47): id, value, fips, lat, lon, CMAQ, zcta, zcta_area, zcta_pop, imp_a5...
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
What is the relationship between the EPA’s CMAQ and PM2.5 values?
Reminder: Community Multiscale Air Quality (CMAQ) is the EPA’s air pollution model. The data do NOT use PM2.5 measures
\[ \hat{y} = b_0 + b_1~x_1 \]
… using formula syntax
# fit model
mod_cmaq_value <- lin_mod |>
fit(value ~ CMAQ, data = pm)
# display results
mod_cmaq_value |>
tidy()# A tibble: 2 × 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 7.40 0.232 31.9 1.81e-148
2 CMAQ 0.405 0.0260 15.6 1.83e- 48
\[\widehat{PM2.5}_{i} = 7.40 + 0.405 \times CMAQ_{i}\]
Q: I’m still a little confused about the difference between inference and prediction. If both can use a linear regression model, what is different about what we are trying to learn from the model?
A: The question is what differs first (prediction vs. quantifying a relationship.). Then, sometimes the model used differs. In the case of linear regression, the model is the same. But for prediction, the approach will differ (training vs. validation vs. test set - we’ll talk about this soon!). In other cases, models are built for prediction and only used to make predictions. We’ll get to this too.
Due Dates:
Notes:
\[\widehat{PM2.5}_{i} = 7.40 + 0.405 \times CMAQ_{i}\]
\[\widehat{PM2.5}_{i} = 7.40 + 0.405 \times CMAQ_{i}\]
❓ What would we expect the PM2.5 value to be in an area with a CMAQ of 2?
❓ What would we expect the PM2.5 value to be in an area with a CMAQ of 100? \[\widehat{PM2.5}_{i} = 7.40 + 0.405 \times 100\]

The strength of the fit of a linear model is most commonly evaluated using \(R^2\).
It tells us what percent of variability in the response variable is explained by the model.
The remainder of the variability is explained by variables not included in the model.
\(R^2\) is sometimes called the coefficient of determination.
# A tibble: 1 × 12
r.squared adj.r.squared sigma statistic p.value df logLik AIC BIC
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 0.217 0.216 2.29 243. 1.83e-48 1 -1966. 3939. 3953.
# ℹ 3 more variables: deviance <dbl>, df.residual <int>, nobs <int>
[1] 0.2172969
Roughly 21.7% of the variability in PM2.5 values can be explained by CMAQ.
…suggests that we can do better to explain the variance in PM2.5 values
\[ \hat{y} = b_0 + b_1~x_1 + b_2~x_2 + \cdots + b_k~x_k \]
What is the relationship between CMAQ & population density (predictors) and and PM2.5 values (outcome)?
value (PM2.5)CMAQ and Population Density hi/low❓ What is this accomplishing?

❓ Which does your intuition/knowledge of the data suggest is more appropriate?
Put a green sticky if you think main; pink if you think interaction.
# A tibble: 3 × 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 7.70 0.272 28.3 1.02e-125
2 CMAQ 0.389 0.0270 14.4 1.89e- 42
3 popdenslow -0.345 0.160 -2.15 3.17e- 2
\[\widehat{PM2.5} = 7.70 + 0.389 \times CMAQ - 0.345 \times popdens\]
❓ How do we interpret this model?
popdens\[\widehat{PM2.5} = 7.70 + 0.389 \times CMAQ - 0.345 \times 0\]
\(= 7.70 + 0.389 \times CMAQ\)
popdens\[\widehat{PM2.5} = 7.70 + 0.389 \times CMAQ - 0.345 \times 1\]
\(= 7.355 + 0.389 \times CMAQ\)

# A tibble: 3 × 2
term estimate
<chr> <dbl>
1 (Intercept) 7.70
2 CMAQ 0.389
3 popdenslow -0.345
CMAQ * popdensvalue (PM2.5)CMAQ, popdens, and their interaction# A tibble: 4 × 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 8.35 0.381 21.9 4.63e-85
2 CMAQ 0.319 0.0396 8.05 2.69e-15
3 popdenslow -1.45 0.485 -3.00 2.79e- 3
4 CMAQ:popdenslow 0.131 0.0540 2.42 1.56e- 2
\[\widehat{PM2.5} = 8.35 + 0.32 \times CMAQ - 1.45 \times popdens + 0.13 \times CMAQ * popdens\]

[1] 0.2172969
[1] 0.2214237
[1] 0.2266283
Adjusted R-squared adjusts for number of terms in the model
It appears that adding the interaction actually increased adjusted \(R^2\), so we should indeed use the model with the interactions.
Occam’s Razor states that among competing hypotheses that predict equally well, the one with the fewest assumptions should be selected.
Model selection follows this principle.
We only want to add another variable to the model if the addition of that variable brings something valuable in terms of predictive power to the model.
In other words, we prefer the simplest best model, i.e. parsimonious model.
For this demo, we’ll ignore interaction effects…and just model main effects:
First, keep only columns that make sense as predictors:
❓ What would happen if we left city (607 different values) in the model?
Remove zcta_area
zcta_area): 0.5732…Increased improved variance explained, so remove variable
… continue to remove one variable at a time until adjusted \(R^2\) stops improving. (You’ll do this yourself in Lab 07.)
❓ How does this maybe inform our ultimate goal (building a prediction model)?