# install.packages("OCSdata")
# OCSdata::raw_data("ocs-bp-air-pollution", outpath = getwd())
# once CS02 repos are created (after proposals are due 11/8), the file will also be at "data/pm25_data.csv" in your repo
pm <- read_csv("OCS_data/data/raw/pm25_data.csv")11-cs02-eda
CS02: Predicting Air Pollution (EDA)
Q&A
A: We’d want to see both, with a comparison with what has changed.
A: I’m curious too for more thoughts on the lecture-specific disucssion! From what y’all said in lab 05 (summarized):
Creative ways to use GenAI:
- Feedback on visualizations
- AI as grader or critical reviewer (Catching duplicated work across teammates; jargon-only proofread; grader primed with your past feedback)
- AI as a naive audience (Explain it back to me.)
- Debugging and code cleanup (AI-generated edge cases to stress-test code, like how cutoff values would be handled)
- Brainstorming analyses
- Challenging conclusions: Alternative explanations, limitations, confounders, counterarguments.
- Report structure and flow
- Finding sources or background
- explaining concepts like sensitivity and specificity (mock debate on sensitivity vs. specificity)
- team planning and dividing tasks
- and generating test data for code
Note: Telling AI to use Socratic mode
How to NOT use GenAI:
- Having AI write the report, analysis or conclusions
- Copying code they don’t understand
- Made-up data, results or citations
- Having AI do the whole project or a whole section
- Letting AI make the decisions
- Having AI build the visualizations from scratch
- Having AI make the general-audience piece
- Not disclosing AI use
- Not uploading the CS01 data to an AI tool
Course Announcements
Due Dates:
- Lab 06 due Fri (11:59 PM)
- 📄 Final Project Proposal due Sun (11:59 PM)
- 🔘 Lecture Participation survey “due” after class
Notes:
- My office hours moved from today to Fri at 11AM (this week only)
- Either work from lab06 data or et the data in RStudio for today (and
install.packages()"OCSdata","skimr","GGally", and"corrplot").
Question
With what accuracy can we predict US annual average air pollution concentrations?
Wrangling
Viewing the data
Take a look…
. . .
Given the dataset we’re working with, what wrangling should we consider doing here?
. . .
Last lecture, you looked for columns stored as numbers that aren’t really measurements:
pm |>
select(id, fips, zcta) |>
head(3)# A tibble: 3 × 3
id fips zcta
<dbl> <dbl> <dbl>
1 1003. 1003 36532
2 1027. 1027 36251
3 1033. 1033 35660
. . .
These are ID codes: averaging or correlating them doesn’t mean anything. So we store them as factors:
pm <- pm |>
mutate(across(c(id, fips, zcta), as.factor))EDA
Getting to “know” the data
skimr
skimr | A helpful way to get an overall sense of a dataset
# install.packages("skimr")
skimr::skim(pm)| Name | pm |
| Number of rows | 876 |
| Number of columns | 50 |
| _______________________ | |
| Column type frequency: | |
| character | 3 |
| factor | 3 |
| numeric | 44 |
| ________________________ | |
| Group variables | None |
Variable type: character
| skim_variable | n_missing | complete_rate | min | max | empty | n_unique | whitespace |
|---|---|---|---|---|---|---|---|
| state | 0 | 1 | 4 | 20 | 0 | 49 | 0 |
| county | 0 | 1 | 3 | 20 | 0 | 471 | 0 |
| city | 0 | 1 | 4 | 48 | 0 | 607 | 0 |
Variable type: factor
| skim_variable | n_missing | complete_rate | ordered | n_unique | top_counts |
|---|---|---|---|---|---|
| id | 0 | 1 | FALSE | 876 | 100: 1, 102: 1, 103: 1, 104: 1 |
| fips | 0 | 1 | FALSE | 569 | 170: 12, 603: 10, 261: 9, 107: 8 |
| zcta | 0 | 1 | FALSE | 842 | 475: 3, 110: 2, 160: 2, 290: 2 |
Variable type: numeric
| skim_variable | n_missing | complete_rate | mean | sd | p0 | p25 | p50 | p75 | p100 | hist |
|---|---|---|---|---|---|---|---|---|---|---|
| value | 0 | 1 | 10.81 | 2.580000e+00 | 3.02 | 9.27 | 11.15 | 12.37 | 2.316000e+01 | ▂▆▇▁▁ |
| lat | 0 | 1 | 38.48 | 4.620000e+00 | 25.47 | 35.03 | 39.30 | 41.66 | 4.840000e+01 | ▁▃▅▇▂ |
| lon | 0 | 1 | -91.74 | 1.496000e+01 | -124.18 | -99.16 | -87.47 | -80.69 | -6.804000e+01 | ▃▂▃▇▃ |
| CMAQ | 0 | 1 | 8.41 | 2.970000e+00 | 1.63 | 6.53 | 8.62 | 10.24 | 2.313000e+01 | ▃▇▃▁▁ |
| zcta_area | 0 | 1 | 183173481.91 | 5.425989e+08 | 15459.00 | 14204601.75 | 37653560.50 | 160041508.25 | 8.164821e+09 | ▇▁▁▁▁ |
| zcta_pop | 0 | 1 | 24227.58 | 1.777216e+04 | 0.00 | 9797.00 | 22014.00 | 35004.75 | 9.539700e+04 | ▇▇▃▁▁ |
| imp_a500 | 0 | 1 | 24.72 | 1.934000e+01 | 0.00 | 3.70 | 25.12 | 40.22 | 6.961000e+01 | ▇▅▆▃▂ |
| imp_a1000 | 0 | 1 | 24.26 | 1.802000e+01 | 0.00 | 5.32 | 24.53 | 38.59 | 6.750000e+01 | ▇▅▆▃▁ |
| imp_a5000 | 0 | 1 | 19.93 | 1.472000e+01 | 0.05 | 6.79 | 19.07 | 30.11 | 7.460000e+01 | ▇▆▃▁▁ |
| imp_a10000 | 0 | 1 | 15.82 | 1.381000e+01 | 0.09 | 4.54 | 12.36 | 24.17 | 7.209000e+01 | ▇▃▂▁▁ |
| imp_a15000 | 0 | 1 | 13.43 | 1.312000e+01 | 0.11 | 3.24 | 9.67 | 20.55 | 7.110000e+01 | ▇▃▁▁▁ |
| county_area | 0 | 1 | 3768701992.12 | 6.212830e+09 | 33703512.00 | 1116536297.50 | 1690826566.50 | 2878192209.00 | 5.194723e+10 | ▇▁▁▁▁ |
| county_pop | 0 | 1 | 687298.44 | 1.293489e+06 | 783.00 | 100948.00 | 280730.50 | 743159.00 | 9.818605e+06 | ▇▁▁▁▁ |
| log_dist_to_prisec | 0 | 1 | 6.19 | 1.410000e+00 | -1.46 | 5.43 | 6.36 | 7.15 | 1.045000e+01 | ▁▁▃▇▁ |
| log_pri_length_5000 | 0 | 1 | 9.82 | 1.080000e+00 | 8.52 | 8.52 | 10.05 | 10.73 | 1.205000e+01 | ▇▂▆▅▂ |
| log_pri_length_10000 | 0 | 1 | 10.92 | 1.130000e+00 | 9.21 | 9.80 | 11.17 | 11.83 | 1.302000e+01 | ▇▂▇▇▃ |
| log_pri_length_15000 | 0 | 1 | 11.50 | 1.150000e+00 | 9.62 | 10.87 | 11.72 | 12.40 | 1.359000e+01 | ▆▂▇▇▃ |
| log_pri_length_25000 | 0 | 1 | 12.24 | 1.100000e+00 | 10.13 | 11.69 | 12.46 | 13.05 | 1.436000e+01 | ▅▃▇▇▃ |
| log_prisec_length_500 | 0 | 1 | 6.99 | 9.500000e-01 | 6.21 | 6.21 | 6.21 | 7.82 | 9.400000e+00 | ▇▁▂▂▁ |
| log_prisec_length_1000 | 0 | 1 | 8.56 | 7.900000e-01 | 7.60 | 7.60 | 8.66 | 9.20 | 1.047000e+01 | ▇▅▆▃▁ |
| log_prisec_length_5000 | 0 | 1 | 11.28 | 7.800000e-01 | 8.52 | 10.91 | 11.42 | 11.83 | 1.278000e+01 | ▁▁▃▇▃ |
| log_prisec_length_10000 | 0 | 1 | 12.41 | 7.300000e-01 | 9.21 | 11.99 | 12.53 | 12.94 | 1.385000e+01 | ▁▁▃▇▅ |
| log_prisec_length_15000 | 0 | 1 | 13.03 | 7.200000e-01 | 9.62 | 12.59 | 13.13 | 13.57 | 1.441000e+01 | ▁▁▃▇▅ |
| log_prisec_length_25000 | 0 | 1 | 13.82 | 7.000000e-01 | 10.13 | 13.38 | 13.92 | 14.35 | 1.523000e+01 | ▁▁▃▇▆ |
| log_nei_2008_pm25_sum_10000 | 0 | 1 | 3.97 | 2.350000e+00 | 0.00 | 2.15 | 4.29 | 5.69 | 9.120000e+00 | ▆▅▇▆▂ |
| log_nei_2008_pm25_sum_15000 | 0 | 1 | 4.72 | 2.250000e+00 | 0.00 | 3.47 | 5.00 | 6.35 | 9.420000e+00 | ▃▃▇▇▂ |
| log_nei_2008_pm25_sum_25000 | 0 | 1 | 5.67 | 2.110000e+00 | 0.00 | 4.66 | 5.91 | 7.28 | 9.650000e+00 | ▂▂▇▇▃ |
| log_nei_2008_pm10_sum_10000 | 0 | 1 | 4.35 | 2.320000e+00 | 0.00 | 2.69 | 4.62 | 6.07 | 9.340000e+00 | ▅▅▇▇▂ |
| log_nei_2008_pm10_sum_15000 | 0 | 1 | 5.10 | 2.180000e+00 | 0.00 | 3.87 | 5.39 | 6.72 | 9.710000e+00 | ▂▃▇▇▂ |
| log_nei_2008_pm10_sum_25000 | 0 | 1 | 6.07 | 2.010000e+00 | 0.00 | 5.10 | 6.37 | 7.52 | 9.880000e+00 | ▁▂▆▇▃ |
| popdens_county | 0 | 1 | 551.76 | 1.711510e+03 | 0.26 | 40.77 | 156.67 | 510.81 | 2.682191e+04 | ▇▁▁▁▁ |
| popdens_zcta | 0 | 1 | 1279.66 | 2.757490e+03 | 0.00 | 101.15 | 610.35 | 1382.52 | 3.041884e+04 | ▇▁▁▁▁ |
| nohs | 0 | 1 | 6.99 | 7.210000e+00 | 0.00 | 2.70 | 5.10 | 8.80 | 1.000000e+02 | ▇▁▁▁▁ |
| somehs | 0 | 1 | 10.17 | 6.200000e+00 | 0.00 | 5.90 | 9.40 | 13.90 | 7.220000e+01 | ▇▂▁▁▁ |
| hs | 0 | 1 | 30.32 | 1.140000e+01 | 0.00 | 23.80 | 30.75 | 36.10 | 1.000000e+02 | ▂▇▂▁▁ |
| somecollege | 0 | 1 | 21.58 | 8.600000e+00 | 0.00 | 17.50 | 21.30 | 24.70 | 1.000000e+02 | ▆▇▁▁▁ |
| associate | 0 | 1 | 7.13 | 4.010000e+00 | 0.00 | 4.90 | 7.10 | 8.80 | 7.140000e+01 | ▇▁▁▁▁ |
| bachelor | 0 | 1 | 14.90 | 9.710000e+00 | 0.00 | 8.80 | 12.95 | 19.22 | 1.000000e+02 | ▇▂▁▁▁ |
| grad | 0 | 1 | 8.91 | 8.650000e+00 | 0.00 | 3.90 | 6.70 | 11.00 | 1.000000e+02 | ▇▁▁▁▁ |
| pov | 0 | 1 | 14.95 | 1.133000e+01 | 0.00 | 6.50 | 12.10 | 21.22 | 6.590000e+01 | ▇▅▂▁▁ |
| hs_orless | 0 | 1 | 47.48 | 1.675000e+01 | 0.00 | 37.92 | 48.65 | 59.10 | 1.000000e+02 | ▁▃▇▃▁ |
| urc2013 | 0 | 1 | 2.92 | 1.520000e+00 | 1.00 | 2.00 | 3.00 | 4.00 | 6.000000e+00 | ▇▅▃▂▁ |
| urc2006 | 0 | 1 | 2.97 | 1.520000e+00 | 1.00 | 2.00 | 3.00 | 4.00 | 6.000000e+00 | ▇▅▃▂▁ |
| aod | 0 | 1 | 43.70 | 1.956000e+01 | 5.00 | 31.66 | 40.17 | 49.67 | 1.430000e+02 | ▃▇▁▁▁ |
. . .
What’s something you’ve learned about the data from the skimr output?
. . .
Things to note:
- data are summarized by variable type
empty/n_missinggives you a sense of how much data are missing for each variablen_uniqueforstateindicates that we have data for 49 states- many different distributions for continuous data, but many show bimodal distribution
- large range of possible values for many variables (i.e. population)
. . .
- Consider variable type - need more factors?
- Check how monitors are spread across states and cities: which places are overrepresented? decide what to do
- log or other transformations necessary? decide during EDA
Why 49 “states”?
pm |>
distinct(state)# A tibble: 49 × 1
state
<chr>
1 Alabama
2 Arizona
3 Arkansas
4 California
5 Colorado
6 Connecticut
7 Delaware
8 District Of Columbia
9 Florida
10 Georgia
# ℹ 39 more rows
- DC is included
- Alaska and Hawaii are not
Where are the monitors?
Guess: how many monitors does San Diego County have?
. . .
pm |>
filter(county == "San Diego") |>
select(city, county, county_pop, popdens_county, value)# A tibble: 6 × 5
city county county_pop popdens_county value
<chr> <chr> <dbl> <dbl> <dbl>
1 Chula Vista San Diego 3095313 284. 12.3
2 El Cajon San Diego 3095313 284. 13.4
3 San Diego San Diego 3095313 284. 11.3
4 Escondido San Diego 3095313 284. 17.5
5 San Diego San Diego 3095313 284. 13.7
6 Boulevard San Diego 3095313 284. 3.47
. . .
pm |>
filter(city == "Baltimore") |>
select(city, county, county_pop, popdens_county, value)# A tibble: 5 × 5
city county county_pop popdens_county value
<chr> <chr> <dbl> <dbl> <dbl>
1 Baltimore Baltimore (City) 620961 2962. 12.2
2 Baltimore Baltimore (City) 620961 2962. 12.5
3 Baltimore Baltimore (City) 620961 2962. 12.8
4 Baltimore Baltimore (City) 620961 2962. 14.3
5 Baltimore Baltimore (City) 620961 2962. 13.3
. . .
San Diego County has 6 monitors for about 3.1 million people. Baltimore, a city that is its own (small) county, has 5 monitors for about 621,000 people. Monitor placement doesn’t simply follow population.
. . .
And not every monitor is in a city:
pm |>
count(city, sort = TRUE) |>
head(5)# A tibble: 5 × 2
city n
<chr> <int>
1 Not in a city 103
2 New York 9
3 Cleveland 6
4 Baltimore 5
5 Chicago 5
The outcome: value
pm |>
ggplot(aes(x = value)) +
geom_histogram(binwidth = 0.5, fill = "#2b6a99", color = "white") +
labs(x = "Annual average PM2.5 in 2008 (ug/m^3)", y = "Number of monitors") +
theme_minimal()
. . .
Which states have the highest averages?
pm |>
group_by(state) |>
summarize(n_monitors = n(), mean_pm25 = mean(value)) |>
arrange(desc(mean_pm25)) |>
head(5)# A tibble: 5 × 3
state n_monitors mean_pm25
<chr> <int> <dbl>
1 West Virginia 14 13.4
2 Ohio 44 12.9
3 Pennsylvania 32 12.8
4 Georgia 28 12.5
5 Indiana 36 12.4
How many monitors exceed health guidelines?
Guess: what share of these monitors would exceed today’s EPA annual standard (9 ug/m3)?
. . .
pm |>
summarize(
epa_2008 = sum(value > 15),
who_2005 = sum(value > 10),
epa_2024 = sum(value > 9),
who_2021 = sum(value > 5)
)# A tibble: 1 × 4
epa_2008 who_2005 epa_2024 who_2021
<int> <int> <int> <int>
1 28 583 686 864
. . .
Of 876 monitors, 28 (3%) were above the EPA standard in effect in 2008 (15 ug/m3). By today’s EPA standard (9 ug/m3), 686 (78%) would be. By WHO’s current guideline (5 ug/m3), 864 (99%).
A rough comparison: EPA’s official test uses a 3-year average, and these are 2008 values, not today’s air.
Feature Correlation
On Friday, you looked for variables that seemed redundant. Let’s check.
Why do we care if variables in our dataset are correlated?
- we don’t want to include redundant variables
- can add unnecessary noise to our algorithm causing a reduction in prediction accuracy
- can cause our algorithm to be slower
- can also make it difficult to interpret what variables are actually predictive
Which features track the outcome?
Guess: which feature is most correlated with value?
. . .
pm |>
select(where(is.numeric)) |>
cor() |>
as_tibble(rownames = "feature") |>
select(feature, r = value) |>
filter(feature != "value") |>
slice_max(abs(r), n = 10) |>
ggplot(aes(x = r, y = fct_reorder(feature, r))) +
geom_col(fill = "#2b6a99") +
labs(x = "Correlation with value (r)", y = NULL, title = "Top 10 features") +
theme_minimal()
. . .
CMAQ(the chemical model) is the strongest, followed by emissions (nei), roads (prisec), and the satellite measure (aod)- These are moderate correlations: no single feature predicts PM2.5 on its own
Correlations among features
PM_cor <- cor(pm |> select(where(is.numeric)))Taking a look at all of our numeric variables…
corrplot::corrplot(PM_cor, tl.cex = 0.5)
- deep blue | strongly, positively correlated
- deep red | strongly, negatively correlated
If we don’t care about direction, but only strength…and using hierarchical clustering:
corrplot::corrplot(abs(PM_cor), order = "hclust", tl.cex = 0.5, col.lim = c(0, 1))
Observations:
- development variables (
imp), road density (pri), and the emission (nei) variables all seem to be correlated with their group - several predictors are moderately correlated with
value(our outcome), but none strongly
Development (imp)
select(pm, contains("imp")) |>
GGally::ggpairs()
. . .
The same measure at different radii is strongly correlated with itself. The emissions (nei) and road (pri) groups show the same pattern (see the notes).
Emissions (nei)
select(pm, contains("nei")) |>
GGally::ggpairs()
Road Density (pri)
select(pm, contains("pri")) |>
GGally::ggcorr(hjust = .85, size = 3,
layout.exp = 2, label = TRUE,
# same colors as corrplot above: blue = positive, red = negative
low = "#67001F", mid = "#F7F7F7", high = "#053061")
Log-transforming right-skewed data
pm |>
mutate(log_popdens_county=log(popdens_county),
log_pop_county = log(county_pop)) |>
select(log_nei_2008_pm25_sum_10000, log_popdens_county,
log_pri_length_10000, imp_a10000, log_pop_county) |>
GGally::ggpairs()
Your Turn
Try to learn at least three things about the data that we haven’t yet discussed now on your own.
Put a green sticky on the front of your computer when you’re done. Put a pink if you want help/have a question.
