2016-12-16 2 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

と私はそれが持っているかもしれないと考えています入力の$ PCLASSが選択肢 "第一"、 "第二"、 "第3"、あなたは'LLであるため、 'X [1,1] = as.numeric(入力の$ PCLASS)'で... NAによって引き起こされ'as.numeric'を実行してNAsを取得します。予測機能が失敗します。だから、予測関数から返されたマトリックスを得ることはありません、と '[2]'を実行することはできません。 – Jean

+0

PCLASSは数値です。あなたは – user3725021

+0

'入力$ Pclass'が文字である($ PCLASS DF)のユニークな実行することによって確認することができます。 – 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

関連する問題