Multinomial Logistic Regression"

knitr::opts_chunk$set(echo = FALSE)
library(nnet)
library(e1071)
library(caret)
library(datasets)
library(stats)

About the instructor

h6("", tags$img(src ="K.JPG", height= 400, width=400))

Why Multinomial logistic regression?

Key Ideas

  1. Dependent variable is nominal scale and has more than two categories/classes/outcomes
  2. Independent variables can be qualitative or quantitative

Typical Applications ( Use Cases)

  1. Distinguishing/Classifying the different variants of the product based on the features

    Eg. Understanding the Product Profile

  2. Predicting the customer choice of a particular product among different products in a product category

    Eg. Understanding the Customer Profile

Assumptions and Conceptual idea

h6("", tags$img(src ="IA.JPG", height= 200, width=400))

Assumptions and Conceptual idea

Product Classification Problem

h6("", tags$img(src ="IRIS.JPG", height= 400, width=600))

Exploring IRIS Dataset

renderUI({

    data("iris")
    dataset = data.frame(iris)
     set.seed(1)
     index = runif(nrow(dataset))
     dataset = dataset[order(index),]
     a =  split(names(dataset),sapply(dataset, function(x) paste(class(x), collapse=" ")))
     a = data.frame(a$numeric)
     selectInput("variablex","Select the variable",choices = a[,1] ,selected = unique(a[,1]) )

     })



     renderPlot({
    data("iris")
    dataset = data.frame(iris)
    set.seed(1)
    index = runif(nrow(dataset))
    dataset = dataset[order(index),]
    attach(dataset)
    boxplot(get(input$variablex) ~ Species, col = "red")})

     inputPanel(
downloadButton("downloadPlot", "Download the Plot")
)

output$downloadPlot <- downloadHandler(
    filename = function() {
      paste("irisplot", ".png", sep = "")
    },
    content = function(file) {
      png(file)

      boxplot(get(input$variablex) ~ Species, col = "red",ylab = input$variablex,xlab = "Species")
      dev.off()
    })

Training Model

inputPanel(
  textAreaInput("model","Enter the model",value =  "Species ~ Sepal.Length +  Sepal.Width + Petal.Length + Petal.Width"),
  sliderInput("train_num", label = "Enter the proportion of training dataset:",
                     min = 0.6, max = 1, value = 0.6, step = 0.01)
)
renderPrint({

      data("iris")
      dataset1 = data.frame(iris)
      set.seed(1)
      index = runif(nrow(dataset1))
       dataset1 = dataset1[order(index),]
       prop = input$train_num
       set.seed(1)
       dataframe =  dataset1
       train.rows = sample(row.names(dataframe),dim(dataframe)[1]*prop)
       dataframet = dataframe[train.rows,]
       valid.rows = setdiff(row.names(dataframe),train.rows)
       dataframev = dataframe[valid.rows,]
       mod = multinom(formula = input$model, data = dataframet,trace = FALSE)
      print(mod)
      # print(summary(mod))
     #  print(round(coefficients(mod),0))

})

Explaining Model for prediction

h6("", tags$img(src ="PW.JPG", height= 500, width=600))

Evaluation of the Model

renderPrint({   data("iris")
                dataset = data.frame(iris)
                set.seed(1)
                index = runif(nrow(dataset))
                 dataset = dataset[order(index),]
       prop = input$train_num
       set.seed(1)
       dataframe =  dataset
       train.rows = sample(row.names(dataframe),dim(dataframe)[1]*prop)
       dataframet = dataframe[train.rows,]
       valid.rows = setdiff(row.names(dataframe),train.rows)
       dataframev = dataframe[valid.rows,]
       mod = multinom(input$model, data = dataframet,trace = FALSE)
       prediction = predict(mod, newdata=dataframev)
       print(confusionMatrix(dataframev$Species,prediction))
})

Deployment

inputPanel(
 numericInput("SL", label = "Sepal Length:",5.1),
 numericInput("SW", label = "Sepal Width:", 3.5),
 numericInput("PL", label = "Petal Length:",1.4),
 numericInput("PW", label = "Petal Width:",0.2)
)
renderPrint({   data("iris")
                dataset = data.frame(iris)
                set.seed(1)
                index = runif(nrow(dataset))
                 dataset = dataset[order(index),]
       prop = input$train_num
       set.seed(1)
       dataframe =  dataset
       train.rows = sample(row.names(dataframe),dim(dataframe)[1]*prop)
       dataframet = dataframe[train.rows,]
       valid.rows = setdiff(row.names(dataframe),train.rows)
       dataframev = dataframe[valid.rows,]
       mod = multinom(input$model, data = dataframet,trace = FALSE)
       newdatar = data.frame(Sepal.Length = input$SL, Sepal.Width = input$SW, Petal.Length = input$PL, Petal.Width = input$PW)
       prediction = predict(mod, newdata= newdatar)
        cat(sprintf("\nThe predicted flower is %s ",prediction))
       predictionp = max(predict(mod, newdata = newdatar, type = "prob"))
       cat(sprintf("\nThe probability is %f ",predictionp))


})

