This repository has been archived by the owner on Aug 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
01-startup.Rmd
110 lines (87 loc) · 3.01 KB
/
01-startup.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
```{r}
###################################################################################################
##
## STARTUP CODE TO LOAD LIBRARIES AND DATA
##
## NO TEXT
##
###################################################################################################
```
```{r 01-su-clean-load,echo=FALSE,child="CleanLoad.Rmd"}
```
<!--
```{r 01-su-needed-functions}
############################################################################################################
##
## Given a date and a 3-digit zip, returns a triplet of:
## - the unemployment rate
## - the median income
## - personal income
## for that ZIP.
##
## The indicators are published by FIPS (Federal Information Processing Standard state code). A ZIP code
## can cover several FIPS. In such a case, the function looks up all the relevant FIPS code and returns
## a population weighted average of the indicators.
##
##
indicatorsFromZIPandDate <- function(zip3d, application_date) {
require(tidyverse)
require(lubridate)
emptyResult <-
tibble(
unemployment = 0,
medianIncome = 0,
personalIncome = 0
)
if (!is.numeric(zip3d))
return(emptyResult)
if (!is.Date(application_date))
return(emptyResult)
# Get all the FIPS with population
populatedzips <- zipfips %>%
# Look up a specific zip
filter(zip == zip3d) %>%
select(zip, population, county_fips) %>%
# Rename the FIPS column
rename(FIPS = county_fips) %>%
# Add the population of all the counties with the same FIPS
group_by(FIPS) %>%
mutate(population = sum(population)) %>%
# And keep unique zip/FIPS pairs with its population
distinct(FIPS, .keep_all = TRUE)
# Get macro stats
ratePerFIPS <- unemploymentRate %>%
# filter for FIPS
filter(FIPS %in% populatedzips$FIPS) %>%
# And all dates before the application date
filter(application_date >= Date) %>%
# Sort the dates in decreasing order
arrange(desc(Date)) %>%
# So that we only keep the first one
distinct(FIPS, .keep_all = TRUE)
medianPerFIPS <- medianIncome %>%
filter(FIPS %in% populatedzips$FIPS) %>%
filter(application_date >= Date) %>%
arrange(desc(Date)) %>%
distinct(FIPS, .keep_all = TRUE)
personalPerFIPS <- personalIncome %>%
filter(FIPS %in% populatedzips$FIPS) %>%
filter(application_date >= Date) %>%
arrange(desc(Date)) %>%
distinct(FIPS, .keep_all = TRUE)
# Put it all back together
summ <- populatedzips %>%
left_join(ratePerFIPS, by = "FIPS") %>%
left_join(medianPerFIPS, by = "FIPS") %>%
left_join(personalPerFIPS, by = "FIPS") %>%
mutate_at(c("unemploymentRate", "medianIncome", "personalIncome"),
as.double)
# And return
tibble(
unemployment = sum(summ$population * summ$unemploymentRate) / (sum(summ$population)),
medianIncome = sum(summ$population * summ$medianIncome) / (sum(summ$population)),
personalIncome = sum(summ$population * summ$personalIncome) / (sum(summ$population))
)
}
```
-->