Caret和Shiny:无法创建由Caret模型驱动的预测应用

我正在尝试开发一个简单的Shiny应用,用于预测泰坦尼克号乘客在给定年龄、舱位、票价等条件下的生存概率。我希望这些变量是动态的,并且希望使用底层的Caret模型来计算预测的生存概率。

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

警告:在[.data.frame中出错:选择了未定义的列 堆栈跟踪(最内层优先): 70: [.data.frame 69: [ 68: sweep 67: predict.preProcess 66: predict 65: probFunction 64: predict.train 63: predict 62: predict 61: is.data.frame 60: data.matrix 59: observerFunc [#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)

回答:

以下在服务器端的修改对我来说效果很好(添加了生存概率列,我想这就是你想要的):

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})}

输出如下 enter image description here

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注