-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpreliminary_analysis.Rmd
341 lines (274 loc) · 10.2 KB
/
preliminary_analysis.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
---
title: "Modeling Food and Housing Insecurity at UTEP - Phase 1"
subtitle: "Data Preprocessing and Exploration"
author:
- "George E. Quaye"
- "John Koomson"
- "Willliam O. Agyapong"
affiliation: "Department of Mathematical Sciences, University of Texas at El Paso"
date: \center University of Texas, El Paso (UTEP)\center
\center Department of Mathematical Sciences \center
output:
html_document:
fig_caption: yes
keep_tex: no
number_sections: yes
toc: yes
toc_depth: 4
bookdown::pdf_document2:
fig_caption: yes
keep_tex: yes
latex_engine: pdflatex
number_sections: yes
toc: yes
toc_depth: 4
header-includes:
- \usepackage{float}
- \usepackage{setspace}
- \doublespacing
- \usepackage{bm}
- \usepackage{amsmath}
- \usepackage{amssymb}
- \usepackage{amsfonts}
- \usepackage{amsthm}
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \fancyhf{}
- \rhead{George Quaye, Johnson Koomson, William Agyapong}
- \lhead{Analysis Plan - DS 6335}
- \cfoot{\thepage}
- \usepackage{algorithm}
- \usepackage[noend]{algpseudocode}
geometry: margin = 0.8in
fontsize: 10pt
bibliography: references.bib
link-citations: yes
linkcolor: blue
csl: apa-6th-edition-no-ampersand.csl
nocite: |
---
\end{center}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = F)
# Load required packages
library(dplyr)
library(stringr)
library(ggplot2)
library(knitr)
```
```{r data-reading}
file_path <- dirname(rstudioapi::getSourceEditorContext()$path)
FIHI <- read.csv(paste0(file_path, '/datcsv.csv'))
#=========================================================
# Helper functions
#========================================================
to_zero <- function(x)
{
return(ifelse(is.na(x), 0, x ))
}
# This function helps restore back any prior missingness. Care must be taken in
# order not to apply it to a variable where 0 has a different meaning.
to_na <- function(x)
{
return(ifelse(x == 0, NA, x ))
}
```
```{r data-preprocessing}
FIHI_new <- FIHI %>%
# change column names to lowercase
rename_with(tolower) %>%
# Removing unwanted variables
select(-externalid, -satellite, -email, -datetime, -carddata,
-starts_with(c("q5", "q8", "q18", "q21", "q29", "q24", "q36", "q37", "q38", "q39"))
) %>%
# Cleaning column names by removing anything after the first occurrence of a dot in the column name
rename_with(~gsub('\\..*', '', .x))
# col_names <- names(FIHI)
# str_view_all(col_names, "\\..*")
## Consolidating common variables split across levels:
## We first create dummy columns for the number of non-na values within each range for the targeted variable.
## Next, we filter out all rows where the count for each of the targeted variables is more than 1.
FIHI_new <- FIHI_new %>%
# Creating dummy columns for the number of non-na values within each range for the targeted variable
mutate(q6_n = rowSums(!is.na(select(., starts_with("q6")))),
q7_n = rowSums(!is.na(select(., starts_with("q7")))),
q11_n = rowSums(!is.na(select(., starts_with("q11")))),
q25_n = rowSums(!is.na(select(., starts_with("q25")))))
# Identifyig multiracial instances
FIHI_new_sub <- FIHI_new %>%
filter(if_all(q6_n:q25_n, ~ !(.x>1))) %>% # filter out where there are multiple responses for the same question
select(-(q6_n:q25_n)) %>%
mutate(across(starts_with(c("q6", "q7", "q11", "q25")), to_zero)) %>%
mutate(q6 = rowSums(across(starts_with("q6"))),
q7 = rowSums(across(starts_with("q7"))),
q11 = rowSums(across(starts_with("q11"))),
q25 = rowSums(across(starts_with("q25"))),
.after = starts_with("q4"),
.keep = "unused") %>%
relocate(q11, .after = starts_with("q10")) %>%
relocate(q25, .after = starts_with("q23")) %>%
mutate(across(starts_with(c("q6", "q7", "q11", "q25")), to_na),
across(!respondentid, as.factor) # convert all variables except the respondent id to a factor
)
# Rename the remaining variables
var_names <- c("respondent_id", "enrollment", "employment", "employment_type", "weekly_work_hrs", "ethnicity", "gender", "total_income", "academic_level", "college/school", "mode_transport", "transport_reliability", "living_alone", "have_dependents", "dependents", "household_head", "residence", "permanent_address", "HI", "know_homelessness_studt", "federal_aid", "FI_q26", "FI_q27", "FI_q28", "FI_q30", "FI_q31", "change_expenditures", "change_income", "change_fin_aid", "change_debt")
names(FIHI_new_sub) <- var_names
```
### A glimpse into the issue of multiple responses per individual where a single response was expected.
```{r}
## Issue of multiple race
multi_racial <- FIHI_new %>%
filter(q6_n>1) %>%
select(respondentid, starts_with("q6"))
# names(multi_racial) <- c("respondentid", paste0("q6_", 1:7),"q6_n")
head(multi_racial, 30) %>%
kable(caption = "Individuals identifying with more than one ethnic group")
# Those of more than 1 gender designations.
multi_gender <- FIHI_new %>%
filter((q7_n>1)) %>%
select(respondentid, starts_with("q7"))
# names(multi_gender) <- c("RespondentId", paste0("q6_", 1:6),"q7_n")
kable(multi_gender,caption = "Individuals identifying with more than one gender")
# Those who belong to more than 1 college/schools.
multi_colleges <- FIHI_new %>%
filter((q11_n>1)) %>%
select(respondentid, starts_with("q11"))
# names(multi_colleges) <- c("RespondentId", paste0("Q11_", 1:9),"q11_n")
head(multi_colleges, 30) %>%
kable(caption = "Individuals belonging to more than one college/school")
# Those who received more than one source of Federal Aid.
multi_aid <- FIHI_new %>%
filter((q25_n>1)) %>%
select(respondentid, starts_with("q25"))
# names(multi_aid) <- c("RespondentId", paste0("q25_", 1:7),"q25_n")
head(multi_aid, 30) %>%
kable(caption = "Those who received more than one source of Federal Aid")
```
## Missing Data Exploration
### Structure of the preprocessed data
```{r}
glimpse(FIHI_new_sub)
```
### Summary
```{r}
## Get summary of the data
summary(FIHI_new_sub)
```
### Visualizing missingness
```{r}
# install.packages("naniar")
# install.packages("UpSetR")
library(naniar)
library(UpSetR)
# explore the patterns
gg_miss_upset(FIHI_new_sub)
# explore missingness in variables with gg_miss_var
miss_var_summary(FIHI_new_sub) %>% kable()
gg_miss_var(FIHI_new_sub) + ylab("Number of missing values")
gg_miss_var(FIHI_new_sub, show_pct = T)
```
```{r eval=F}
# Permanent Address
plotdf <- FIHI_sub3 %>%
group_by(permanent_address) %>%
summarise(n=n()) %>%
mutate(pct = n/sum(n),
lbl = percent(pct))
p1 <- ggplot(plotdf, aes(permanent_address,n, fill=as.factor(permanent_address))) +
geom_bar(stat = "identity",position = "dodge") +
geom_text(aes(label = n), size=3, position = position_stack(vjust = 0.5)) +
scale_fill_brewer(palette="Set2") +
labs(x="Had permanent address in the past 12 months?", y="Number of Respondents", title="") +
theme_classic() +
theme(legend.position = "none")
# Spending night elsewhere
plotdf <- FIHI_sub3 %>%
filter(permanent_address=='No') %>%
group_by(spent_night_elsewhere) %>%
summarise(n=n()) %>%
mutate(pct = n/sum(n),
lbl = percent(pct))
p2 <- ggplot(plotdf, aes(spent_night_elsewhere,n, fill=as.factor(spent_night_elsewhere))) +
geom_bar(stat = "identity",position = "dodge") +
geom_text(aes(label = n), size=3, position = position_stack(vjust = 0.5)) +
scale_fill_brewer(palette="Set2") +
labs(x="How fequently did you spend the night elsewhere?", y="Number of Respondents", title="") +
theme_classic() +
theme(legend.position = "none")
# Display plots side by side
p1 + p2
```
## Distribution of Food Insecurity Responses
```{r eval=F}
# Q26: Food bought didn't last
plotdf <- FIHI_sub3 %>%
group_by(FI_q26) %>%
summarise(n=n()) %>%
mutate(pct = n/sum(n),
lbl = percent(pct))
p1 <- ggplot(plotdf, aes(FI_q26,n, fill=FI_q26)) +
geom_bar(stat = "identity",position = "dodge") +
geom_text(aes(label = n), size=3, position = position_stack(vjust = 0.5)) +
scale_fill_brewer(palette="Set2") +
labs(x="Food bought didn't last", y="Number of Respondents", title="") +
theme_classic() +
theme(legend.position = "none")
# Q27: Balanced diet
plotdf <- FIHI_sub3 %>%
group_by(FI_q27) %>%
summarise(n=n()) %>%
mutate(pct = n/sum(n),
lbl = percent(pct))
p2 <- ggplot(plotdf, aes(FI_q27,n, fill=FI_q27)) +
geom_bar(stat = "identity",position = "dodge") +
geom_text(aes(label = n), size=3, position = position_stack(vjust = 0.5)) +
scale_fill_brewer(palette="Set2") +
labs(x="Couldn't afford balanced meals", y="Number of Respondents", title="") +
theme_classic() +
theme(legend.position = "none")
# Q27: Balanced diet
plotdf <- FIHI_sub3 %>%
group_by(FI_q28) %>%
summarise(n=n()) %>%
mutate(pct = n/sum(n),
lbl = percent(pct))
p3 <- ggplot(plotdf, aes(FI_q28,n, fill=FI_q28)) +
geom_bar(stat = "identity",position = "dodge") +
geom_text(aes(label = n), size=3, position = position_stack(vjust = 0.5)) +
scale_fill_brewer(palette="Set2") +
labs(x="Ever cut the size of meals?", y="Number of Respondents", title="") +
theme_classic() +
theme(legend.position = "none")
# Q27: Balanced diet
plotdf <- FIHI_sub3 %>%
group_by(FI_q30) %>%
summarise(n=n()) %>%
mutate(pct = n/sum(n),
lbl = percent(pct))
p4 <- ggplot(plotdf, aes(FI_q30,n, fill=FI_q30)) +
geom_bar(stat = "identity",position = "dodge") +
geom_text(aes(label = n), size=3, position = position_stack(vjust = 0.5)) +
scale_fill_brewer(palette="Set2") +
labs(x="Ever ate less than you should?", y="Number of Respondents", title="") +
theme_classic() +
theme(legend.position = "none")
# Q27: Balanced diet
plotdf <- FIHI_sub3 %>%
group_by(FI_q31) %>%
summarise(n=n()) %>%
mutate(pct = n/sum(n),
lbl = percent(pct))
p5 <- ggplot(plotdf, aes(FI_q31,n, fill=FI_q31)) +
geom_bar(stat = "identity",position = "dodge") +
geom_text(aes(label = n), size=3, position = position_stack(vjust = 0.5)) +
scale_fill_brewer(palette="Set2") +
labs(x="Ever hungry but didn't eat?", y="Number of Respondents", title="") +
theme_classic() +
theme(legend.position = "none")
# Display plots side by side
(p1 + p2)
(p3 + p4)
```
# References{-}
<div id="refs"></div>