【问题标题】:Caret and shiny: cannot create prediction app driven by caret model插入符号和闪亮:无法创建由插入符号模型驱动的预测应用程序
【发布时间】:2018-12-04 11:39:17
【问题描述】:

我试图开发一个简单的应用程序,在给定特定年龄、等级、票价等的情况下,预测乘客在泰坦尼克号上幸存的概率。我希望这些变量是动态的,并希望计算预测的幸存概率使用底层插入符号模型。

运行此代码时,我收到以下错误消息:

警告:[.data.frame 中的错误:选择了未定义的列堆栈跟踪 (最里面的第一): 70:[.data.frame 69:[ 68:扫 67:预测.preProcess 66:预测 65:概率函数 64:预测训练 63:预测 62:预测 61:是.data.frame 60:数据.矩阵 59:观察者函数[#17] 4: 3:do.call 2:print.shiny.appobj 1:错误:[on_request_read] 连接被对等方重置

我的代码如下。任何想法是什么导致了这个错误?非常感谢。

require(shiny)
require(plyr)
require(dplyr)
require(ggplot2)
require(caret)
require(xgboost)

require(titanic)
df=na.omit(titanic_train)
y=data.matrix(select(df, Survived))
y[y==0]="N"
y[y==1]="Y"
x=data.matrix(select(df, Pclass, Age, SibSp, Parch, Fare))

tCtrl <- trainControl(method = "repeatedcv", number = 3, repeats=3, summaryFunction = twoClassSummary, verbose=TRUE, classProbs = TRUE)
fit_xgbTree= train(x, y, method = "xgbTree" , family= "binomial", trControl = tCtrl, metric = "ROC", preProc = c("center", "scale"))

ui = pageWithSidebar(
  headerPanel("Titanic"),
  sidebarPanel(
    radioButtons("Pclass", "Passenger Class", choices=c("1", "2", "3"),selected = "1", inline = TRUE,width = NULL),
    sliderInput("Age", "Passenger Age", min=0, max=80, value=30),
    radioButtons("SibSp", "SibSp", choices=c("0", "1", "2", "3", "4", "5")),
    radioButtons("Parch", "Parch", choices=c("0", "1", "2", "3", "4", "5", "6")),
    sliderInput("Fare", "Passenger Fare", min=0, max=520, value=35)
  ),
  mainPanel(
    dataTableOutput('testTable'),
    textOutput('outputBox')
  )
)

server=function(input, output){

  values <- reactiveValues()

  newEntry <- observe({ # use observe pattern

    x=as.data.frame(matrix(0, nrow=1, ncol=5))
    colnames(x)=c("Pclass", "Age",    "SibSp", "Parch",  "Fare")

    x[1,1]=as.numeric(input$Pclass)
    x[1,2]=input$Age
    x[1,3]=as.numeric(input$SibSp)
    x[1,4]=as.numeric(input$Parch)
    x[1,5]=input$Fare


    pred <- data.matrix(predict(object=fit_xgbTree, x, type="prob")[,2])
    isolate(values$df <- x)
    #isolate(values$df2 <- x)
  })

  output$testTable <- renderDataTable({values$df})
}

shinyApp(ui=ui, server=server)

【问题讨论】:

  • 我相信它可能是由 NA 引起的...在x[1,1]=as.numeric(input$Pclass) 中,由于 input$PClass 是选择“1st”、“2nd”、“3rd”,你会得到运行 as.numeric 的 NA。预测功能失败。所以你没有得到从预测函数返回的矩阵,也不能运行[,2]
  • Pclass 是数字。您可以通过运行 unique(df$Pclass) 来检查这一点
  • input$Pclass 是字符。
  • 啊,是的,谢谢。不过,我仍然遇到类似的错误 - 已修改问题以说明 Pclass
  • 您不需要输出表中的另一列来显示预测(生存概率)吗?否则你的代码可以正常工作。

标签: r machine-learning shiny r-caret


【解决方案1】:

服务器中的以下修改非常适合我(添加生存概率列,我认为这就是您想要的):

server=function(input, output){

  values <- reactiveValues()

  newEntry <- observe({ # use observe pattern

    x=as.data.frame(matrix(0, nrow=1, ncol=6))
    colnames(x)=c("Pclass", "Age",    "SibSp", "Parch",  "Fare", "SurvProb")

    x[1,1]=as.numeric(input$Pclass)
    x[1,2]=input$Age
    x[1,3]=as.numeric(input$SibSp)
    x[1,4]=as.numeric(input$Parch)
    x[1,5]=input$Fare

    pred <- data.matrix(predict(object=fit_xgbTree, x[-length(x)], type="prob")[,2])
    x[1,6] <- round(pred,2)

    isolate(values$df <- x)
    #isolate(values$df2 <- x)
  })

  output$testTable <- renderDataTable({values$df})
}

有输出

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-29
    • 1970-01-01
    • 2012-07-01
    • 2022-12-30
    • 1970-01-01
    • 1970-01-01
    • 2014-07-27
    • 1970-01-01
    相关资源
    最近更新 更多