2016-12-16 24 views
3

我試圖開發一個簡單的應用程序,用於預測在給定某個年齡,班級,票價等的情況下乘客倖存泰坦尼克號的概率。我希望這些變量是動態的,並且希望使用底層脫字符模型計算預測的生存概率。插入符號和閃亮符號:無法創建由插入符號模式驅動的預測應用程序

當運行這段代碼,我收到以下錯誤信息:

Warning: Error in [.data.frame: undefined columns selected Stack trace (innermost first): 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: ERROR: [on_request_read] connection reset by peer

我的代碼如下。任何想法是什麼導致這個錯誤?非常感謝。

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) 
+0

我相信它可能有是由NA導致的...在'x [1,1] = as.numeric(輸入$ Pclass)'中,由於輸入$ PClass是選擇「1st」,「2nd」,「3rd」,所以通過運行'as.numeric'來獲得NAs。預測功能失敗。所以你不會得到預測函數返回的矩陣,也不能運行'[,2]'。 – Jean

+0

Pclass是數字。你可以通過運行唯一的(df $ Pclass) – user3725021

+0

'input $ Pclass'來檢查這個字符。 – Jean

回答

2

在服務器下面的修改工作非常適合我(加上一個生存概率列,我想這就是你想要的):

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