我正在创建一个Shiny应用程序,以使用随机森林模型预测患者的阳性流感病例。用户可以通过应用程序输入一定数量的输入变量,并且可以单击actionButton,然后查看出现正感的可能性。 GeneXpert
(二进制编号)是用于训练RF模型的目标变量,所有其他变量本质上都是数字。当我运行该应用程序时,出现错误。
sample.int(length(x),size,replace,prob)中的错误:无效的'size'参数
这是我的代码:
library(shiny)
library(randomForest, quietly=TRUE)
library(ggplot2)
ui <- fluidPage(
titlePanel("Random Forest Flu Prediction Model"),
sidebarLayout(
sidebarPanel(
numericInput("month",
label = "Enter the Month:",
min = 1, max = 12, value = 5, step = 1),
numericInput("DOI", label = "Enter the days of illness:",
min = 1, max = 7, value = 5, step = 1),
numericInput(inputId = "age", label = "Enter the age:",
min = 1, max = 120, value = 5, step = 1),
numericInput(inputId = "rhi", label = "cursympt_rhinorrhea:",
min = 0, max = 1, value = 1, step = 1),
numericInput(inputId = "wt", label = "Enter your weight:",
min = 1, max = 400, value = 5, step = 0.5),
numericInput(inputId = "ht",
label = "Enter your Height:",
min = 1, max = 400, value = 1, step = 0.5),
numericInput(inputId = "sore", label = "cursympt_sorethroat.facto:",
min = 0, max = 1, value = 1, step = 1),
numericInput(inputId = "shkilchi", label =
"cursympt_shakingchills.facto:",
min = 0, max = 1, value = 1, step = 1),
numericInput(inputId = "shtbt", label = "cursympt_shortnessbreath.facto:",
min = 0, max = 1, value = 1, step = 1),
numericInput(inputId = "appt", label = "cursympt_appetite.factor:",
min = 0, max = 1, value = 1, step = 1),
numericInput(inputId = "incsput", label = "cursympt_incrsputum.facto:",
min = 0, max = 1, value = 1, step = 1),
numericInput(inputId = "wheez", label = "cursympt_wheezing.facto:",
min = 0, max = 1, value = 1, step = 1),
numericInput(inputId = "stom", label = "cursympt_stomachpain.facto:",
min = 0, max = 1, value = 1, step = 1),
actionButton("Action", "Analyze")
),
mainPanel(
textOutput('dynamicText')
)
)
)
server <- function(input, output) {
dataset =read.csv(file="Flu_Shiny_Data.csv",header=TRUE)
nobs <- nrow(dataset)
sample <- train <- sample(nrow(dataset), 0.8*nobs)
validate <- NULL
test <- setdiff(setdiff(seq_len(nrow(dataset)), train), validate)
input_features <- c("age","heightin","weightlb",
"cursympt_days","cursympt_incrsputum.factor",
"cursympt_sorethroat.factor","cursympt_rhinorrhea.factor",
"cursympt_shortnessbreath.factor","cursympt_wheezing.factor",
"cursympt_shakingchills.factor","cursympt_appetite.factor",
"cursympt_stomachpain.factor","Month")
input_numbers <- c("age","heightin","weightlb","cursympt_days",
"cursympt_incrsputum.factor","cursympt_sorethroat.factor",
"cursympt_rhinorrhea.factor", "cursympt_shortnessbreath.factor",
"cursympt_wheezing.factor","cursympt_shakingchills.factor",
"cursympt_appetite.factor","cursympt_stomachpain.factor","Month")
target_feature <- "GeneXpert"
result.rf <- randomForest::randomForest(as.factor(GeneXpert) ~ .,
data=dataset[sample,c(input_features, target_feature)],
ntree=500,
mtry=3,
importance=TRUE,
na.action=randomForest::na.roughfix,
replace=FALSE)
values <- reactiveValues()
observe({
if(input$Action>0){
newLine<-isolate(c(input$month,input$DOI,input$age,input$rhi,input$wt,
input$ht,input$sore,input$shkilchi,input$shtbt,
input$appt,input$incsput,input$wheez,input$stom))
isolate(values$dataset <- unlist(newLine))
}
})
output$dynamicText <- renderText({
if (is.null(values$dataset)))
return()
pr <- predict(result.rf, newdata=na.omit(values$dataset))
pr <- as.numeric(pr)
({pr-1})
})
}
# Run the application
shinyApp(ui = ui, server = server)