I am running a multiple linear regression model using an available training dataset mtcars.
Now I want to run this model against a test dataset. So I am trying to fetch test dataset manually from user on Shiny UI. User should have an option to select the dependant and independant variables, based on the independant and dependant variable selection, it should give the predicted value of the test dataset
Currently, its is giving an error "could not find function "modelq"
modelq is the linear regression model which I have built(based on mtcars dataset) to predict the values of the test dataset Code
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)