我正在尝试使用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。我尝试通过输入简单代码
来使用randomforesttit_glm <- randomForest(Survived ~ ., tit)
但我一直收到错误消息。请有人帮帮我吗?
答案 0 :(得分:0)
tit_glm <- randomForest(Survived ~ ., tit)
会出错,因为这意味着您正在使用所有其他功能进行模型培训。然而,这是不可能的,因为有很多特征主要是NA,并且有一些特征几乎没有方差。
首先尝试删除大部分为NA的功能,然后删除近零方差功能。