我做了一个随机的森林预测模型,并希望我的用户输入自变量并预测因变量。我正在使用闪亮来实现这一点。
我收到以下错误,因为反应值存储为字符输入,因为我的预测模型是使用因子变量构建的。
警告:predict.randomForest中的错误:新数据中预测变量的类型 与训练数据不匹配。
任何人都可以帮助我如何将其转换为具有与我的训练数据集类似的因子变量的数据框。我使用了以下代码。
#ui.R
library(shiny)
library(shinydashboard)
library(randomForest)
ui = dashboardPage(
dashboardHeader(title = "title"),
dashboardSidebar(
selectInput("transaction_type", "select transaction type:", choices =
tranlist),
selectInput("entry_type", "select entry type", choices = entrylist),
selectInput("sic", "select sic", choices = siclist),
selectInput("markettype", "select market type", choices = marketlist),
numericInput("qty", "enter quantity","",min = 0),
numericInput("volume", "enter Total_amnt","",min = 0),
actionButton("goButton","Enter")
),
dashboardBody(
tableOutput("result"),
textOutput("predict")
)
)
#server.R
function(input, output,session){
pos <- eventReactive(input$goButton,{
if(input$goButton >0){
test[1:7] = c(input$transaction_type, input$entry_type, input$qty,
input$volume , input$sic, input$markettype, input$volume/input$qty)
} else{pos <- test}
test
})
output$result <- renderTable(pos())
output$predict = renderPrint(predict(settforest,newdata = pos(), type = "class"))
}