26.    R descriptive, predict, shiny

October 11, 2018
home

Contents

01. R Descriptive Statistics




         summary(cars)
         
Transmission Fuel.Economy .... Autumatic: 19 Min. : 4.000 Manual: 13 1st Qu. : 4.000 Median : 6.000 Mean : 6.188 3rd Qu. : 8:000 Max. : 8.000


        cor(
                x = cars$Cylinders,
                y = cars$Fuel.Economy)
            

02. Prediction

code part 1: prepare the data

data(iris) set.seed(42) indexes <- sample( x = 1:150, size = 100) indexes train <- iris[indexes, ] test <- iris[-indexes, ]

code part 2.1: Train a decision tree model

library(tree) # Train a decision tree model model <- tree( formula = Species ~ ., data = train) # Inspect the model summary(model) # Visualize the decision tree model plot(model) text(model)

code part 2.2:   Present the train model with a scatter plot

library(RColorBrewer) palette <- brewer.pal(3, "Set2") plot( x = iris$Petal.Length, y = iris$Petal.Width, pch = 19, col = palette[as.numeric(iris$Species)], main = "Iris Petal Length vs. Width", xlab = "Petal Length (cm)", ylab = "Petal Width (cm)") # Plot the decision boundaries partition.tree( tree = model, label = "Species", add = TRUE) #------------------------------------- # Set working directory setwd("~/documents/peter_r") # Save the tree model for others save(model, file = "Tree.RData") # Save the training data for others save(train, file = "Train.RData")

ai_1 case1_3



code part 3:   Predict

# Predict with the model predictions <- predict( object = model, newdata = test, type = "class") # Load the caret package library(caret) # Evaluate the prediction results confusionMatrix( data = predictions, reference = test$Species)

03. Package shiny for R HTML client and server app

web app demo 1

# install and load package shiny ui <- fluidPage("How are you doing!") server <- function(input, output) shinyApp( ui = ui, server = server)

web app demo 2

ui <- fluidPage( titlePanel("Input and Output"), sidebarLayout( sidebarPanel( sliderInput( inputId = "num", label = "select a Number", min = 0, max = 50, value = 25)), mainPanel( textOutput( outputId = "text")))) server <- function(input, output) { output$text <- renderText({ paste("You selected ", input$num )}) } shinyApp( ui = ui, server = server)

web app demo 3 - Prediction

predict

library(tree) setwd("~/documents/peter_r") load("Train.RData") load("Tree.RData") library(RColorBrewer) palette <- brewer.pal(3, "Set2") ui <- fluidPage( titlePanel("Iris Species Predictor"), sidebarLayout( sidebarPanel( sliderInput( inputId = "petal.length", label = "Petal Length (cm)", min = 1, max = 7, value = 4), sliderInput( inputId = "petal.width", label = "Petal Width (cm)", min = 0.0, max = 2.5, step = 0.5, value = 1.5)), mainPanel( textOutput( outputId = "text"), plotOutput( outputId = "plot")))) server <- function(input, output) { output$text = renderText({ # Create predictors predictors <- data.frame( Petal.Length = input$petal.length, Petal.Width = input$petal.width, Sepal.Length = 0, Sepal.Width = 0) # Make prediction prediction = predict( object = model, newdata = predictors, type = "class") # Create prediction text paste( "The predicted species is ", as.character(prediction)) }) output$plot = renderPlot({ # Create a scatterplot colored by species plot( x = iris$Petal.Length, y = iris$Petal.Width, pch = 19, col = palette[as.numeric(iris$Species)], main = "Iris Petal Length vs. Width", xlab = "Petal Length (cm)", ylab = "Petal Width (cm)") # Plot the decision boundaries partition.tree( model, label = "Species", add = TRUE) # Draw predictor on plot points( x = input$petal.length, y = input$petal.width, col = "red", pch = 4, cex = 2, lwd = 2) }) } shinyApp( ui = ui, server = server)