-
Notifications
You must be signed in to change notification settings - Fork 1
/
Lab7-PCA.Rmd
377 lines (272 loc) · 9.69 KB
/
Lab7-PCA.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
---
title: "Lab 7 - Principle Component Analysis (PCA) "
author: "Factor Analysis ED 216B - Instructor: Karen Nylund-Gibson"
date: "`r format(Sys.time(), '%B %d, %Y')`"
output:
html_document: default
pdf_document: default
subtitle: '**Adam Garber**'
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, eval = FALSE, message = FALSE, warning = FALSE, tidy = TRUE)
```
## ____________________________________
### Outline lab 7 - three examples of Principle Component Analysis:
1. 10th grade student demographics & school safety (ELS, 2002 public-use data)
Next two PCA examples included are adapted from Allison Horst's ESM-206 course at UCSB (Horst, 2020):
2. California pollution burden example (from: OEHHA)
3. Combined pollution data & California county demographic data (from: CA census 2010)
## ____________________________________
DATA SOURCE: This lab exercise utilizes the NCES public-use dataset: Education Longitudinal Study of 2002 (Lauff & Ingels, 2014) [$\color{blue}{\text{See website: nces.ed.gov}}$](https://nces.ed.gov/surveys/els2002/avail_data.asp)
## ____________________________________
### load packages
```{r, eval=TRUE}
library(FactoMineR)
library(factoextra)
library(skimr)
library(naniar)
library(ggfortify)
library(janitor)
library(tidyverse)
library(here)
```
### read in ELS-2002 lab data:
```{r}
lab_data <- read_csv(here("data", "els_sub4.csv"))
```
### make all column names "lower_snake_case" style
```{r}
lab_tidy <- lab_data %>%
clean_names()
```
### Prepare data for PCA
```{r}
# remove variables that don't make sense in a PCA
lab_sub1 <- lab_tidy %>%
select(-stu_id, # these are random numbers
-sch_id,
-byrace, # nominal (non-ordered variable)
-byparace, # nominal (non-ordered variable)
-byparlng, # nominal (non-ordered variable)
-byfcomp, # nominal (non-ordered variable)
-bypared, -bymothed, -byfathed,
-bysctrl, -byurban, -byregion)
# select columns and rename variables to have descriptive names
lab_sub2 <- lab_sub1 %>%
select(1:9,
bys20a, bys20h, bys20j, bys20k, bys20m, bys20n,
bys21b, bys21d, bys22a, bys22b, bys22c, bys22d,
bys22e, bys22g, bys22h, bys24a, bys24b) %>%
rename("stu_exp" = "bystexp",
"par_asp" = "byparasp",
"mth_read" = "bytxcstd",
"mth_test" = "bytxmstd",
"rd_test" = "bytxrstd",
"freelnch" = "by10flp",
"stu_tch" = "bys20a",
"putdownt" = "bys20h",
"safe" = "bys20j",
"disrupt" = "bys20k",
"gangs" = "bys20m",
"rac_fght" = "bys20n",
"fair" = "bys21b",
"strict" = "bys21d",
"stolen" = "bys22a",
"drugs" = "bys22b",
"t_hurt" = "bys22c",
"p_fight" = "bys22d",
"hit" = "bys22e",
"damaged" = "bys22g",
"bullied" = "bys22h",
"late" = "bys24a",
"skipped" = "bys24b")
# write a CSV datafile of the new subset with renamed columns (will use for lab 8)
write_csv(lab_sub2, here("data", "lab7-8_els2002_data_subset.csv"))
```
### Investigate missingness {`naniar`} & make data summary with {`skimr`}
```{r}
# Plot number of missings by variable
gg_miss_var(lab_sub2)
# Look at summary of data using skimr::skim()
skim(lab_sub2)
pca1 <- lab_sub2 %>%
drop_na()
```
### run PCA with `prcomp()` (function does not permit NA values)
```{r, eval = FALSE}
pca_out1 <- prcomp(pca1, scale = TRUE)
plot(pca_out1)
#summary(pca_out1)
```
### plot PCA biplot
```{r}
jpeg(here("figures", "biplot_pca1.jpg"), res = 100) # to save the biplot
my_biplot <- autoplot(pca_out1,
colour = NA,
loadings.label = TRUE,
loadings.label.size = 3,
loadings.label.colour = "black",
loadings.label.repel = TRUE) +
theme_minimal()
my_biplot
dev.off()
```
```{r}
my_biplot
```
### alternative funtion to run & plot PCA biplot
```{r}
PCA(pca1, scale.unit = TRUE, ncp = 20, graph = TRUE)
```
## ____________________________________
### Examples and code for following section of lab are adapted from "Lab 2" of UCSB's ESM 206 course:
## taught by Allison Horst
### These course materials are openly available @ https://allisonhorst.github.io/
## ____________________________________
### A. Get the data
Data:
- California pollution burden (California Office of Environmental Health Hazard Assessment (OEHHA)’s CalEnviroScreen database, https://oehha.ca.gov/calenviroscreen/maps-data/download-data).
- See metadata, https://oehha.ca.gov/media/downloads/calenviroscreen/fact-sheet/ces30factsheetfinal.pdf.
- California county demographics (from: CA census 2010)
### Read it in:
```{r}
ca_pb <- read_csv(here("data", "ca_pollution_burden.csv"))
ca_dem <- read_csv(here("data", "ca_census_demographics_2010.csv"))
```
### B. Do some cleaning
1. For the pollution burden data:
- Clean up the column headers
- Exclude any column that is a calculated percentile (contains 'percentile', 'perc', or 'pctl')
```{r}
ca_pb_nopct <- ca_pb %>%
clean_names() %>%
select(-contains("pctl")) %>%
select(-contains("perc")) %>%
select(-latitude, - longitude)
```
2. For the demographic data:
- Clean up column names
```{r}
ca_dem_clean <- ca_dem %>%
clean_names()
```
### C. PCA for pollution burden indicator variables
First, starting with ca_pb_nopct:
**Note**: The pollution burden and population characteristic variables are aggregates (averages) of existing variables in the data frame, so we won't include those. That means we'll include columns:
- From `ozone:solid_waste`, and
- From `asthma:housing_burden`
First, just selecting those:
```{r}
ca_pb_subset <- ca_pb_nopct %>%
select(ozone:solid_waste, asthma:housing_burden)
```
To run pca we will use the `prcomp` function:
```{r, eval = FALSE}
pb_pca <- prcomp(ca_pb_subset, scale = TRUE) # hmmm an error
```
#### Look at the NA situation:
A little aside: the `naniar` package for exploring missingness!
See: https://naniar.njtierney.com/
Use `naniar::gg_miss_var()` to plot the number of missings by variable:
```{r}
# Plot number of missings by variable
gg_miss_var(ca_pb_subset)
```
Let's say our conclusion is that there are missings, but not many (compared to the actual scope of the data). We'll only keep our complete cases (census tracts without any missings).
Use `tidyr::drop_na()` with no variables specified to keep **ONLY** complete cases across all variables:
```{r}
ca_pb_nona <- ca_pb_subset %>%
drop_na()
# Now check for NAs:
summary(ca_pb_nona)
# Or use `skimr::skim()`!
skim(ca_pb_nona)
```
Cool. No NAs, NOW let's try PCA again:
```{r}
my_ca_pca <- prcomp(ca_pb_nona, scale = TRUE)
plot(my_ca_pca)
```
```{r}
jpeg(here("figures", "biplot_pca2.jpg"), res = 100) # to save the biplot
# Hmmm let's try something else (this requires ggfortify):
my_biplot <- autoplot(my_ca_pca,
colour = NA,
loadings.label = TRUE,
loadings.label.size = 3,
loadings.label.colour = "black",
loadings.label.repel = TRUE) +
theme_minimal()
my_biplot
dev.off()
```
4. Join data by census tract (inner join)
```{r}
ca_df <- ca_dem_clean %>%
inner_join(ca_pb_nopct, by = c("census_tract_number" = "census_tract"))
```
Look at the dataframe first & then use drop_na() to get complete cases only:
```{r}
ca_df_nona <- ca_df %>%
drop_na()
```
5. Make a new subset for PCA, that includes % white and elderly, and some interesting pollution burden & health indicators:
Like (you can choose a different set):
- white_percent
- elderly_65_percent
- pm2_5
- pesticides
- traffic
- asthma
- cardiovascular_disease
- poverty
Make our subset:
```{r}
my_sub <- ca_df_nona %>%
select(white_percent,
elderly_65_percent,
pm2_5,
pesticides,
traffic,
asthma,
cardiovascular_disease,
poverty)
```
Then run PCA:
```{r}
my_dem_pca <- prcomp(my_sub, scale = TRUE)
```
Check it out a bit:
```{r}
# Proportion of variance (& cumulative variance) explained by each PC
summary(my_dem_pca)
# Rotations (linear combinations for each PC):
my_dem_pca
```
Make a sweet biplot:
```{r}
jpeg(here("figures", "biplot_pca3.jpg"), res = 100) # to save the biplot
my_dem_biplot <- autoplot(my_dem_pca,
colour = NA,
loadings.label = TRUE,
loadings.label.size = 3,
loadings.label.colour = "black",
loadings.label.repel = TRUE) +
theme_minimal() +
scale_y_continuous(limits = c(-0.05, 0.05))
my_dem_biplot
dev.off()
```
- What are a few main things we can take out of this?
- What are the main correlations you notice?
- Are they in line with what you would expect, or is anything surprising?
## ____________________________________
### End of PCA section by Allison Horst
## ____________________________________
## References
Hallquist, M. N., & Wiley, J. F. (2018). MplusAutomation: An R Package for Facilitating Large-Scale Latent Variable Analyses in Mplus. Structural equation modeling: a multidisciplinary journal, 25(4), 621-638.
Horst, A. (2020). Course & Workshop Materials. GitHub Repositories, https://https://allisonhorst.github.io/
Muthén, L.K. and Muthén, B.O. (1998-2017). Mplus User’s Guide. Eighth Edition. Los Angeles, CA: Muthén & Muthén
R Core Team (2017). R: A language and environment for statistical computing. R Foundation for Statistical Computing, Vienna, Austria. URL http://www.R-project.org/
Wickham et al., (2019). Welcome to the tidyverse. Journal of Open Source Software, 4(43), 1686, https://doi.org/10.21105/joss.01686
![](figures/UCSB_Navy_mark.png){ width=75% }