通过 R Shiny 进行逻辑回归

Logistic Regression through R Shiny

我正在尝试在 RShiny 中开发一个应用程序。 我的Objective: 可以进行逻辑回归并显示输出的应用程序。

步骤:

  1. 用户将上传 CSV(第一个 TAB)
  2. 用户select自变量(第二个TAB)
  3. 用户Select其他变量(第二个TAB)
  4. 第二个选项卡中的主面板将显示后勤摘要 回归。

我的代码:

library(shiny)
ui<-navbarPage("Model Developement by Subhasish",
               tabPanel("Data Import",sidebarLayout(sidebarPanel( fileInput("file","Upload your CSV",multiple = FALSE),
                                                                  tags$hr(),
                                                                  h5(helpText("Select the read.table parameters below")),
                                                                  checkboxInput(inputId = 'header', label = 'Header', value = FALSE),
                                                                  checkboxInput(inputId = "stringAsFactors", "stringAsFactors", FALSE),
                                                                  radioButtons(inputId = 'sep', label = 'Separator', choices = c(Comma=',',Semicolon=';',Tab='\t', Space=''), selected = ',')
               ),
               mainPanel(uiOutput("tb1"))
               ) ),
               tabPanel("Model_dev",sidebarLayout(sidebarPanel(uiOutput("model_select"),uiOutput("var1_select"),uiOutput("rest_var_select")),mainPanel( helpText("Your Selected variables"),verbatimTextOutput("other_val_show"))))
)
server<-function(input,output) { data <- reactive({
  file1 <- input$file
  if(is.null(file1)){return()} 
  read.table(file=file1$datapath, sep=input$sep, header = input$header, stringsAsFactors = input$stringAsFactors)

})  
output$table <- renderTable({
  if(is.null(data())){return ()}
  data()
})
output$tb1 <- renderUI({
  tableOutput("table")
})
output$model_select<-renderUI({
  selectInput("modelselect","Select Algo",choices = c("Logistic_reg"="logreg","SVM"="svm"))
})
output$var1_select<-renderUI({
  selectInput("ind_var_select","Select Independent Var", choices =as.list(names(data())),multiple = FALSE)
})
output$rest_var_select<-renderUI({
  checkboxGroupInput("other_var_select","Select other Var",choices =as.list(names(data())))
})
output$other_val_show<-renderPrint({
  input$other_var_select

  #f<-data()
  #library(caret)
  #logreg<-glm(f[,1]~.,family = binomial,data=f)
  #summary(logreg)

})

}
shinyApp(ui=ui,server=server)

至此 CSV 上传部分完成。 glm() 函数 req 结构面临的问题如下: glm(var 1~ var 2+var 3+ var 4,family=binomial,data=df)

如何使用 var 2+ var 3.. 等复选框值? 我使用的是过去 1 周以来的 Shiny R。所以可能有任何我无法发现的更简单的解决方案。

提前致谢

你非常接近,一旦我明白你想要做什么,只需要几行就可以完成它。这是一个很好的例子,将数据框加载到 shiny 中,为 glm 选择该框架中的列,然后执行它。

您可以使用 as.forumla 函数在 glm 中使用字符串变量。参见 Eric Green 的示例 ,

我搞砸了你的程序并让它工作 - 请注意它需要一个值介于 0 和 1 之间的列才能工作 - 我使用了 randu 数据集 (write.csv(randu,"randu.csv"):

library(shiny)
ui<-navbarPage("Model Developement by Subhasish",
               tabPanel("Data Import",
                        sidebarLayout(sidebarPanel( fileInput("file","Upload your CSV",multiple = FALSE),
                              tags$hr(),
                              h5(helpText("Select the read.table parameters below")),
                              checkboxInput(inputId = 'header', label = 'Header', value = FALSE),
                              checkboxInput(inputId = "stringAsFactors", "stringAsFactors", FALSE),
                                          radioButtons(inputId = 'sep', label = 'Separator', 
                                         choices = c(Comma=',',Semicolon=';',Tab='\t', Space=''), selected = ',')
               ),
               mainPanel(uiOutput("tb1"))
               ) ),
               tabPanel("Model_dev",
                        sidebarLayout(sidebarPanel(
                          uiOutput("model_select"),
                          uiOutput("var1_select"),
                          uiOutput("rest_var_select")),
                          mainPanel( helpText("Your Selected variables"),
                                     verbatimTextOutput("other_val_show"))))
)
server<-function(input,output) { data <- reactive({
  file1 <- input$file
  if(is.null(file1)){return()} 
  read.table(file=file1$datapath, sep=input$sep, header = input$header, stringsAsFactors = input$stringAsFactors)

})  
output$table <- renderTable({
  if(is.null(data())){return ()}
  data()
})
output$tb1 <- renderUI({
  tableOutput("table")
})
output$model_select<-renderUI({
  selectInput("modelselect","Select Algo",choices = c("Logistic_reg"="logreg","SVM"="svm"))
})
output$var1_select<-renderUI({
  selectInput("ind_var_select","Select Independent Var", choices =as.list(names(data())),multiple = FALSE)
})
output$rest_var_select<-renderUI({
  checkboxGroupInput("other_var_select","Select other Var",choices =as.list(names(data())))
})
output$other_val_show<-renderPrint({
  input$other_var_select
  input$ind_var_select
  f<-data()

  library(caret)
  form <- sprintf("%s~%s",input$ind_var_select,paste0(input$other_var_select,collapse="+"))
  print(form)

  logreg <-glm(as.formula(form),family=binomial(),data=f)
  print(summary(logreg))

})

}
shinyApp(ui=ui,server=server)

这里的数据是从randu.csv加载的

这是可配置的 glm: