Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(shiny) # shiny library
- # begining of ui component
- ui <- shinyUI(fluidPage(theme = "bootstrap.css",
- titlePanel("HDDF"),
- sidebarLayout(
- sidebarPanel(
- h3('Model selection panel'),
- # the actioButton called rpart which is the name of the variable you need to use in the server component
- actionButton('rpart', label = 'Decision Tree', class = "btn btn-success btn-lg"),
- actionButton('Neuralnet', label = 'Neuronal network', class = "btn btn-secondary btn-lg"),
- # the training sample split you allow the user to control on your model
- numericInput("ratio",
- h4("Training sample in %"),
- value = 50 / 100,
- min = 50 / 100,
- max = 90 / 100,
- step = 0.1
- ),
- numericInput(
- "size",
- h4("Size"),
- value = 5,
- min = 0,
- max = 10,
- step = 0.5
- ),
- numericInput(
- "decay",
- h4("Decay"),
- value = 0.1,
- min = 0,
- max = 0.9,
- step = 0.1
- )
- ),
- # this is how you create many "tabs" for the output from ML models
- mainPanel(tabsetPanel(
- tabPanel(
- h4(tags$label("Application form")),
- fluidRow(column(width=3,
- sliderInput(
- "Edad",
- h3("Age (years)"),
- min = 19,
- max = 72,
- value = 20
- )),
- column(width=3,
- radioButtons(
- "Estado_civil_sexo",
- h3("Marital Status/Sex"),
- choices = list(
- "Male divorced/living apart" = "1",
- "Male Single" = "2",
- "Male married/widowed" = "3",
- "Female single" = "4"
- )
- )),
- column(width = 3,
- radioButtons(
- "Tiempo_empleo",
- h3("Employed"),
- choices = list(
- "Unemployed" = "1",
- "<= 1 year" = "2",
- "1-4 years" = "3",
- "4-7 years" = "4",
- ">=7 years" = "5"
- )
- )),
- column(width = 3,
- radioButtons(
- "Trabajo",
- h3("Occupation"),
- choices = list(
- "Unemployed/Unskilled/no residence" = "1",
- "Unskilled resident" = "2",
- "Skilled worker" = "3",
- "self-employed" = "4"
- )
- )
- )),
- fluidRow(column(width = 3,
- radioButtons(
- "Trabajador_extranjero",
- h3("Foreign worker"),
- choices = list("Yes" = "1",
- "No" = "2")
- )
- ),
- column(width = 3,
- radioButtons(
- "Dependientes",
- h3("Number of persons to maintenance"),
- choices = list("0 to 2" = "2",
- "3 and more" = "1")
- )),
- column(width = 3,
- radioButtons(
- "Propiedad",
- h3("Most valuable available assets"),
- choices = list(
- "Ownership of house or land " = "4",
- "Life insurance" = "3",
- "Car/other" = "2",
- "Nor availabre/No assets" = "1"
- )
- )),
- column(width = 3,
- radioButtons(
- "Alojamiento",
- h3("Type of apartment"),
- choices = list(
- "Rented falt" = "2",
- "Owner-occupied flat" = "3",
- "Free apartment" = "1"
- )
- )
- )),
- fluidRow(column(width = 3,
- radioButtons(
- "Tiempo_residencia",
- h3("Living in current household for"),
- choices = list(
- "< 1 year" = "1",
- "1-4 years" = "2",
- "4-7 years" = "3",
- ">=7 years" = "4"
- )
- )),
- column(width = 3,
- radioButtons(
- "Tasa",
- h3("Instalment in % of available income"),
- choices = list(
- "<20" = "4",
- "20-25" = "3",
- "25-35" = "2",
- ">=35" = "1"
- )
- )),
- column(width = 3,
- radioButtons(
- "Telefono",
- h3("Telephone"),
- choices = list("Yes" = "2",
- "No" = "1")
- )),
- column(width = 3,
- radioButtons(
- "Estado_cuenta",
- h3("Balance of current account"),
- choices = list(
- "No balance or debit" = "2",
- "0- 200 DM" = "3",
- ">=200" = "4",
- "No running account" = "1"
- )
- )
- )),
- fluidRow(column(width = 3,
- sliderInput(
- "Duracion_credito",
- h3("Duration credit in months"),
- min = 4,
- max = 72,
- value = 5
- )
- ),
- column(width = 3,
- radioButtons(
- "Historial_crediticio",
- h3("Payment of previous credits"),
- choices = list(
- "No credits/all paid" = "2",
- "Paid back previous credits at bank" = "4",
- "No problems with current credits at bank" = "3",
- "Hesitant payment of previous credits" = "0",
- "problematic running account" = "1"
- )
- )
- ),
- column(width = 3,
- radioButtons(
- "N_creditos",
- h3("Number of previous credits at this bank"),
- choices = list(
- "One" = "1",
- "two or three" = "2",
- "four or five" = "3",
- "Six or more" = "4"
- )
- )),
- column(width = 3,
- radioButtons(
- "Proposito",
- h3("Purpose"),
- choices = list(
- "New car" = "1",
- "Used car" = "2",
- "items of furiture" = "3",
- "Radio/TV" = "4",
- "Household appliances" = "5",
- "Education" = "6",
- "Vacation" = "8",
- "Retraining" = "9",
- "Business" = "10",
- "Other" = "0"
- )
- )
- )),
- fluidRow(column(width = 3,
- sliderInput(
- "Monto_credito",
- h3("Amount of credit"),
- min = 338,
- max = 18424,
- value = 270
- )
- ),
- column(width = 3,
- radioButtons(
- "Cuenta_ahorros",
- h3("Value of savings or stocks"),
- choices = list(
- "<100 EUR" = "2",
- "100-499 EUR" = "3",
- "500-999 EUR" = "4",
- ">=1000 EUR" = "5",
- "not available" = "1"
- )
- )),
- column(width = 3,
- radioButtons(
- "Deudor",
- h3("Further debtors/Guarantors"),
- choices = list(
- "None" = "1",
- "Co applicant" = "2",
- "Guarantor" = "3"
- )
- )),
- column(width = 3,
- radioButtons(
- "Planes_pago",
- h3("Further running credits"),
- choices = list(
- "At other banks" = "1",
- "At department store or mail order house" = "2",
- "No futher running credits" = "3"
- )
- ))
- )
- ),
- tabPanel(h4(tags$label("Data")),
- tableOutput("head")
- ),
- tabPanel(
- h4(tags$label("Decision Tree")),
- tags$div(style="height:200px; padding-left:40%;",
- imageOutput("image1", height = 150)
- ),
- tags$h4(align="center",
- tableOutput("result_a")
- ),
- plotOutput("result_2")
- ),
- tabPanel(
- h4(tags$label("Neural network")),
- tags$div(style="height:200px; padding-left:40%;",
- imageOutput("image2", height = 150)
- ),
- tags$h4(align="center",
- tableOutput("result_B")
- ),
- plotOutput("result_4")
- )
- )
- ))
- )
- )
- # all the libraries you need for your machine learning models and plots
- library(rpart) # Popular decision tree algorithm
- library(rpart.plot) # Enhanced tree plots
- library(nnet)
- library(neuralnet)
- library(png) # For writePNG function
- load("D:/emely/Desktop/TFM/dataModelo.Rdata", envir = environment())
- # begining of your server component
- server <- function(input, output, session) {
- data.test <- function() {
- return(
- data.frame(
- "Estado_cuenta" = as.numeric(input$Estado_cuenta),
- "Duracion_credito" = input$Duracion_credito,
- "Historial_crediticio" = as.numeric(input$Historial_crediticio),
- "Proposito" = as.numeric(input$Proposito),
- "Monto_credito" = input$Monto_credito,
- "Cuenta_ahorros" = as.numeric(input$Cuenta_ahorros),
- "Tiempo_empleo" = as.numeric(input$Tiempo_empleo),
- "Tasa" = as.numeric(input$Tasa),
- "Estado_civil_sexo" = as.numeric(input$Estado_civil_sexo),
- "Deudor" = as.numeric(input$Deudor),
- "Tiempo_residencia" = as.numeric(input$Tiempo_residencia),
- "Propiedad" = as.numeric(input$Propiedad),
- "Edad" = input$Edad,
- "Planes_pago" = as.numeric(input$Planes_pago),
- "Alojamiento" = as.numeric(input$Alojamiento),
- "N_creditos" = as.numeric(input$N_creditos),
- "Trabajo" = as.numeric(input$Trabajo),
- "Dependientes" = as.numeric(input$Dependientes),
- "Telefono" = as.numeric(input$Telefono),
- "Trabajador_extranjero" = as.numeric(input$Trabajador_extranjero),
- row.names = "Value"
- )
- )
- }
- observe({
- # this is how you fetch the input variable=ratio from ui component
- r <- input$ratio
- size <- input$size
- decay <- input$decay
- # construct your train and test set for machine learning models
- ind <- sample(2, nrow(data), replace = TRUE, prob = c(r, 1 - r))
- trainset = data[ind == 1, ]
- testset = data.test()
- # decision tree action button
- observeEvent(input$rpart, {
- ml_rpart <- rpart(trainset$clase ~ ., method = 'class', data = trainset)
- model_pred <- predict(ml_rpart, testset, type = "class")
- output$result <- renderPrint(model_pred)
- output$result_a<-renderPrint({
- if (model_pred == "F") {
- show('INVALID TRANSACTION')
- }
- else if (model_pred == "NF") {
- show("VALID TRANSACTION")
- }
- })
- output$image1 <- renderImage({
- if (model_pred == "F") {
- return(list(
- src = "images/F.png",
- contentType = "image/png",
- alt = "F"
- ))
- } else if (model_pred == "NF") {
- return(list(
- src = "images/NF.png",
- filetype = "image/png",
- alt = "NF"
- ))
- }
- }, deleteFile = FALSE)
- output$result_2 <- renderPlot(rpart.plot(ml_rpart, cex = 0.8))
- })
- # neural network action button
- observeEvent(input$Neuralnet, {
- fit <- nnet(
- clase ~ .,
- size = size,
- decay = decay,
- trace = F,
- data = trainset
- )
- ann <-
- neuralnet(
- as.numeric(clase) ~ Estado_cuenta + Duracion_credito + Historial_crediticio +
- Proposito +
- Monto_credito + Cuenta_ahorros + Tiempo_empleo + Tasa + Estado_civil_sexo +
- Deudor + Tiempo_residencia + Propiedad + Edad + Planes_pago + Alojamiento +
- N_creditos + Trabajo + Dependientes + Telefono + Trabajador_extranjero ,
- data = trainset,
- hidden = 1,
- linear.output = FALSE,
- threshold = 0.01,
- rep = 1
- )
- pred <- predict(fit, testset, type = "class")
- output$result_3 <- renderPrint(pred)
- output$result_B<-renderPrint({
- if (pred == "F") {
- show('INVALID TRANSACTION')
- }
- else if (pred == "NF") {
- show("VALID TRANSACTION")
- }
- })
- output$image2 <- renderImage({
- if (pred == "F") {
- return(list(
- src = "images/F.png",
- contentType = "image/png",
- alt = "F"
- ))
- } else if (pred == "NF") {
- return(list(
- src = "images/NF.png",
- filetype = "image/png",
- alt = "NF"
- ))
- }
- }, deleteFile = FALSE)
- output$result_4 <-renderPlot(plot(ann, rep = "best", cex = 0.9))
- })
- #print dataframe's sample head
- output$head <- renderTable({
- t(data.test())}, hover = TRUE, striped = TRUE, bordered = TRUE,
- spacing = c("s"),
- align = "c", rownames = TRUE, colnames = TRUE
- )
- })
- }
- shinyApp(ui = ui, server = server) # you call shiny all like this
- # free server shinyapps.io
Add Comment
Please, Sign In to add comment