-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.R
523 lines (442 loc) · 21 KB
/
app.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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
# Libraries and Source Files
library(shiny)
library(tidyverse)
library(scales)
# Set options
options(shiny.sanitize.errors = TRUE)
options(scipen = 999)
# Source file for Windows
Sys.setenv(R_GSCMD = "C:\\Program Files\\gs\\gs9.20\\bin\\gswin64.exe")
source('urban_institute_themes/urban_theme_windows.R')
# Source file for Mac
#source('urban_institute_themes/urban_theme_mac.R')
# Load Data
option_text <- read_csv("text/option.csv",
col_types = cols(
option = col_character(),
text = col_character()
)
)
option_asset <- read_csv("text/asset.csv",
col_types = cols(
asset = col_character(),
text = col_character()
)
)
factor_order <- c("Baseline", "HRS", "PSID", "SCF", "SIPP", "Reduce fees",
"Rebalance every 5 years", "Low participation",
"High participation", "Less risk","More risk",
"No target-date funds", "No auto-enrollment",
"No cash outs", "All Roth-401(K) accounts #1",
"All Roth-401(K) accounts #2", "Repeat the 1970s",
"RothIRA2", "RothIRA2allpart", "RothIRA2b", "RothIRA2c",
"RothIRA2d", "RothIRA2e", "RothIRA2f", "RothIRA2g",
"RothIRA2nocashout", "RothIRA3", "RothIRA3b", "RothIRA3c",
"RothIRA3d", "RothIRA4", "RothIRA4b", "RothIRA4c",
"RothIRA4d","RothIRAHighLimits", "RothIRALimit2",
"RothIRALimit2b", "RothIRALimit2c", "RothIRALimit2d",
"RothIRALimit2e", "RothIRALimit3", "RothIRALimit3b",
"RothIRALimit4", "SAVEopt2", "SaveOpt2b", "SaveOpt2firm10",
"SaveOpt3", "SAVEopt3b", "SAVEopt4", "SAVEopt4b",
"Bipartisan Policy Center package")
financial <- read_csv("data/financial-assets.csv",
col_types = cols(
Age = col_integer(),
cohort = col_character(),
data_source = col_character(),
`10th percentile` = col_double(),
`20th percentile` = col_double(),
`30th percentile` = col_double(),
`40th percentile` = col_double(),
`5th percentile` = col_double(),
`50th percentile` = col_double(),
`60th percentile` = col_double(),
`70th percentile` = col_double(),
`80th percentile` = col_double(),
`90th percentile` = col_double(),
`95th percentile` = col_double(),
`98th percentile` = col_double(),
Mean = col_double()
)
) %>% mutate(data_source = factor(data_source, levels = factor_order))
home_equity <- read_csv("data/home-equity.csv",
col_types = cols(
Age = col_integer(),
cohort = col_character(),
data_source = col_character(),
`10th percentile` = col_double(),
`20th percentile` = col_double(),
`30th percentile` = col_double(),
`40th percentile` = col_double(),
`5th percentile` = col_double(),
`50th percentile` = col_double(),
`60th percentile` = col_double(),
`70th percentile` = col_double(),
`80th percentile` = col_double(),
`90th percentile` = col_double(),
`95th percentile` = col_double(),
`98th percentile` = col_double(),
Mean = col_double()
)
) %>% mutate(data_source = factor(data_source, levels = factor_order))
retirement_account <- read_csv("data/retirement-account-assets.csv", col_types =
cols(
Age = col_integer(),
cohort = col_character(),
data_source = col_character(),
`10th percentile` = col_integer(),
`20th percentile` = col_double(),
`30th percentile` = col_double(),
`40th percentile` = col_double(),
`5th percentile` = col_integer(),
`50th percentile` = col_double(),
`60th percentile` = col_double(),
`70th percentile` = col_double(),
`80th percentile` = col_double(),
`90th percentile` = col_double(),
`95th percentile` = col_double(),
`98th percentile` = col_double(),
Mean = col_double()
)
) %>% mutate(data_source = factor(data_source, levels = factor_order))
total <- read_csv("data/total-assets.csv",
col_types = cols(
Age = col_integer(),
cohort = col_character(),
data_source = col_character(),
`10th percentile` = col_double(),
`20th percentile` = col_double(),
`30th percentile` = col_double(),
`40th percentile` = col_double(),
`5th percentile` = col_double(),
`50th percentile` = col_double(),
`60th percentile` = col_double(),
`70th percentile` = col_double(),
`80th percentile` = col_double(),
`90th percentile` = col_double(),
`95th percentile` = col_double(),
`98th percentile` = col_double(),
Mean = col_double()
)
) %>% mutate(data_source = factor(data_source, levels = factor_order))
# Set colors so colors don't change as levels are added/drops
cols <- c("Baseline" = "#1696d2",
"HRS" = "#fdbf11",
"PSID" = "#000000",
"SCF" = "#ec008b",
"SIPP" = "#d2d2d2",
"Reduce fees" = "#55B748",
"Rebalance every 5 years" = "#55B748",
"Low participation" = "#55B748",
"High participation" = "#55B748",
"Less risk" = "#55B748",
"More risk" = "#55B748",
"No target-date funds" = "#55B748",
"No auto-enrollment" = "#55B748",
"No cash outs" = "#55B748",
"Repeat the 1970s" = "#55B748",
"RothIRA2" = "#55B748",
"RothIRA2allpart" = "#55B748",
"RothIRA2b" = "#55B748",
"RothIRA2c" = "#55B748",
"RothIRA2d" = "#55B748",
"RothIRA2e" = "#55B748",
"RothIRA2f" = "#55B748",
"RothIRA2g" = "#55B748",
"RothIRA2nocashout" = "#55B748",
"RothIRA3" = "#55B748",
"RothIRA3b" = "#55B748",
"RothIRA3c" = "#55B748",
"RothIRA3d" = "#55B748",
"RothIRA4" = "#55B748",
"RothIRA4b" = "#55B748",
"RothIRA4c" = "#55B748",
"RothIRA4d" = "#55B748",
"RothIRAHighLimits" = "#55B748",
"RothIRALimit2" = "#55B748",
"RothIRALimit2b" = "#55B748",
"RothIRALimit2c" = "#55B748",
"RothIRALimit2d" = "#55B748",
"RothIRALimit2e" = "#55B748",
"RothIRALimit3" = "#55B748",
"RothIRALimit3b" = "#55B748",
"RothIRALimit4" = "#55B748",
"SAVEopt2" = "#55B748",
"SaveOpt2b" = "#55B748",
"SaveOpt2firm10" = "#55B748",
"SaveOpt3" = "#55B748",
"SAVEopt3b" = "#55B748",
"SAVEopt4" = "#55B748",
"SAVEopt4b" = "#55B748",
"Bipartisan Policy Center package" = "#55B748")
##
## Shiny
##
latoCSS <- "http://fonts.googleapis.com/css?family=Lato:300,400,700,900,300italic,400italic,700italic,900italic"
ui <- fluidPage(
tags$head(tags$link(rel = "stylesheet", type = "text/css", href = latoCSS)),
tags$head(tags$link(rel = "stylesheet", type = "text/css", href = "bootstrap.css")),
tags$head(tags$base(target = "_blank")),
tags$head(tags$script(src = "pym.min.js")),
theme = "shiny.css",
fluidRow(
column(12,
p("Long-run projections are sensitive to small changes in data and
assumptions. Use this interactive tool to compare changes in
DYNASIM's assumptions for defined-contribution pensions with data
from the Health and Retirement Study, Panel Study of Income
Dynamics, Survey of Consumer Finances, and Survey of Income and
Program Participation. ")
)
),
fluidRow(
column(10,
style = "position:relative",
h4(textOutput("title")),
h5(textOutput("subtitlea")),
h5(textOutput("subtitleb")),
plotOutput("chart", width = "100%", height = "400px")
)
),
fluidRow(
column(6,
selectInput(inputId = "option",
label = "Option",
choices = c("Baseline" = "Baseline",
"Reduce fees" = "Reduce fees",
"Rebalance every 5 years" = "Rebalance every 5 years",
"Low participation" = "Low participation",
"High participation" = "High participation",
"Less risk" = "Less risk",
"More risk" = "More risk",
"No target-date funds" = "No target-date funds",
"No auto-enrollment" = "No auto-enrollment",
"No cash outs" = "No cash outs",
"Repeat the 1970s" = "Repeat the 1970s",
"Bipartisan Policy Center package" = "Bipartisan Policy Center package",
"RothIRA2" = "RothIRA2",
"RothIRA2allpart" = "RothIRA2allpart",
"RothIRA2b" = "RothIRA2b",
"RothIRA2c" = "RothIRA2c",
"RothIRA2d" = "RothIRA2d",
"RothIRA2e" = "RothIRA2e",
"RothIRA2f" = "RothIRA2f",
"RothIRA2g" = "RothIRA2g",
"RothIRA2nocashout" = "RothIRA2nocashout",
"RothIRA3" = "RothIRA3",
"RothIRA3b" = "RothIRA3b",
"RothIRA3c" = "RothIRA3c",
"RothIRA3d" = "RothIRA3d",
"RothIRA4" = "RothIRA4",
"RothIRA4b" = "RothIRA4b",
"RothIRA4c" = "RothIRA4c",
"RothIRA4d" = "RothIRA4d",
"RothIRAHighLimits" = "RothIRAHighLimits",
"RothIRALimit2" = "RothIRALimit2",
"RothIRALimit2b" = "RothIRALimit2b",
"RothIRALimit2c" = "RothIRALimit2c",
"RothIRALimit2d" = "RothIRALimit2d",
"RothIRALimit2e" = "RothIRALimit2e",
"RothIRALimit3" = "RothIRALimit3",
"RothIRALimit3b" = "RothIRALimit3b",
"RothIRALimit4" = "RothIRALimit4",
"SAVEopt2" = "SAVEopt2",
"SaveOpt2b" = "SaveOpt2b",
"SaveOpt2firm10" = "SaveOpt2firm10",
"SaveOpt3" = "SaveOpt3",
"SAVEopt3b" = "SAVEopt3b",
"SAVEopt4" = "SAVEopt4",
"SAVEopt4b" = "SAVEopt4b")
),
selectInput(inputId = "asset",
label = "Asset",
choices = c("Retirement account assets" = "Retirement account assets",
"Financial assets" = "Financial assets",
"Home equity" = "Home equity",
"Total assets" = "Total assets")
),
selectInput(inputId = "percentile",
label = "Percentile or Mean",
choices = c("Mean" = "Mean",
"5th percentile" = "`5th percentile`",
"10th percentile" = "`10th percentile`",
"20th percentile" = "`20th percentile`",
"30th percentile" = "`30th percentile`",
"40th percentile" = "`40th percentile`",
"50th percentile" = "`50th percentile`",
"60th percentile" = "`60th percentile`",
"70th percentile" = "`70th percentile`",
"80th percentile" = "`80th percentile`",
"90th percentile" = "`90th percentile`",
"95th percentile" = "`95th percentile`",
"98th percentile" = "`98th percentile`")
),
selectInput(inputId = "cohort",
label = "Cohort",
choices = c("All Cohorts" = "All",
"1926-1930" = "1926-1930",
"1931-1935" = "1931-1935",
"1936-1940" = "1936-1940",
"1941-1945" = "1941-1945",
"1946-1950" = "1946-1950",
"1951-1955" = "1951-1955",
"1956-1960" = "1956-1960",
"1961-1965" = "1961-1965",
"1966-1970" = "1966-1970",
"1971-1975" = "1971-1975")
)
),
column(6,
checkboxGroupInput(inputId = "data_source",
label = "Validation Data",
choices = c("Health and Retirement Study" = "HRS",
"Panel Study of Income Dynamics" = "PSID",
"Survey of Consumer Finances" = "SCF",
"Survey of Income and Program Participation" = "SIPP",
"Baseline" = "Baseline"),
selected = c("Baseline", "HRS", "PSID", "SCF", "SIPP"))
)
),
br(),
fluidRow(
column(12,
downloadButton('download_data', 'Download charted data')
)
),
br(),
fluidRow(
column(12,
# Explanation of Social Security Reform
htmlOutput("text_option")
)
),
fluidRow(
column(12,
# Explanation of asset
htmlOutput("text_asset")
)
),
fluidRow(
column(12,
HTML("<h4><a href='http://hrsonline.isr.umich.edu/'>Health and Retirement
Study</a></h4><p>A national longitudinal study of approximately
20,000 respondents age 50 and older that asks questions about
assets, health care, housing, and pensions. The study is conducted
every two years and began in 1992.</p>"),
HTML("<h4><a href='https://psidonline.isr.umich.edu/'>Panel Study of
Income Dynamics</a></h4><p>A national longitudinal study of more
than 18,000 individuals in 5,000 families that asks questions about
employment, income, wealth, expenditures, health, marriage,
childbearing, child development, philanthropy, and education.
Families were asked questions annually from 1968 to 1997 and every
other year after 1997.</p> "),
HTML("<h4><a href='https://www.federalreserve.gov/econres/scfindex.htm'>Survey of Consumer Finances</a></h4>
<p>A national cross-sectional study of approximately 6,500 families
that focuses on balance sheets, pensions, income, and demographic
characteristics. The survey is conducted every three years and began
in 1983.</p> "),
HTML("<h4><a href='https://www.census.gov/sipp/'>Survey of Income and
Program Participation</a></h4><p>A continuous series of national
panels of 14,000 to 52,000 households that focuses on the interaction
between tax, transfer, and other government and private policies.
The survey began in 1983 and includes monthly data.</p>")
)
),
br(),
fluidRow(
column(6,
h3("About the data"),
HTML("<p>The Urban Institute’s Dynamic Simulation of Income Model (DYNASIM) projects the size and characteristics (such as financial, health, and disability status)
of the US population for the next 75 years. Using the best and most recent data available, it helps sort out how profound social, economic, and demographic
shifts will likely affect older adults and their retirement as well as taxpayers, business, and government. The model can also show how outcomes would likely
evolve under changes to public policies, business practices, or individual behaviors.</p>"),
HTML("<p><a href='https://www.urban.org/node/65826'>Read the DYNASIM primer</a></p>"),
HTML("<p><a href='https://www.urban.org/research/publication/dynamic-simulation-income-model-dynasim-overview'>Review the DYNASIM documentation</a></p>"),
HTML("<p>Questions about DYNASIM? <a href='mailto:retirementpolicy@urban.org' target='_self'>Contact us</a>.</p>")
),
column(6,
h3("Project Credits"),
HTML("<p><i>This work was funded by the US Department of Labor’s Employee Benefits Security Administration.
We are grateful to them and to all our funders, who make it possible for Urban Institute to advance its mission.</i></p>
<p><i>The views expressed are those of the authors and should not be attributed to the Urban Institute, its trustees,
or its funders. Funders do not determine research findings or the insights and recommendations of our experts.
More information on our funding principles is available <a href='https://www.urban.org/support'>here</a>.
Read our terms of service <a href='https://www.urban.org/terms-service'>here</a></i>.</p>"),
h5(HTML("<div class='credit-labels'>RESEARCH")),
HTML("<div class='credit-names'><p><a href='https://www.urban.org/author/karen-e-smith'>Karen Smith</a></p></div>"),
h5(HTML("<div class='credit-labels'>DESIGN AND DEVELOPMENT")),
HTML("<div class='credit-names'><p><a href='https://www.urban.org/author/aaron-r-williams'>Aaron Williams</a>, <a href='https://www.urban.org/author/jerry-ta'>Jerry Ta</a>, and <a href='https://www.urban.org/author/benjamin-chartoff'>Ben Chartoff</a></p></div>"),
h5(HTML("<div class='credit-labels'>EDITING")),
HTML("<div class='credit-names'><p><a href='https://www.urban.org/author/michael-marazzi'>Michael Marazzi</a></p></div>"),
h5(HTML("<div class='credit-labels'>WRITING")),
HTML("<div class='credit-names'><p><a href = 'https://www.urban.org/author/karen-e-smith'>Karen Smith</a> and <a href='https://www.urban.org/author/aaron-r-williams'>Aaron Williams</a></p></div>"),
HTML("Copyright © <a href='https://www.urban.org/'>Urban Institute</a> September 2017. View this project on <a href='https://github.com/urbaninstitute/dynasim-shiny1.git'>GitHub</a>.</p>")
)
),
tags$script(src = "activatePym.js")
)
server <- function(input, output) {
options(shiny.sanitize.errors = FALSE)
output$title <- renderText({
paste(input$Percentile, input$asset, sep = " ")
})
output$subtitlea <- renderText({
paste(input$cohort, " Cohorts", sep = " ")
})
output$subtitleb <- renderText({
paste(input$asset, "(divided by average earnings)")
})
filter_df <- function(df) {
print(input$option)
df %>%
filter(cohort == input$cohort) %>%
filter(data_source %in% c("Baseline", "HRS", "PSID", "SCF", "SIPP", input$option)) %>%
select_("Age", "cohort", "data_source", value_subset = input$percentile) %>%
mutate(value_subset = if_else(data_source %in% c(input$data_source, input$option), as.numeric(value_subset), as.numeric(NA)))
}
data_subset <- reactive({
if (input$asset == "Total assets") {
filter_df(total)
} else if (input$asset == "Retirement account assets") {
filter_df(retirement_account)
} else if (input$asset == "Financial assets") {
filter_df(financial)
} else if (input$asset == "Home equity") {
filter_df(home_equity)
}
})
output$chart <- renderPlot({
data_subset() %>%
ggplot(aes(x = Age, y = value_subset, color = data_source)) +
geom_line(size = 1) +
scale_x_continuous(breaks = c(20, 30, 40, 50, 60, 70, 80, 90, 100)) +
labs(y = NULL,
caption = "DYNASIM3
Urban Institute") +
scale_color_manual(values = cols) +
theme(axis.line = element_blank())
})
output$download_data <- downloadHandler(
filename = function() { paste0(input$option, '.csv') },
content = function(file) {
data_subset() %>%
filter(!is.na(value_subset)) %>%
write_csv(file)
}
)
output$text_option <- renderText({
as.character(
option_text %>%
filter(option == input$option) %>%
select(text)
)
})
output$text_asset <- renderText({
as.character(
option_asset %>%
filter(asset == input$asset) %>%
select(text)
)
})
}
shinyApp(ui = ui, server = server)