Search code examples
rshinystatisticsregressionlinear-regression

Regression in shinyApp with dynamic variable selection


I want to perform linear regressions of Feature_A and I want the user to select the other variable dynamically. I also want to display statistics about my overall predictive model fit adjusted R2, each model estimated parameter coefficient, and coefficient p-values.

Below is what I could come up with. Needless to say it does not work. I have been struggling with it and any help will be very greatly appreciated

library(shiny)
library(ggplot2)
library(dplyr)
library(purrr)
       
Feature_A <- c(1, 2,1, 4,2)
Feature_B <- c(4,5,6,6,6)
Feature_C <- c(22,4,3,1,5)
df<- data.frame(Feature_A ,Feature_B ,Feature_C)
                           
 # Define UI for application
 ui= fluidPage(
                  
 # Header or Title Panel 
   titlePanel(title = h4("Regression")),
      sidebarLayout(
       # Sidebar panel
         sidebarPanel(
          selectInput('ip', 'Select an Explanatory Variable', names(df)),
          actionButton(inputId = "btn1",label="Regression Plot"),
          actionButton(inputId = "btn2",label="Show Stats")),
                    
                    
                    
      # Main Panel
      mainPanel("main panel", regOutput("regplot"),
                              verbatimTextOutput("summary"))
                      
                    ))
     server = function(input, output,session) {
                  
     #code for regression
    lm_fit <- lm(Feature_A ~ input$ip, data=df)
                  
  summary_stats <- eventReactive(input$btn2,{summary(lm_fit)
                  })

                  
regression_plot<- eventReactive(input$btn1,{ggplot(data = df, aes(x = input$ip, y = Feature_A)) + 
                      geom_point(color='blue') +
                      geom_smooth(method = "lm", se = FALSE)
                    
                  })
                  #end of regression code
                  
                  
          
                  output$regplot <- renderPlot({
                    regression_plot()
                  })
                  output$summary <- renderPrint({
                    summary_stats()
                  })
                  
                }
                
shinyApp(ui,server)

Solution

  • A few things are wrong here:

    • regOutput is not an existing command, you want plotOutput instead.
    • lm_fit <- lm(Feature_A ~ input$ip, data=df) should be in a reactive since it uses input$ip. This means you need lm_fit() to get the results, and not lm_fit.
    • Also, input$ip is a character, and lm() requires a formula. Therefore, you need to wrap the whole formula in as.formula.

    This should work now, the plot is a bit strange but I think it's due to your simplified example:

    library(shiny)
    library(ggplot2)
    library(dplyr)
    library(purrr)
    
    Feature_A <- c(1, 2,1, 4,2)
    Feature_B <- c(4,5,6,6,6)
    Feature_C <- c(22,4,3,1,5)
    df<- data.frame(Feature_A ,Feature_B ,Feature_C)
    
    
    # Define UI for application
    ui= fluidPage(
      
      # Header or Title Panel 
      titlePanel(title = h4("Regression")),
      sidebarLayout(
        # Sidebar panel
        sidebarPanel(
          selectInput('ip', 'Select an Explanatory Variable', names(df)),
          actionButton(inputId = "btn1",label="Regression Plot"),
          actionButton(inputId = "btn2",label="Show Stats")),
        
        
        
        # Main Panel
        mainPanel("main panel", plotOutput("regplot"),
                  verbatimTextOutput("summary"))
        
      ))
    server = function(input, output,session) {
      
      #code for regression
      lm_fit <- reactive({
        lm(as.formula(paste0("Feature_A ~ ", input$ip)), data=df)
      })
      
      summary_stats <- eventReactive(input$btn2,{
        summary(lm_fit())
      })
      
      
      regression_plot<- eventReactive(input$btn1, {
        ggplot(data = df, aes(x = input$ip, y = Feature_A)) + 
          geom_point(color='blue') +
          geom_smooth(method = "lm", se = FALSE)
        
      })
      #end of regression code
      
      
      
      output$regplot <- renderPlot({
        regression_plot()
      })
      output$summary <- renderPrint({
        summary_stats()
      })
      
    }
    
    shinyApp(ui,server)