基于Rshiny的多元线性回归模型预测值的动态模型

时间:2018-05-20 07:19:05

标签: r shiny

我使用可用的训练数据集mtcars运行多元线性回归模型。

现在我想针对测试数据集运行此模型。所以我试图在Shiny UI上手动从用户手动获取测试数据集。用户应该有一个选择依赖和独立变量的选项,根据独立变量和因变量选择,它应该给出测试数据集的预测值

目前,它正在发出错误"无法找到功能" modelq"

modelq是我建立的线性回归模型(基于mtcars数据集)来预测测试数据集的值 代码:

library(shiny)
library(datasets)
library(caret)
library(shiny)
library(curl)

library(shiny)

library(shiny)

ui <- fluidPage(
  titlePanel("My first predictive model"),
  sidebarLayout(
    sidebarPanel(
      fileInput('file1', 'Choose CSV File',
                accept=c('text/csv', 
                         'text/comma-separated-values,text/plain', 
                         '.csv')),
      tags$hr(),
      uiOutput("dependent"),
      uiOutput("independents"),
      tags$hr(),
      actionButton("action", "Predict!")
    ),
    mainPanel(
      verbatimTextOutput("regTab")
    )
  )
)

server <- function(input, output, session) {
  filedata <- reactive({
    infile <- input$file1
    if (is.null(infile)){
      return(NULL)      
    }
    read.csv(infile$datapath)
  })

  output$dependent <- renderUI({
    df <- filedata()
    if (is.null(df)) return(NULL)
    items=names(df)
    names(items)=items
    selectInput("dependent","Select ONE variable as dependent variable from:",items)
  })
  output$independents <- renderUI({
    df <- filedata()
    if (is.null(df)) return(NULL)
    checkboxGroupInput('independents','Select the regressors', choices = names(df))
  })
  #regression formula
  pred12 <- eventReactive(input$action, {
   modelq <- lm(as.formula(paste(input$dependent," ~ ",paste(input$independents,collapse="+"))),data=mtcars)
  pred1<-predict(modelq(),filedata())
  }
)
  #model
  output$regTab <- renderPrint({
    if(!is.null(input$independents)){
      pred12()
    } else {
      print(data.frame(Warning="Please select Model Parameters."))
    }
  })
}

shinyApp(ui, server)

0 个答案:

没有答案