r - 通过 R Shiny 进行逻辑回归

标签 r dataframe shiny glm

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

步骤:

  1. 用户将上传 CSV(第一个选项卡)
  2. 用户选择自变量(第二个选项卡)
  3. 用户选择其他变量(第二个选项卡)
  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=二项式,data=df)

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

提前致谢

最佳答案

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

您可以通过使用 as.forumla 函数在 glm 中使用字符串变量。请参阅 Eric Green 的示例 http://stackoverflow.com/questions/17024685/how-to-use-a-character-string-in-formula ,

我搞乱了你的程序并让它工作 - 请注意它需要一个值在 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 加载的 enter image description here

这是可配置的glm:

enter image description here

关于r - 通过 R Shiny 进行逻辑回归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41141157/

相关文章:

r - 什么可以阻止 h2o 响应我们的 R 脚本?

apache-spark - 如何使用withColumn计算列中的最大值?

r - 如何将 "reactive"数据作为参数传递给 R Markdown?

渲染一个 Shiny 的仪表板信息框......没有 Shiny 的仪表板

docker - 通过 nginx 和 letsencrypt ssl 使用 docker 运行 Shiny 的应用程序

r - 局部变量的用法

r - ggplot2:手动添加图例

r - 如何加速这种双 for 循环?

python - 使用参数列表删除行 Pandas

python - 如何在 Pandas 数据框上应用定义的函数