Skip to content

Instantly share code, notes, and snippets.

@tomasgreif
Last active December 20, 2015 05:19
Show Gist options
  • Save tomasgreif/6077948 to your computer and use it in GitHub Desktop.
Save tomasgreif/6077948 to your computer and use it in GitHub Desktop.
library(shiny)
german_data <- read.table(file="http://archive.ics.uci.edu/ml/machine-learning-databases/statlog/german/german.data",
sep=" ", header=FALSE, stringsAsFactors=TRUE)
names(german_data) <- c('ca_status','duration','credit_history','purpose','credit_amount','savings',
'present_employment_since','installment_rate_income','status_sex','other_debtors',
'present_residence_since','property','age','other_installment','housing','existing_credits',
'job','liable_maintenance_people','telephone','foreign_worker','gb')
german_data$gb <- factor(german_data$gb, levels=c(2,1), labels=c("bad","good"))
german_data <- german_data[,c("duration","age","credit_amount","gb")]
shinyServer(function(input, output) {
# Show the first "n" observations
output$gd <- reactive({
gd <- german_data[,c(input$variable,"gb")]
})
output$view <- renderTable({
cuts <- sort(unique(c(input$g1,input$g2,input$g3,input$g4,input$g5,input$g6,input$g7,input$g8,input$g9)))
# Table by g/b
vartable <- table(cut(gd[,1],breaks=c(min(gd[,1]),cuts,max(gd[,1]))),gd[,2])
probtable <- prop.table(vartable,margin=2)
woedata <- data.frame(
good = vartable[,"good"],
bad = vartable[,"bad"],
pctgood = probtable[,"good"],
pctbad = probtable[,"bad"],
odds = probtable[,"good"]/probtable[,"bad"],
woe = log(probtable[,"good"]/probtable[,"bad"]),
miv = log(probtable[,"good"]/probtable[,"bad"]) * (probtable[,"good"] - probtable[,"bad"])
)
woedata$class <- factor(rownames(woedata), levels=rownames(woedata))
woedata
})
output$distPlot <- renderPlot({
barplot(woedata$woe, names=levels(woedata$class))
})
})
library(shiny)
# Define UI for application that plots random distributions
shinyUI(pageWithSidebar(
# Application title
headerPanel("Interactive Binning"),
# Sidebar with a slider input for number of observations
sidebarPanel(
selectInput("variable", "Variable:", list("Age" = "age", "Duration" = "duration", "Credit Amount" = "credit_amount")),
sliderInput("g1","g1",min = min(gd[,1]),max = max(gd[,1]), value = 10),
sliderInput("g2","g2",min = min(gd[,1]),max = max(gd[,1]), value = 10),
sliderInput("g3","g3",min = min(gd[,1]),max = max(gd[,1]), value = 10),
sliderInput("g4","g4",min = min(gd[,1]),max = max(gd[,1]), value = 10),
sliderInput("g5","g5",min = min(gd[,1]),max = max(gd[,1]), value = 10),
sliderInput("g6","g6",min = min(gd[,1]),max = max(gd[,1]), value = 10),
sliderInput("g7","g7",min = min(gd[,1]),max = max(gd[,1]), value = 10),
sliderInput("g8","g8",min = min(gd[,1]),max = max(gd[,1]), value = 10),
sliderInput("g9","g9",min = min(gd[,1]),max = max(gd[,1]), value = 10)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot"),
h4("Observations"),
tableOutput("view")
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment