SL42
SL42

Reputation: 221

How can I get my regression model results to be outputted in R shiny?

So I am trying to print out the result for my fit1 model, but I can't seem to resolve why I am getting this error message in my r shiny app: cannot coerce class ‘c("train", "train.formula")’ to a data.frame.

Edit: The train function is coming from the caret package.

The result I want to get:

  intercept     RMSE  Rsquared      MAE  RMSESD RsquaredSD    MAESD
  1      TRUE 11222.05 0.1019506 8760.358 377.088 0.01622065 168.4909

Sample data:

"","age","sex","bmi","children","smoker","region","expenses"
"1",19,"0",27.9,0,"1","0",16884.92
"2",18,"1",33.8,1,"0","1",1725.55
"3",28,"1",33,3,"0","1",4449.46
"4",33,"1",22.7,0,"0","2",21984.47
"5",32,"1",28.9,0,"0","2",3866.86
"6",31,"0",25.7,0,"0","1",3756.62
"7",46,"0",33.4,1,"0","1",8240.59
"8",37,"0",27.7,3,"0","2",7281.51
"9",37,"1",29.8,2,"0","3",6406.41
"10",60,"0",25.8,0,"0","2",28923.14
"11",25,"1",26.2,0,"0","3",2721.32
"12",62,"0",26.3,0,"1","1",27808.73

ui:

source(global.R)
sidebarLayout(
   sidebarPanel(
      sliderInput("proportion", "Proportion of data you want to use: ",
                              min = 0.1, max = 0.9, value = 0.5, step = 0.1),
      checkboxInput("mlrmodel","Check if you want to display the multiple linear regression model: "),
      conditionalPanel(
         condition = "input.mlrmodel == 1",
         checkboxInput("ageid", "Age"),
         checkboxInput("sexid", "Sex"),
                  )
      ),
      mainPanel(
                  box(tableOutput("mlrmodelplot"))
                )
              )

server:

 source(global.R)
 getData <- reactive({
 newData <- insurance_data_update #%>% filter(sex == input$sex)#, smoker==input$smoker, region==input$region)
 })
 
 
 trainsplit <- reactive({
 set.seed(123) # For reproducibility
 train = sample(1:nrow(getData()), size=nrow(getData())*input$proportion)
 })
 traindata <- reactive({
 train_data = getData()[trainsplit(), ]
 })
 testpilot <- reactive({
 test = dplyr::setdiff(1:nrow(getData()), trainsplit())
 })
 testdata <- reactive({
 test_data = getData()[testpilot(), ]
 })

 output$mlrmodelplot = renderTable({
 if(input$mlrmodel){
    if(input$ageid){
       fit1 <- train(expenses ~ age, data = traindata(),
                  method = "lm", preProcess = c("center", "scale"), trControl = trainControl(method = "cv",number = 5))
       fit1
    }
  }
})

Any help is greatly appreciated.

Upvotes: 0

Views: 174

Answers (1)

Sean McKenzie
Sean McKenzie

Reputation: 909

The solution to this problem is to use fit$results. The output from train() is a list, so you need to select the attribute you want, which in your case is the "results" attribute. Try updating your server function like this:

source(global.R)
 getData <- reactive({
 newData <- insurance_data_update #%>% filter(sex == input$sex)#, smoker==input$smoker, region==input$region)
 })
 
 
 trainsplit <- reactive({
 set.seed(123) # For reproducibility
 train = sample(1:nrow(getData()), size=nrow(getData())*input$proportion)
 })
 traindata <- reactive({
 train_data = getData()[trainsplit(), ]
 })
 testpilot <- reactive({
 test = dplyr::setdiff(1:nrow(getData()), trainsplit())
 })
 testdata <- reactive({
 test_data = getData()[testpilot(), ]
 })

 output$mlrmodelplot = renderTable({
 if(input$mlrmodel){
    if(input$ageid){
       fit1 <- train(expenses ~ age, data = traindata(),
                  method = "lm", preProcess = c("center", "scale"), trControl = trainControl(method = "cv",number = 5))
       fit1$results
    }
  }
})

Upvotes: 1

Related Questions