繁体   English   中英

闪亮的R randomForest错误

[英]Shiny R randomForest Error

我正在尝试使用Shiny应用程序,发现下面的代码有效。

ui.R:

data(Titanic)
head(as.data.frame(Titanic),5)
library(shiny)
shinyUI(pageWithSidebar(
  headerPanel("Titanic Survival Calculator"),
  sidebarPanel(
    p("Select person attributes to calculate his/her chances of surviving of 
the titanic sinking."),
    selectInput("c", label =h3("Crew/Passenger:"), list("1st Class Passenger" = "1st","2nd Class Passenger" = "2nd", "3rd Class Passenger" = "3rd", "Crew Member" = "Crew")),
radioButtons("s", label = h3("Sex:"),
             choices = list("Male" = "Male", "Female" = "Female"), 
             selected = "Female"),
radioButtons("a", label = h3("Age:"),
             choices = list("Child" = "Child", "Adult" = "Adult"),
             selected = "Adult")),
  mainPanel(
h3("Survival Probability:"),
h4(textOutput('prob')),
p("Please note that this is estimated probability based on a logistic regression model."),
p("That means this value is slightly different than historical survival rate."))))

server.R:

library(shiny)
library(datasets)
data(Titanic)
tit <- as.data.frame(Titanic)
tit_glm <- glm(Survived ~ Class + Sex + Age, binomial, tit, tit$Freq)

pred_tit <- function(class, sex, age ){
  inputdata <- c(class, sex, age)
  pred_data <- as.data.frame(t(inputdata))
  colnames(pred_data) <- c("Class", "Sex", "Age")
  surv_prob <- predict(tit_glm,pred_data , type = "response" )
  return(surv_prob)
}

shinyServer(
  function(input, output) {
output$prob <- renderText({pred_tit(input$c,input$s, input$a)})
  })  

但是,我想使用Randomforest模型而不是GLM。 我试图通过输入简单的代码来使用randomforest

tit_glm <- randomForest(Survived ~ ., tit)

但是我一直在收到错误消息。 有人可以帮我吗?

tit_glm <- randomForest(Survived ~ ., tit)

会产生错误,因为这意味着您将所有其他功能都用于模型训练。 但是,这是不可能的,因为有很多特征大部分都是NA,而其中一些特征的方差接近零。

首先尝试删除大部分为NA的特征,然后删除接近零方差的特征。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM