2017-03-07 45 views
0

我是相對新的R,甚至更新使用Shiny,而且我在將一個selectInput鏈接到Shiny內的randomForest模型時遇到了問題。鏈接一個SelectId到一個隨機森林模型閃亮

我已經創建了一個RandomForest模型,可以預測客戶的保險費用,並且它表現良好。我希望能夠將此模型設置爲Shiny,並允許用戶通過滑塊和下拉菜單更改風險信息,以便相應更新成本值。這與數字字段完美結合,但是一旦我添加了一個selectImput列表(一個停車值輸入列表=「garage」),我得到以下錯誤;

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> 

我以爲,在下拉列表中的值是不是在模型中,讓我走進隨機森林對象選擇的實際值並將其放置在代碼中。

> 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模型中Parking的數據類別是Factor。停車值與輸入=「車庫」相關聯。

請在下面看到我的代碼的副本。任何與此有關的幫助將非常感謝,因爲我非常接近這個工作。

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) 

代碼工作完全沒有車庫部分。我失去了與數據格式有關的事情,但我很努力地解決這個問題。

乾杯 馬克

回答

0

我真的不能肯定地說,因爲我們無法查看或訪問你的模型,但我有點相信你的性格變量應轉換爲因素隨機森林使用,希望這可以幫助

+0

答覆Alex,你的意思是創建模型中的變量需要成爲一個因子,或者SelectInput中的值需要成爲一個因子? –

+0

@MarkJones我認爲你需要嘗試將SelectInput轉換成因子 – Alexvonrass