Customer Choice Problem

h6("", tags$img(src ="MP.JPG", height= 400, width=600))

inputPanel(
downloadButton("downloadData", "Download the dataset")
)
datasetInput <- reactive({
    dataset2 = read.csv(system.file("extdata", "MC.csv", package = "MNLR"),header =  TRUE)
  })
output$downloadData <- downloadHandler(
    filename = function() {
      paste("ProductChoiceDataset", ".csv", sep = "")
    },
    content = function(file) {
      write.csv(datasetInput(), file, row.names = FALSE)
    }
  )

Exploring Customer Choice Dataset

renderUI({
   dataset2 = read.csv(system.file("extdata", "MC.csv", package = "MNLR"),header =  TRUE)
     dataset = dataset2
     a =  split(names(dataset),sapply(dataset, function(x) paste(class(x), collapse=" ")))
     a = data.frame(a$integer)
     selectInput("variablex1","Select the variable",choices = a )
     })



     renderPlot({
    dataset2 = read.csv(system.file("extdata", "MC.csv", package = "MNLR"),header =  TRUE)

    attach(dataset2)
    assocplot(table(get(input$variablex1),Product),col=c("green","red"))})

  inputPanel(
downloadButton("downloadPlot2", "Download the Plot")
)

output$downloadPlot2 <- downloadHandler(
    filename = function() {
      paste("MobileChoiceplot", ".png", sep = "")
    },
    content = function(file) {
      png(file)
     dataset2 = read.csv(system.file("extdata", "MC.csv", package = "MNLR"),header =  TRUE)

    attach(dataset2)
    assocplot(table(get(input$variablex1),Product),col=c("green","red"),xlab = input$variablex1)
      dev.off()
    })

Consumer Choice Prediction and Deployment

inputPanel(
  textAreaInput("model1","Enter the model",value =  "Product ~ ."),
 sliderInput("train_num1", label = "Enter the proportion of training dataset:",
                     min = 0.6, max = 1, value = 0.6, step = 0.01),
 sliderInput("is", label = "Enter Income Status :  1 for High and 0 for Low",
                     min = 0, max = 1, value = 1, step = 1),
 sliderInput("se", label = "Enter Selfie Enthusiast :  1 for Yes and 0 for No",
                     min = 0, max = 1, value = 1, step = 1)

)

renderPrint({   
       dataset2 = read.csv(system.file("extdata", "MC.csv", package = "MNLR"),header =  TRUE)

       prop = input$train_num1
       set.seed(1)
       dataframe =  dataset2
       train.rows = sample(row.names(dataframe),dim(dataframe)[1]*prop)
       dataframet = dataframe[train.rows,]
       valid.rows = setdiff(row.names(dataframe),train.rows)
       dataframev = dataframe[valid.rows,]
       mod = multinom(input$model1, data = dataframet,trace = FALSE)
       newdatar = data.frame(IncomeStatus =  input$is,SelfieEnthusiast = input$se)
       prediction = predict(mod, newdata= newdatar)
        cat(sprintf("\nThe predicted Mobile Phone Product is %s ",prediction))
       predictionp = max(predict(mod, newdata = newdatar, type = "prob"))
       cat(sprintf("\nThe probability is %f ",predictionp))


})

Best Practices of Multinomial Logistic Regression Usage in Industry

Summary

Try out for Any data set

inputPanel(

#,
fileInput("file", "Upload the *.csv file with headers"),
textAreaInput("model2", "Enter the model")

)

renderPrint({

     file1 = input$file
     if(is.null(file1)){return()}

     data =  read.table(file =  file1$datapath,sep =",",header = TRUE)
     if(is.null(data())){return()}

     print(head(data,1))




   })
renderPrint({

      file1 = input$file
     if(is.null(file1)){return()}

     data =  read.table(file =  file1$datapath,sep =",",header = TRUE)
     if(is.null(data())){return()}

     datai = data.frame(data)
       mod2 = multinom(formula = input$model2, data = datai,trace = FALSE)

      print(mod2)



   })

RScript

h6("", tags$img(src ="RS.JPG", height= 500, width=600))
inputPanel(
downloadButton("downloadCode", "Download the Rcode")
)
datasetInput2<- reactive({
    dataset3 = read.csv(system.file("extdata", "multinom.txt", package = "MNLR"))
  })
output$downloadCode <- downloadHandler(
    filename = function() {
      paste("RCode", ".R", sep = "")
    },
    content = function(file) {
      write.csv(datasetInput2(), file,row.names = FALSE,fileEncoding = "")
    })


Try the MNLR package in your browser

Any scripts or data that you put into this service are public.

MNLR documentation built on May 2, 2019, 2:13 p.m.