使用RShiny中的回归模型来打印预测输出

时间:2018-02-07 12:41:31

标签: r interface regression predict

我使用xgboost建立了一个回归模型来预测房屋的价格。我现在正在尝试构建一个界面,用于打印用户输入输入变量的房屋的预测价格。

然而,我无法弄清楚如何让它发挥作用。我试过用模型创建一个函数并调用函数,我试过这个:

library(shiny)
library(xgboost)

ui=fluidPage(
  numericInput(inputId="GrLivArea", label="Enter Ground Living Area", 
               value=0,min=0, max=100000),
  numericInput(inputId="OverallRate", label="Enter Overall rating", 
               value=1,min=1, max=20),
  numericInput(inputId="AreaInside", label="Enter Area Inside", 
               value=0,min=0, max=100000),
  selectInput(inputId = "Neighborhood", label="Choose neighborhood of house",
              choices = list("Blmngtn"=Blmngtn,"Blueste"=Blueste,"BrDale"=BrDale, "BrkSide"=BrkSide,"ClearCr"=ClearCr,"CollgCr"=CollgCr,
                             "Crawfor"=Crawfor,"Edwards"=Edwards,"Gilbert"=Gilbert, "Greens"=Greens,"GrnHill"=GrnHill,"IDOTRR"=IDOTRR,
                             "Landmrk"=Landmrk,"MeadowV"=MeadowV,"Mitchel"=Mitchel, "NAmes"=NAmes,"NoRidge"=NoRidge,"NPkVill"=NPkVill,
                             "NridgHt"=NridgHt,"NWAmes"=NWAmes,"OldTown"=OldTown, "Sawyer"=Sawyer,"SawyerW"=SawyerW,"Somerst"=Somerst,
                             "StoneBr"=StoneBr,"SWISU"=SWISU,"Timber"=Timber,"Veenker"=Veenker)),
  numericInput(inputId="TotalArea", label="Enter total area of property", 
               value=0,min=0, max=10000000),
  actionButton("Enter", "Enter Values")
)

server <- function(input,output, session) {
  observeEvent(input$Enter, {
    t <- data.frame(input$GrLivArea, input$AreaInside, as.factor(input$Neighborhood),
                    input$TotalArea, as.factor(input$OverallRate))
    bst <- xgboost(data.matrix(training),
                   label = training$SalePrice,
                   verbose=0, max.depth = 2, eta = 0.1, gamma=0,
                   nrounds = 500, colsample_bytree = 0.8, min_child_weight = 3)
    output$modelSummary <- renderPrint({
        Predict(bst, data.matrix(t))
    })
})
}
shinyApp(ui=ui, server=server)

1 个答案:

答案 0 :(得分:0)

由于您没有提供数据,我只使用了xgboost包的虚拟数据。 我认为您的示例可行,但您没有包含renderPrint modelSummary )的任何输出。

我认为Predict应为predict

library(shiny)
library(xgboost)

data(agaricus.train, package='xgboost')
data(agaricus.test, package='xgboost')

ui=fluidPage(
  numericInput(inputId="GrLivArea", label="Enter Ground Living Area", 
               value=0,min=0, max=100000),
  numericInput(inputId="OverallRate", label="Enter Overall rating", 
               value=1,min=1, max=20),
  numericInput(inputId="AreaInside", label="Enter Area Inside", 
               value=0,min=0, max=100000),
  selectInput(inputId = "Neighborhood", label="Choose neighborhood of house",
              choices = list("Blmngtn"="Blmngtn","Blueste"="Blueste","BrDale"="BrDale"
                             # "BrkSide"=BrkSide,"ClearCr"=ClearCr,"CollgCr"=CollgCr,
                             # "Crawfor"=Crawfor,"Edwards"=Edwards,"Gilbert"=Gilbert,
                             # "Greens"=Greens,"GrnHill"=GrnHill,"IDOTRR"=IDOTRR,
                             # "Landmrk"=Landmrk,"MeadowV"=MeadowV,"Mitchel"=Mitchel,
                             # "NAmes"=NAmes,"NoRidge"=NoRidge,"NPkVill"=NPkVill,
                             # "NridgHt"=NridgHt,"NWAmes"=NWAmes,"OldTown"=OldTown,
                             # "Sawyer"=Sawyer,"SawyerW"=SawyerW,"Somerst"=Somerst,
                             # "StoneBr"=StoneBr,"SWISU"=SWISU,"Timber"=Timber,"Veenker"=Veenker)
              )),
  numericInput(inputId="TotalArea", label="Enter total area of property", 
               value=0,min=0, max=10000000),
  actionButton("Enter", "Enter Values"),
  verbatimTextOutput("modelSummary")
)

server = function(input,output, session){
  observeEvent( input$Enter, {
    Gr.Liv.Area = input$GrLivArea
    Area.Inside = input$AreaInside
    Neighborhood = as.factor(input$Neighborhood)
    Total.Area = input$TotalArea
    Overall.Qual = as.factor(input$OverallRate)
    t = data.frame(Gr.Liv.Area, Area.Inside, Neighborhood,Total.Area,Overall.Qual)

    # bst = xgboost(data.matrix(training), 
    #               label = training$SalePrice, verbose=0, max.depth = 2, 
    #               eta = 0.1, gamma=0, nrounds = 500, colsample_bytree=0.8, min_child_weight=3)

    bst = xgboost(data = agaricus.train$data, label = agaricus.train$label, 
            max_depth = 2, eta = 1, nthread = 2, nrounds = 2, 
            objective = "binary:logistic")

    output$modelSummary <- renderPrint({
      predict(bst, agaricus.test$data)
    })
  })
}
shinyApp(ui=ui, server=server)