-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprincipal_component_analysis.R
197 lines (187 loc) · 5.34 KB
/
principal_component_analysis.R
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
library(tidyverse)
# Read in the data
census_data <- lapply(
list.files("data"),
function(filename) {
read.csv(
file = paste0("data/", filename),
stringsAsFactors = FALSE
) %>%
select(
-dist_name,
-dsd_name,
-gnd_name,
-gnd_number,
-total
) %>%
mutate(
total = rowSums(.[-1])
) %>%
rename_at(
vars(-code_7),
list(~paste(substr(filename, 1, 3), ., sep = "_"))
)
}
)
# Merge the census datasets into a single large dataset
census_data <- Reduce(
function(x, y) {
inner_join(x, y, by = c("code_7"))
},
census_data
)
# Aggregate the census data by household and population data
## Remove the housing variables as they are redundant
## Remove most of the "other" fields as they are ambiguous
## Remove the age and gender variables as they are irrelevant
datasets <- list(
"combined" = census_data %>%
select(
code_7,
starts_with("coo"),
starts_with("edu"),
starts_with("emp"),
starts_with("flo"),
starts_with("lig"),
starts_with("roo"),
starts_with("str"),
starts_with("ten"),
starts_with("toi"),
starts_with("wal"),
starts_with("wat")
) %>%
mutate(
coo_total = coo_total - coo_other,
flo_total = flo_total - flo_other,
lig_total = lig_total - lig_other,
roo_total = roo_total - roo_other,
ten_total = ten_total - ten_other,
wal_total = wal_total - wal_other,
wat_total = wat_total - wat_other
) %>%
select(
-coo_other,
-flo_other,
-lig_other,
-roo_other,
-ten_other,
-wal_other,
-wat_other
) %>%
na.omit(),
"household" = census_data %>%
select(
code_7,
starts_with("coo"),
starts_with("flo"),
starts_with("lig"),
starts_with("roo"),
starts_with("str"),
starts_with("ten"),
starts_with("toi"),
starts_with("wal"),
starts_with("wat")
) %>%
mutate(
coo_total = coo_total - coo_other,
flo_total = flo_total - flo_other,
lig_total = lig_total - lig_other,
roo_total = roo_total - roo_other,
ten_total = ten_total - ten_other,
wal_total = wal_total - wal_other,
wat_total = wat_total - wat_other
) %>%
select(
-coo_other,
-flo_other,
-lig_other,
-roo_other,
-ten_other,
-wal_other,
-wat_other
) %>%
na.omit(),
"population" = census_data %>%
select(
code_7,
starts_with("edu"),
starts_with("emp")
) %>%
na.omit()
)
# Normalize the datasets with respect to category within each GND
normalized_datasets <- datasets
categories <- unique(substr(names(census_data), 1, 3))[-1]
for(i in names(normalized_datasets)) {
for(j in categories) {
normalized_datasets[[i]] <- normalized_datasets[[i]] %>%
mutate_at(
vars(starts_with(j)),
list(~. / .data[[paste(j, "total", sep = "_")]])
)
}
normalized_datasets[[i]] <- normalized_datasets[[i]] %>%
select(-ends_with("total"))
}
normalized_datasets[["population"]] <- normalized_datasets[["population"]] %>%
mutate_all(~replace(., is.na(.), 0))
rm(categories, i, j)
# Standardize the datasets with respect to each variable across all GNDs
standardized_datasets <- list()
for(i in names(normalized_datasets)) {
standardized_datasets[[i]] <- normalized_datasets[[i]] %>%
mutate_at(
vars(-code_7),
list(~scale(.))
)
}
rm(i)
# Conduct principle component analysis on the normalized datasets
pca_results <- list()
first_components <- list()
for(i in names(standardized_datasets)) {
## Run the analysis
pca_results[[i]] <- prcomp(
standardized_datasets[[i]] %>%
select(-code_7),
scale. = FALSE
)
## Plot the results
var <- pca_results[[i]]$sdev^2
biplot(pca_results[[i]], scale = 0)
plot(var/sum(var), type = "b")
plot(cumsum(var/sum(var)), type = "b")
## Extract the first principle component
first_components[[i]] <- pca_results[[i]]$rotation[, "PC1"]
first_components[[i]]["code_7"] <- 1
}
rm(i, var)
# Calculate the socioeconomic index for each dataset
indexes <- list()
for(i in names(standardized_datasets)) {
indexes[[i]] <- as.data.frame(
mapply(
'*',
standardized_datasets[[i]],
first_components[[i]][names(standardized_datasets[[i]])]
)
) %>%
mutate(
pc_1 = rowSums(
select(., -code_7)
)
) %>%
select(
code_7, pc_1
)
}
rm(i)
# Write the results
for(i in names(indexes)) {
write.csv(
x = indexes[[i]],
file = paste0("results/gnd_", i, "_pc1.csv"),
row.names = FALSE
)
}
rm(i)