在 r Shiny 中对反应数据框进行多元回归的最佳方法是什么?

问题描述

我有一个反应式数据框,我希望用户从该反应式数据框中选择因变量和多个自变量并返回回归输出。 有人对在 Shiny 中对反应式数据框进行多元回归的最佳方法有什么建议吗?

我看到这个帖子:Using R Shiny for Multiple Linear Regression (SelectInput --> multiple=TRUE)

但我已经评论显示代码不起作用。

我也看到了这个问题:Perform multiple linear regression with variables based on shiny widget selection

但这只是简单的 1 比 1 回归。

解决方法

好的,所以我查看了您说的 first answer 不起作用,我对它稍作修改以允许您也选择因变量。当您将因变量包含在自变量中时会出现错误,但我相信您可以找到一种方法来确保因变量不作为选择包含在自变量中。

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

AttributeChoices=c("mpg","cyl","disp","hp","drat","wt","qsec","vs")


# 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."),p(strong(tags$u("Here's another question."))),p("Here's my answer."),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="dependent",label = "Dependent Variables",choices = as.list(AttributeChoices)),selectInput(inputId = "indep",label = "Independent Variables",multiple = TRUE,choices = as.list(AttributeChoices),selected = AttributeChoices[1]),verbatimTextOutput(outputId = "RegOut")
                        
               )
    ))
# Define server logic 
server <- function(input,output) {
    
    #-------------------REGRESSION-------------------#
    
    recipe_formula <- reactive(mtcars %>%
                                   recipe() %>%
                                   update_role(!!!input$dependent,new_role = "outcome") %>%
                                   update_role(!!!input$indep,new_role = "predictor") %>% 
                                   formula())
    
    lm_reg <- reactive(
        lm(recipe_formula(),data = mtcars)
    )
    
    
    output$RegOut = renderPrint({summary(lm_reg())})
    
}

# Run the application 
shinyApp(ui = ui,server = server)
,

出于某种原因,它现在需要 prep(),只需将它添加到管道的末尾,我也改进了 @dodo1672 所说的选择。

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

AttributeChoices=c("mpg","vs")


# Define UI for application
ui = fluidPage(
  navbarPage("R Shiny Dashboard",p("You can click on any of the tabs above to see a different analysis of the data.")
                      )),uiOutput("indep"),verbatimTextOutput(outputId = "RegOut")
                      
             )
  ))
# Define server logic 
server <- function(input,output) {
  
  #-------------------REGRESSION-------------------#
  
  
  
  output$indep <- renderUI({
    selectInput(inputId = "indep",choices = as.list(AttributeChoices[AttributeChoices!= input$dependent]),selected = AttributeChoices[1])
  })
  
  
  
  recipe_formula <- reactive({
    req(input$indep)
    mtcars %>%
      recipe() %>%
      update_role(!!!input$dependent,new_role = "outcome") %>%
      update_role(!!!input$indep,new_role = "predictor") %>%
      prep() %>% 
      formula()
    })
  
  lm_reg <- reactive(
    lm(recipe_formula(),data = mtcars)
  )
  
  
 
  
  output$RegOut = renderPrint({
    summary(lm_reg())
    })
  
}

# Run the application 
shinyApp(ui = ui,server = server)