Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(shiny)
- library(pROC)
- library(plyr)
- library(here)
- shinyUI(fluidPage(
- titlePanel(HTML('<div style="text-align: left;"><b>EVALUACION CREDITICIA</b></div>')),
- style = "background-color: #e6ffe6;",
- sidebarLayout(
- sidebarPanel(
- column(width = 6,
- numericInput("loanAmount",HTML('<span style="color: black;">MONTO DEL CREDITO</span>'), value = NULL),
- selectInput("paymentFrequency", "FRECUENCIA PAGO NOMINA", choices = c("Semanal", "Catorcenal", "Quincenal", "Mensual"), selected = "Semanal"),
- numericInput("Plazo", "PLAZO DE CREDITO", value = NULL),
- numericInput("Ingresos", "INGRESOS MENSUALES", value = NULL, min = 1),
- numericInput("Deducciones", "DEDUCCIONES MENSUALES", value = NULL, min = 1),
- ),
- column(width = 6,
- numericInput("Edad", "EDAD", value = NULL, min = 1),
- dateInput("Fechaingreso", "FECHA INGRESO LABORAL", value = Sys.Date()),
- selectInput("Empresapagadora", "EMPRESA", choices = c("Accionistas", "Denso", "La Tradicional", "MINCO", "Muebles Krill", "Roan", "Seguridad MG", "Otros"), selected = "Denso"),
- radioButtons("Comisiones", "COMISION CREDITO", choices = c("2%" = 1.02, "3%" = 1.03), selected = NULL),
- actionButton("calculate", "Calcular Probabilidad de Pago")
- )
- ),
- mainPanel(
- column(width = 6,
- textOutput("Predictiontext"),
- textOutput("optimalPlazoOutput")
- ),
- style = "background-color: #f0f8ff;"
- )
- )
- ))
- library(shiny)
- shinyServer(function(input, output) {
- observeEvent(input$calculate, {
- monto_prestamoo <- input$loanAmount
- ingresos <- input$Ingresos
- deducciones <- input$Deducciones
- edad <- input$Edad
- plazo_credito <- input$Plazo
- fecha_inicio <- input$Fechaingreso
- empresa_pagadora <- input$Empresapagadora
- frecuencia_pago <- input$paymentFrequency
- comisiones <- as.numeric(input$Comisiones)
- # Ajusta la tasa de interés según la frecuencia de pago
- if (frecuencia_pago == "Semanal") {
- plazo_credito_m <- plazo_credito / 360 * 7
- dias_entre_pagos <- 7
- } else if (frecuencia_pago == "Catorcenal") {
- plazo_credito_m <- plazo_credito / 360 * 14
- dias_entre_pagos <- 14
- } else if (frecuencia_pago == "Quincenal") {
- plazo_credito_m <- plazo_credito / 360 * 15
- dias_entre_pagos <- 15
- } else if (frecuencia_pago == "Mensual") {
- plazo_credito_m <- plazo_credito / 360 * 30
- dias_entre_pagos <- 30
- }
- #Obtener Antiguedad
- antiguedad <- as.numeric(Sys.Date()-fecha_inicio) / 360
- sueldo_neto <- ingresos - deducciones
- library(pROC)
- # Construct the path to the CSV file
- partial_filename <- paste0("FlexiCredit - Datos Modelo ")
- # Encontrar archivos que coincidan con la parte conocida del nombre
- matching_files <- list.files(path = dirname(partial_filename), pattern = basename(partial_filename))
- # Mostrar los archivos coincidentes encontrados
- print(matching_files)
- wd <- getwd()
- data_path <- file.path(wd, matching_files)
- data_flexi <- read.csv(file = data_path)
- data_flexi$Impago <- as.factor(data_flexi$Impago)
- data_flexi$Periodicidad <- as.factor(data_flexi$Periodicidad)
- data_flexi$Sexo <- as.factor(data_flexi$Sexo)
- data_flexi$Empresa_Pagadora <- as.factor(data_flexi$Empresa_Pagadora)
- order <- c('Semanal','Catorcenal','Quincenal','Mensual')
- data_flexi$Periodicidad <- factor(data_flexi$Periodicidad, levels = order)
- levels(data_flexi$Periodicidad)
- set.seed(450)
- index_train <- cbind(runif(1 : nrow(data_flexi), 0 , 1),c(1 : nrow(data_flexi)))
- index_train <- order(index_train[, 1])
- index_train <- index_train[1: (2/3 * nrow(data_flexi))]
- training_set <- data_flexi[index_train, ]
- test_set <- data_flexi[-index_train, ]
- log_model_full <- glm(Impago ~ PeriodicidadDos + Empresa_Pagadora + Edad + Antiguedad + Sueldo_Neto, family = "binomial",
- data = training_set)
- Nuevo_Contrato <- data.frame(Sueldo_Neto = sueldo_neto, Antiguedad = antiguedad, PeriodicidadDos = plazo_credito_m, Edad = edad, Empresa_Pagadora = empresa_pagadora)
- my_prediction <- predict(log_model_full, Nuevo_Contrato, type = "response")
- my_prediction <- unname(my_prediction)
- # Amortizacion data
- # Ajusta la tasa de interés según la frecuencia de pago
- if (empresa_pagadora == "Mondelez") {
- tasa_interes_anual <- .63
- } else if (empresa_pagadora == "Ladrillera") {
- tasa_interes_anual <- .63
- } else {
- tasa_interes_anual <- .60
- }
- if (frecuencia_pago == "Semanal") {
- tasa_interes_anual <- tasa_interes_anual / 360 * 7 * 1.16
- dias_entre_pagos <- 7
- } else if (frecuencia_pago == "Catorcenal") {
- tasa_interes_anual <- tasa_interes_anual / 360 * 14 * 1.16
- dias_entre_pagos <- 14
- } else if (frecuencia_pago == "Quincenal") {
- tasa_interes_anual <- tasa_interes_anual / 360 * 15 * 1.16
- dias_entre_pagos <- 15
- } else if (frecuencia_pago == "Mensual") {
- tasa_interes_anual <- tasa_interes_anual / 360 * 30 * 1.16
- dias_entre_pagos <- 30
- }
- tasa_interes <- tasa_interes_anual
- plazo_prestamo <- plazo_credito
- pago_mensual <- (monto_prestamoo * comisiones) * ((tasa_interes) / (1 - (1 + tasa_interes)^ (-plazo_prestamo)))
- #ajustes
- # Assuming plazo_values is a vector of values you want to test
- plazo_values <- seq(-20, 10, by = .01) # Adjust max_plazo_value as needed
- # Create an empty vector to store the optimal values
- optimal_plazo <- numeric(length(plazo_values))
- # Loop over each value of plazo_credito_m
- for (i in seq_along(plazo_values)) {
- Nuevo_Contrato$PeriodicidadDos <- plazo_values[i]
- prediccion_2 <- predict(log_model_full, Nuevo_Contrato, type = "response")
- prediccion_2 <- unname(prediccion_2)
- # Check if the prediction is greater than or equal to 0.35
- if (any(prediccion_2 >= 0.30)) {
- break # Exit the loop if the condition is met
- }
- # If the condition is not met, update optimal_plazo
- optimal_plazo <- plazo_values[i]
- }
- if (frecuencia_pago == "Semanal") {
- optimal_plazo <- optimal_plazo * 360 / 7
- } else if (frecuencia_pago == "Catorcenal") {
- optimal_plazo <- optimal_plazo * 360 / 14
- } else if (frecuencia_pago == "Quincenal") {
- optimal_plazo <- optimal_plazo * 360 / 15
- } else if (frecuencia_pago == "Mensual") {
- optimal_plazo <- optimal_plazo * 360 / 30
- }
- optimal_plazo
- Nuevo_Contrato
- #ajustes
- output$optimalPlazoOutput <- renderText({
- if(optimal_plazo > 0){ paste("PlazoMaximo", floor(optimal_plazo))}
- else { paste("Su plazo maximo sugiere valores negativos")
- }
- })
- # Display the prediction
- output$Predictiontext <- renderText({
- prediction_value <- round(my_prediction, 4) * 100
- if (prediction_value >= 30) {
- paste("Prediction:", prediction_value, "% Rechazado")
- } else if(ingresos - deducciones <= 0) {
- paste("Rechazado")
- } else if(edad < 18) {
- paste("Rechazado")
- } else if(monto_prestamoo > 75000 || monto_prestamoo < 2000){
- paste("Rechazado")
- } else {
- paste(prediction_value, "% Aceptado. Pago ", frecuencia_pago, ": $", round(pago_mensual, 2))
- }
- })
- })
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement