Automated Dashboard for Credit Modelling with Decision trees and Random forests in R

January 15, 2019

(This article was first published on R Programming – DataScience+, and kindly contributed to R-bloggers)


    1. Programming


    1. Data Visualisation
    2. Flexdashboard
    3. R Programming
    4. RMarkdown

    In this article, you learn how to make Automated Dashboard for Credit Modelling with Decision trees and Random forests in R. First you need to install the `rmarkdown` package into your R library. Assuming that you installed the `rmarkdown`, next you create a new `rmarkdown` script in R.

    After this you type the following code in order to create a dashboard with rmarkdown and flexdashboard:

    title: "Automated Dashboard for Credit Modelling with Decision trees and Random forests in R"
    author: "Kristian Larsen"
        orientation: rows
        vertical_layout: scroll
    ```{r setup, include=FALSE}
    # Data management packages
    options(scipen = 9999)
    # Read dataset
    loans <- read.csv("")
    # Data management
    # Change the order/level of checking_balance variable
    loans$checking_balance <- factor(loans$checking_balance,
                                    levels = c(" 200 DM",
    summary(loans[loans$default == "yes", "checking_balance"])
    # Change the order/level of saving_balance variable
    loans$savings_balance <- factor(loans$savings_balance,
                                    levels = c(" 1000 DM",
    summary(loans[loans$default == "yes", "savings_balance"])
    # Change the order/level of credit_history variable
    loans$credit_history <- factor(loans$credit_history,
                                    levels = c("critical",
                                               "very good",
    summary(loans[loans$default == "yes", "credit_history"])
    # Change the order/level of other_credit variable
    loans$other_credit <- factor(loans$other_credit,
                                    levels = c("none",
    summary(loans[loans$default == "yes", "other_credit"])
    in_loans_train <- sample(nrow(loans), nrow(loans)*0.75)
    loans_train <- loans[in_loans_train, ]
    loans_test <- loans[-in_loans_train, ]
    Row {data-width=350}
    ### Chart A - Decision tree Model I
    loans_model_dt <- ctree(default ~ ., loans_train)
    ### Chart B - Decision tree Model I - simple
    plot(loans_model_dt, type = "simple")
    Row {data-width=650}
    ### Chart C - Decision tree Model Model I - formula
    Row {data-width=650}
    ### Chart D - Confusion Matrix for Decision tree Model Model I
    loans_pred_dt <- predict(loans_model_dt, loans_test)
    dt_conft <- table("prediction" = loans_pred_dt,
                       "actual" = loans_test$default
    accu_dt <- round((dt_conft[1]+dt_conft[4])/sum(dt_conft[1:4]),4)
    prec_dt <- round(dt_conft[4]/(dt_conft[2]+dt_conft[4]), 4)
    reca_dt <- round(dt_conft[4]/(dt_conft[4]+dt_conft[3]), 4)
    spec_dt <- round(dt_conft[1]/(dt_conft[1]+dt_conft[2]), 4)
    confusionMatrix(loans_pred_dt, loans_test$default, positive = "yes")
    Row {data-width=650}
    ### Chart E - Decision tree Model II 
    loans_model_dt2 <- ctree(default ~ ., loans_train, control = ctree_control(mincriterion = 0.7))
    Row {data-width=650}
    ### Chart F - Decision tree Model Model II - formula
    Row {data-width=650}
    ### Chart G - Confusion Matrix for Decision tree Model Model II
    loans_pred_dt2 <- predict(loans_model_dt2, loans_test)
    confusionMatrix(loans_pred_dt2, loans_test$default, positive = "yes")
    Row {data-width=650}
    ### Chart H - Random Forest Model
    ctrl <- trainControl(method = "repeatedcv", number = 10, repeats = 3, allowParallel = TRUE)
    loans_rf <- train(default ~ ., data = loans, method = "rf", trControl = ctrl)
    Row {data-width=650}
    ### Chart I - Random Forest Model - variable importance
    Row {data-width=350}
    ### Chart J - Random Forest Model - Final model plot I
    legend("topright", colnames(loans_rf$finalModel$err.rate),col = 1:6, cex = 0.8, fill = 1:6)
    ### Chart K - Random Forest Model - Final model plot II


    The result of the above coding are published with RPubs here.


    1. Using flexdashboard in R

    Related Post

    1. Automated Dashboard for Classification Neural Network in R
    2. How to Achieve Parallel Processing in Python Programming
    3. Recommender System for Christmas in Python
    4. Automated Dashboard visualizations with Time series visualizations in R
    5. Automated Dashboard visualizations with distribution in R

    To leave a comment for the author, please follow the link and comment on their blog: R Programming – DataScience+. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

    If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

    Comments are closed.

    Search R-bloggers


    Never miss an update!
    Subscribe to R-bloggers to receive
    e-mails with the latest R posts.
    (You will not see this message again.)

    Click here to close (This popup will not appear again)