Brian Yarno
8/29/2017
This application predicts a hypothetical country's Savings Ratio based the values of three variables the user defines. Predictions are based on the Life Cycle Savings dataset in the R library.
Inputs
Outputs
lm(sr ~ pop15 + pop75 + ddpi, data = LifeCycleSavings)
Call:
lm(formula = sr ~ pop15 + pop75 + ddpi, data = LifeCycleSavings)
Coefficients:
(Intercept) pop15 pop75 ddpi
28.1247 -0.4518 -1.8354 0.4278
library(shiny)
library(clue)
library(mapdata)
# Define server logic required to create prediction
data("LifeCycleSavings")
set.seed(1234)
model <- with(LifeCycleSavings, lm(sr ~ pop15 + pop75 + ddpi, data = LifeCycleSavings))
clust <- kmeans(LifeCycleSavings[names(LifeCycleSavings) %in% c("pop15", "pop75", "ddpi", "sr")], 10)
shinyServer(function(input, output) {
prediction <- eventReactive(input$button,{
return(predict(model, newdata = data.frame(
pop15 = as.numeric(input$pop15),
pop75 = as.numeric(input$pop75),
ddpi = as.numeric(input$ddpi)
)
))
})
similar <- eventReactive(input$button, {
prediction <- predict(model, newdata = data.frame(
pop15 = as.numeric(input$pop15),
pop75 = as.numeric(input$pop75),
ddpi = as.numeric(input$ddpi)))
cluster_ids <- cl_predict(clust, newdata = data.frame(
pop15 = as.numeric(input$pop15),
pop75 = as.numeric(input$pop75),
ddpi = as.numeric(input$ddpi),
sr = prediction))
return(names(clust$cluster[clust$cluster == cluster_ids]))
})
country_table <- eventReactive(input$button, {
filter <- data.frame(LifeCycleSavings[row.names(LifeCycleSavings) %in% similar(), names(LifeCycleSavings) %in% c("pop15", "pop75", "ddpi", "sr")])
filter <- cbind(data.frame(country = row.names(filter)), filter)
names(filter) <- c("Country", "Savings Ratio", "% Pop 15 & Younger", "% Pop 75 & Older", "Disposable Income Growth Rate")
return(filter)
})
output$PredSr <- renderText(max(0, prediction()))
output$countries <- renderTable(country_table())
output$map <- renderPlot({
countries <- map('world', names = TRUE, plot = FALSE)
antarctica <- grep("^Antarctica", countries)
countries <- countries[-antarctica]
map('world', regions = countries)
map('world', regions = similar(), fill = TRUE, col = "red", add = TRUE)
})
})