EzrealReal
EzrealReal

Reputation: 83

Create Logistic Regression in R Shiny

I am trying to build a Logistic Regression in R Shiny, but have so much difficult time. Credit to Bruno's answer to another question here, I was able to come with some ideas but my code still doesn't work.

However, I am completely lost at this point and have no idea how to continue with that. I really appreciate any help, guide, or advice from anyone!!

library(shinythemes)
library(shinyWidgets)
library(shiny)
library(shinydashboard)
library(recipes)
#data(mtcars)

AttributeChoices=c("Borough_X", "Avg..Income.H.hold", "Month", "Season", "PartOfDay")


# Define UI for application
ui = fluidPage(
  navbarPage("R Shiny Dashboard",
             tabPanel("Welcome",
                      tabName = "welcome",
                      icon=icon("door-open"),

                      fluidPage(theme=shinytheme("cerulean"),
                                h1("Welcome to my Shiny Dashboard!"),
                                br(),
                                p(strong(tags$u("What is this dashboard all about?"))),
                                p("I'm going to do stuff."),  
                                br(),
                                p(strong(tags$u("Here's another question."))),
                                p("Here's my answer."),
                                br(),
                                p(strong(tags$u("How can I use this dashboard?"))),
                                p("You can click on any of the tabs above to see a different analysis of the data.")
                      )),

             tabPanel("Regression",
                      tabname="regression",
                      icon=icon("calculator"),
                      selectInput(inputId = "indep", label = "Independent Variables", 
                                  multiple = TRUE, choices = as.list(AttributeChoices), selected = AttributeChoices[1]),
                      verbatimTextOutput(outputId = "RegOut")

             )
  ))

# Define server logic 
df <- read.csv('data/sidedf.csv')

server <- function(input, output) {
  recipe_formula <- reactive(df %>%
                               recipe() %>%
                               update_role(df = "outcome") %>%
                               update_role(!!!input$indep,new_role = "predictor") %>% 
                               formula())
  
  glm_reg <- reactive(
    glm(recipe_formula(),data = df)
  )
  
  
  output$RegOut = renderPrint({summary(glm_reg())})
  
}

My data: Y = Sidewalk_Condition, X = the rest of columns

Sidewalk_Condition | Borough_X     | Avg..Income.H.hold | Month | Season | PartOfDay
-------------------------------------------------------------------------------------
1                  | Staten Island | 21109              | 6     | winter |  evening
0                  | Bronx         | 32034              | 12    | fall   |  afternoon
1                  | Queens        | 52304              | 7     | summer |  midday

Upvotes: 1

Views: 872

Answers (1)

writer_typer
writer_typer

Reputation: 798

You almost got it. I think you missed adding Sidewalk_Condition in the outcome update_role function.

Try this:

recipe_formula <- reactive(df %>%
                               recipe() %>%
                               update_role(Sidewalk_Condition, new_role = "outcome") %>%
                               update_role(!!!input$indep,new_role = "predictor") %>% 
                               formula())

enter image description here

Upvotes: 1

Related Questions