我是相对新的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)
代码工作完全没有车库部分。我失去了与数据格式有关的事情,但我很努力地解决这个问题。
干杯 马克
答复Alex,你的意思是创建模型中的变量需要成为一个因子,或者SelectInput中的值需要成为一个因子? –
@MarkJones我认为你需要尝试将SelectInput转换成因子 – Alexvonrass