-
Notifications
You must be signed in to change notification settings - Fork 3
/
cleandata.Rmd
235 lines (177 loc) · 8.23 KB
/
cleandata.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
---
title: "Exploratory Analysis"
author: "Gwendolyn"
date: "5/16/2018"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
# Load packages
library(knitr)
library(tidyverse)
library(anonymizer)
library(lubridate)
library(forcats)
library(googlesheets)
```
```{r import data, echo=FALSE}
# Use Google Sheets to read in data
gs_ls("HACSLdata")
hacsl <- gs_title("HACSLdata")
client <- hacsl %>% gs_read(ws = 1)
vouch <- hacsl %>% gs_read(ws = 3)
lease <- hacsl %>% gs_read(ws = 4)
gs_ls("Voucher issue.lease up date review Jun 7 2018.xlsx")
dates <- gs_title("Voucher issue.lease up date review Jun 7 2018.xlsx")
dates <- dates %>% gs_read(ws = 1)
```
```{r clean and merge, echo=FALSE}
client <- client %>%
rename(ClientId = HOH_SSN)
mergeclientvouch <- full_join(vouch, client, by = c("ClientId" = "ClientId"))
mergelease <- full_join(mergeclientvouch, lease, by = "VoucherID")
childrenbyclientid <- mergelease %>%
filter(RelationshipHOH=="Other Youth Under 18") %>%
group_by(ClientId, RelationshipHOH) %>%
summarise(NumberKids = n()) %>%
#rename(NumberKids = n) %>%
select(ClientId, NumberKids)
adultsbyclientid <- mergelease %>% filter(RelationshipHOH=="Head" | RelationshipHOH=="Co-Head" | RelationshipHOH=="Live-In Aide" | RelationshipHOH=="Other Adult" | RelationshipHOH=="Spouse") %>%
group_by(ClientId, RelationshipHOH) %>%
summarise(n = n()) %>%
spread(RelationshipHOH, n) %>%
rename(cohead = "Co-Head", aide = "Live-In Aide", otheradult = "Other Adult") %>%
mutate(cohead = ifelse(is.na(cohead), 0, cohead),
aide =ifelse(is.na(aide), 0, aide),
otheradult = ifelse(is.na(otheradult), 0, otheradult),
Head = ifelse(is.na(Head), 0, Head),
Spouse = ifelse(is.na(Spouse), 0, Spouse)) %>%
mutate(TotalAdults = cohead + Head + aide + otheradult + Spouse)
mergeleasekids <- full_join(mergelease, childrenbyclientid, by = c("ClientId" = "ClientId"))
mergeleasekidsadults <- full_join(mergeleasekids, adultsbyclientid, by = "ClientId")
demographics <- mergeleasekidsadults %>%
mutate(NumberKids = ifelse(is.na(NumberKids), 0, NumberKids)) %>%
mutate(Kids = ifelse(NumberKids==0, FALSE, TRUE))
```
Clean date file and make demographics long.
```{r clean dates file}
# Function for adding date variables
add_date_vars <- function(my_data){
## Takes the date from data and adds time variables ##
today <- Sys.Date()
my_data$date <- as.Date(my_data$DateUpdated,format = "%m/%d/%Y")
my_data$year_week <- format(my_data$date, '%Y-%W')
my_data$year_month <- format(my_data$date, '%Y-%m')
my_data$day <- as.numeric(lubridate::yday(my_data$date))
my_data$week <- as.numeric(lubridate::week(my_data$date))
my_data$month <- (lubridate::month(my_data$date, label = TRUE))
my_data$year <- as.numeric(lubridate::year(my_data$date))
my_data$days_ago <- as.numeric(difftime(my_data$date, today, units = "days"))
my_data$wday <- as.numeric(lubridate::wday(my_data$date))
my_data$day_type <- ifelse(wday(my_data$date) == 1, "weekend",
ifelse(wday(my_data$date) == 7, "weekend",
"weekday"))
return(my_data)
}
# Clean the data ----------------------------------------------------------
dates <- dates %>%
transmute(name = NAME,
referral = mdy(`REFERRAL DATE FROM TRIAGE`),
packet = mdy(`DATE REFERRAL PACKET RECEIVED`),
application = mdy(`DATE APPLICATION WAS PROCESSED`),
voucher = mdy(`DATE VOUCHER ISSUED`),
leaseup = mdy(`DATE OF LEASE UP`))
dates <- dates %>%
mutate(intervalrefpack = interval(referral, packet)) %>%
#mutate(referraltopacketdur = as.duration(intervalrefpack)) %>%
mutate(referraltopacketdays = as.duration(intervalrefpack) / ddays(1)) %>%
#mutate(referraltopacket = as.period(referraltopacketdur))
mutate(intervalpackapp = interval(packet, application)) %>%
mutate(packettoappdays = as.duration(intervalpackapp) / ddays(1)) %>%
mutate(intervalappvouch = interval(application, voucher)) %>%
mutate(appvouchdays = as.duration(intervalappvouch) / ddays(1)) %>%
mutate(intervalvouchlease = interval(voucher, leaseup)) %>%
mutate(vouchleasedays = as.duration(intervalvouchlease) / ddays(1)) %>%
mutate(intervalrefvouch = interval(referral, voucher)) %>%
mutate(referraltovoucher = as.duration(intervalrefvouch) / ddays(1)) %>%
mutate(intervalrefleaseup = interval(referral, leaseup)) %>%
mutate(referraltoleaseup = as.duration(intervalrefleaseup) / ddays(1))
datesgathered <- dates %>%
select(name, referraltopacketdays, packettoappdays, appvouchdays, referral, vouchleasedays) %>%
rename('ReferralToPacket' = referraltopacketdays,
'PacketToApplication' = packettoappdays,
'ApplicationToVoucher' = appvouchdays,
'VoucherToLeaseUp' = vouchleasedays) %>%
gather(key = "point", value = "days", 'ReferralToPacket', 'PacketToApplication',
'ApplicationToVoucher', 'VoucherToLeaseUp') %>%
filter(!is.na(referral)) %>%
group_by(name) %>%
mutate(in_process = any(is.na(days))) %>%
ungroup() %>%
mutate(point = as.factor(point)) %>%
mutate(point = ordered(point, levels = c("ReferralToPacket", "PacketToApplication",
"ApplicationToVoucher", "VoucherToLeaseUp")))
demographics <- demographics %>%
filter(RelationshipHOH=="Head") %>%
mutate(client_type = ifelse(TotalAdults>=1 & Kids==FALSE, "HH Adults Only",
ifelse(Kids==TRUE, "HH W Children", NA))) %>%
add_date_vars() %>%
mutate(IncomeLevel = fct_relevel(IncomeLevel, c("Extremely Low", "Very Low", "Low"))) %>%
mutate(BedroomSize.y = case_when(BedroomSize.y == "0" ~ "Studio", TRUE ~ as.character(BedroomSize.y))) %>%
mutate(BedroomSize.y = fct_relevel(BedroomSize.y, "Studio"))
dlong <- dates %>%
select_if(funs(!is.interval(.))) %>%
select(-contains("days")) %>%
mutate(referralx = referral) %>%
gather(key = 'point', value = 'date', -name, -referralx) %>%
rename(referral = referralx) %>%
arrange(referral, name) %>%
group_by(referral, name) %>%
mutate(days = date - lag(date)) %>%
#filter(point != 'leaseup') %>%
mutate(process_point = is.na(date) & !is.na(lag(date))) %>%
mutate(days = ifelse(process_point, today() - lag(date), days)) %>%
filter(point != 'referral') %>%
mutate(in_process = ifelse(point=="voucher" & is.na(days), TRUE, FALSE)) %>%
mutate(pointname = case_when(point == 'packet' ~ 'ReferralToPacket',
point == 'application' ~ 'PacketToApplication',
point == 'voucher' ~ 'ApplicationToVoucher',
point == 'leaseup' ~ 'VoucherToLeaseUp',
TRUE ~ NA_character_)) %>%
ungroup() %>%
mutate(pointname = ordered(pointname, levels = c("ApplicationToVoucher", "PacketToApplication", "ReferralToPacket", "VoucherToLeaseUp"))) #%>%
# TODO remind them that they have bad data
# filter(days >= 0)
bestorder <- c("ReferralToPacket", "PacketToApplication", "ApplicationToVoucher", "VoucherToLeaseUp")
```
```{r anonymize data}
## Anonymize Data and add it to the repo
demographics <- demographics %>%
select(-StreetAddress, -OwnerID, -PayeeID, -LeaseID, -VoucherID, -ClientId, -SSN, -LastName, -FirstName, -DOB, -MiddleName)
namevector <- datesgathered %>%
select(name) %>%
mutate(name2 = name) %>%
distinct()
nameanonymized <- namevector %>%
# Now use an algo to anon
mutate(name2 = anonymize(name2, .algo = "sha256", .seed = 123))
demographicsanonymized <- datesgathered %>%
left_join(nameanonymized, by = "name") %>%
rename(namedrop = name,
name = name2) %>%
select(-namedrop)
dlonganonymous <- dlong %>%
left_join(nameanonymized, by = "name") %>%
rename(namedrop = name,
name = name2) %>%
select(-namedrop)
datesanonymous <- dates %>%
select_if(funs(!is.interval(.))) %>%
left_join(nameanonymized, by = "name") %>%
rename(namedrop = name, name = name2) %>%
select(-namedrop)
write_rds(demographics, "./www/mergeddemographics.rds")
write_rds(demographicsanonymized, "./www/datesgathered.rds")
write_rds(dlonganonymous, "./www/dlong.rds")
write_rds(datesanonymous, "./www/dates.rds")
```