0

我是 R 的相对论新手,甚至是使用 Shiny 的新手,我在将 selectInput 链接到 Shiny 中的 randomForest 模型时遇到了麻烦。

我创建了一个 RandomForest 模型来预测客户的保险成本,它看起来表现良好。我希望能够在 Shiny 中建立这个模型,并允许用户通过滑块和下拉菜单更改风险信息,以便相应地更新成本值。这与数字字段完美配合,但是一旦我添加了一个 selectImput 列表(停车值列表输入 =“车库”),我得到以下错误;

    Warning: Error in predict.randomForest: New factor levels not present in the training data
Stack trace (innermost first):
    85: predict.randomForest
    84: predict
    83: pred [#10]
    82: renderText [#2]
    81: func
    80: origRenderFunc
    79: output$guess
     4: <Anonymous>
     3: do.call
     2: print.shiny.appobj
     1: <Promise>

我假设下拉列表中的值不在模型中,所以我进入 RandomForest 对象以选择实际值并将它们放入代码中。

> rf$forest$xlevels$Parking
[1] "Driveway"                   "Locked garage"              "On the road at home"        "On the road away from home"
[5] "Other"                      "Residential car park"       "Work car park"  

同样的错误又回来了。RF 模型中停车的数据类是因子。停车位值链接到输入 =“车库”。

请在下面查看我的代码副本。对此的任何帮助将不胜感激,因为我非常接近让它发挥作用。

library(shiny)
library(randomForest)
library(datasets)


ui <- fluidPage(  titlePanel("Van Market Premium - alpha"),

                  checkboxInput(inputId = "comp", label = "Comprehensive"),
                  sliderInput(inputId = "age", label = "Age of Driver", value = 25, min = 17, max = 100),
                  sliderInput(inputId = "ncd", label = "No Claims Discount", value = 0, min = 0, max = 9),
                  numericInput(inputId = "cc", label = "CC", value = 1600, min = 250, max = 5000),
                  sliderInput(inputId = "value", label = "Current Van Value", value = 2000, min = 50, max = 20000, step = 250),
                  sliderInput(inputId = "aov", label = "Age of Van [years]", value = 5, min = 0, max = 50),
                  numericInput(inputId = "volxs", label = "Voluntary Excess", value = 0, min = 0, max = 1500),
                  sliderInput(inputId = "mileage", label = "Annual Mileage", value = 5000, min = 1000, max = 50000, step = 1000),
                  sliderInput(inputId = "length", label = "Ownership Length", value = 12, min = 0, max = 120, step = 6),
                  checkboxInput(inputId = "fuel", label = "Petrol?"),
                  checkboxInput(inputId = "auto", label = "Automatic?"),
                  selectInput(input = "garage", label = "Overnight Location", choices = as.factor(c("On the road at home",
                                                                                          "Driveway",
                                                                                          "Locked garage",
                                                                                          "Other",
                                                                                          "Residential car park",
                                                                                          "Work car park", 
                                                                                          "On the road away from home"))),
                  textOutput("guess")
                  )


RF <- get(load("C:/Users//Documents/R/RF3.RData"))

pred <- function(co, ag, nc, cc, val, aov, vol, mil, len, fuel, auto, garage) {

                                    inputdata <- c(co, ag, nc, cc, val, aov, vol, mil, len, fuel, auto, garage)

                                    pred_data <- as.data.frame(t(inputdata))

                                    colnames(pred_data) <- c("Comp" , "Age" , "NCD" , "CC" , "Value" , "AgeOfVehicle", "VoluntaryExcess"
                                                             ,"AnnualMileage", "LengthOwned", "petroldiesel", "auto", "Parking")

                                    prob_out <- predict(RF, pred_data)
                                    prob_out <- exp(prob_out)
                                    return(prob_out)

                                  }


server <- function(input, output) {
                                    output$guess <- renderText({pred(input$comp, input$age, input$ncd, input$cc, input$value, input$aov, 
                                                                     input$volxs, input$mileage, input$length, input$fuel, input$auto, input$garage
                                                                                              )})


                                  }

shinyApp(ui = ui, server = server)   

该代码在没有车库部分的情况下完美运行。我认为它与数据格式有关,但我真的很难解决这个问题。

干杯马克

4

1 回答 1

0

我不能肯定地说,因为我们看不到或访问您的模型,但我有点相信您的字符变量应该转换为在 randomForest 中使用的因子,希望这会有所帮助

于 2017-03-07T12:37:54.363 回答