在 R Shiny 中创建逻辑回归

Create Logistic Regression in R Shiny

我正在尝试在 R Shiny 中构建逻辑回归,但遇到了很多困难。 感谢布鲁诺在这里对另一个问题的回答,我能够提出一些想法,但我的代码仍然不起作用。

但是,此时我完全迷失了,不知道如何继续。我非常感谢任何人的任何帮助、指导或建议!!

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())})
  
}

我的数据:Y = Sidewalk_Condition,X = 其余列

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

你差不多明白了。我认为您错过了在结果 update_role 函数中添加 Sidewalk_Condition

试试这个:

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