-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_economic_data.R
90 lines (76 loc) · 2.9 KB
/
create_economic_data.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
# **********************************************************
# * CATEGORY REVENUE & TAXATION
# * GROUP MODELING
# * AUTHOR LANCE HAYNIE <LHAYNIE@SCCITY.ORG>
# **********************************************************
# SMART
# Sustainable Management and Assessment for Responsible Taxation
# Copyright Santa Clara City
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.#
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
library(fredr)
library(dplyr)
source("db.R")
db <- db_connect()
fredr_set_key("ff45b73c550c4782e3f5a791c4a18e98")
cpi <- fredr(
series_id = "CPIAUCSL",
observation_start = as.Date("1990-01-01"),
observation_end = Sys.Date()
)
cpi <- cpi %>%
mutate(fiscal_year = lubridate::year(date)) %>%
group_by(fiscal_year) %>%
summarise(cpi = mean(value))
hpi <- fredr(
series_id = "UTSTHPI",
observation_start = as.Date("1990-01-01"),
observation_end = Sys.Date()
)
hpi <- hpi %>%
mutate(fiscal_year = lubridate::year(date)) %>%
group_by(fiscal_year) %>%
summarise(hpi = mean(value))
gdp <- fredr(
series_id = "GDP",
observation_start = as.Date("1990-01-01"),
observation_end = Sys.Date()
)
gdp <- gdp %>%
mutate(fiscal_year = lubridate::year(date)) %>%
group_by(fiscal_year) %>%
summarise(gdp = mean(value))
population <- fredr(
series_id = "UTPOP",
observation_start = as.Date("2000-01-01"),
observation_end = Sys.Date()
)
population$fiscal_year <- format(as.Date(population$date), "%Y")
population$population <- population$value * 1000
population <- population[, c("fiscal_year", "population")]
population$growth <- c(NA, diff(population$population))
population$population_growth <- (population$growth / lag(population$population)) * 100
unemployment <- fredr(
series_id = "UTURN",
observation_start = as.Date("1990-01-01"),
observation_end = Sys.Date()
)
unemployment <- unemployment %>%
mutate(fiscal_year = lubridate::year(date)) %>%
group_by(fiscal_year) %>%
summarise(unemployment = mean(value))
economic_data <- merge(cpi, hpi, by = "fiscal_year", all = TRUE)
economic_data <- merge(economic_data, gdp, by = "fiscal_year", all = TRUE)
economic_data <- merge(economic_data, population, by = "fiscal_year", all = TRUE)
economic_data <- merge(economic_data, unemployment, by = "fiscal_year", all = TRUE)
economic_data <- economic_data[order(economic_data$fiscal_year),]
save(economic_data, file = 'data/economic_data.RData')
dbWriteTable(db, name = "economic_data", value = economic_data, overwrite = TRUE, append = FALSE)
dbDisconnect(db)