闪亮[列的变换因子]

时间:2018-05-14 09:46:05

标签: r shiny logistic-regression

我正在研究葡萄酒数据集(https://github.com/Honghe/notebook_mermaid),我需要准备一个小项目来传递一个主题......

所以 - 这个数据集有一列有3个值(1,2或3)。这些例子是葡萄酒类。

我必须转换它们的值才能准备Logistic回归(所以 - 值0或1)。我想选择两个值(例如1和3)并使Logistic回归依赖于其他值(例如酒精)。

此刻我还是那样:

shinyUI(navbarPage("Wine Data Set Analysis",
                   tabPanel("Data Import and configuration",
                            sidebarLayout(sidebarPanel( 
                              fileInput("file","Upload CSV file",multiple = FALSE),
                              tags$hr(),
                              h5(helpText("Select the parameters of your file below")),
                              checkboxInput(inputId = 'header', label = 'Header', value = FALSE),
                              radioButtons(inputId = 'sep', label = 'Separator', 
                                           choices = c(Comma=',',Semicolon=';',Tab='\t', Space=''), selected = ',')
                            ),
                            mainPanel(uiOutput("tb1"))
                            ) ),


               tabPanel("Logistic Regression",
                        sidebarLayout(sidebarPanel(
                          helpText("Select values for Logistic Regression"),
                          uiOutput("var_select"),
                          uiOutput("rest_var_selectx"),
                          uiOutput("one"),
                          uiOutput("proba")
                          # uiOutput("var1_select"),
                          # uiOutput("rest_var_select")
                          ),
                          mainPanel( helpText("The result is: "),
                                     verbatimTextOutput("other_val_show"),
                                     helpText("The plot to visualise the regression"),
                                     plotOutput("plotx")
                          )))
))

服务器:

library(shiny)
library(ggplot2)
library(dplyr)

# Define server logic required to draw a histogram
shinyServer(function(input, output, session) {

  data <- reactive({
    file1 <- input$file
    if(is.null(file1)){return()} 
    read.table(file=file1$datapath, sep=input$sep, header = input$header)
  })  

  output$table <- renderTable({
    if(is.null(data())){return ()}
    data()
  })
  output$tb1 <- renderUI({
    tableOutput("table")
  })


  output$var_select<-renderUI({

    checkboxGroupInput("ind_var_select","Select Independent Variation", choices = c("0", "0.5", "1"), selected = c("0", "0.5"))
  })
  output$rest_var_selectx<-renderUI({
    checkboxGroupInput("other_var_selectx","Select other Variationx",choices =as.list(names(data())))
  })

wybrane <- reactive({

  if(input$ind_var_select == "0")
  {
    if (input$ind_var_select == "0.5")
    {
      as.factor(data1$Class)
      dane1<- factor(levels(data1$Class), labels =  c("0","1"))
      data1$Cl<-as.numeric(dane1)




      dane <- sprintf("%s~%s",data1$Cl, paste0(input$other_var_selectx,collapse="+"))
    print(dane)
    #print(nrow(dplyr::select(f,input$ind_var_select)))
    logreg <-glm(as.formula(form),family=binomial(),data=f)
    print(summary(logreg))

    output$plotx<-renderPlot({
      # kolumna<-input$other_var_select,
      # dane<-f$kolumna,
      # ggplot2::ggplot(f,aes(x=unlist(dplyr::select(f,input$other_var_select)),
      #                       y=unlist(dplyr::select(f,input$ind_var_select))))+geom_point()+geom_smooth(method="glm",method.args=list(family="binomial"))
      # 
    }) 
    }
  }


})
你可以帮帮我吗?

1 个答案:

答案 0 :(得分:1)

我不会让它变得如此复杂,而只是使用ifelse。我相信你想象这样的事情

library(shiny)
library(ggplot2)
library(dplyr)

shinyApp(
  ui = navbarPage("Wine Data Set Analysis",
                          tabPanel("Data Import and configuration",
                                   sidebarLayout(sidebarPanel( 
                                     fileInput("file","Upload CSV file",multiple = FALSE),
                                     tags$hr(),
                                     h5(helpText("Select the parameters of your file below")),
                                     checkboxInput(inputId = 'header', label = 'Header', value = FALSE),
                                     radioButtons(inputId = 'sep', label = 'Separator', 
                                                  choices = c(Comma=',',Semicolon=';',Tab='\t', Space=''), selected = ',')
                                   ),
                                   mainPanel(uiOutput("tb1"))
                                   ) ),


                          tabPanel("Logistic Regression",
                                   sidebarLayout(sidebarPanel(
                                     helpText("Select values for Logistic Regression"),
                                     uiOutput("var_select"),
                                     uiOutput("rest_var_selectx"),
                                     uiOutput("one"),
                                     uiOutput("proba")
                                     # uiOutput("var1_select"),
                                     # uiOutput("rest_var_select")
                                   ),
                                   mainPanel( helpText("The result is: "),
                                              verbatimTextOutput("other_val_show"),
                                              helpText("The plot to visualise the regression"),
                                              plotOutput("plotx")
                                   )))
  ),
  server = function(input, output, session) {

    data <- reactive({
      file1 <- input$file
      if(is.null(file1)){return()} 
      read.table(file=file1$datapath, sep=input$sep, header = input$header)
    })  

    output$table <- renderTable({
      if(is.null(data())){return ()}
      data()
    })
    output$tb1 <- renderUI({
      tableOutput("table")
    })


    output$var_select<-renderUI({

      checkboxGroupInput("ind_var_select","Select Independent Variation", choices = c( "1", "2","3"), selected = c("1", "3"))
    })
    output$rest_var_selectx<-renderUI({
      checkboxGroupInput("other_var_selectx","Select other Variationx",choices =as.list(names(data())))
    })

    wybrane <- reactive({
      dta <- data()
      dta$Cl<-ifelse(dta$V1 %in% input$ind_var_select,1,0)




      dane <- sprintf("%s~%s",dta$Cl, paste0(input$other_var_selectx,collapse="+"))
      print(dane)
      #print(nrow(dplyr::select(f,input$ind_var_select)))
      logreg <-glm(as.formula(paste0("Cl ~ ",paste0(input$other_var_selectx,collapse = " + "))),family=binomial(),data=dta)
      print(summary(logreg))
      dta    
        })
    output$plotx<-renderPlot({
      req(!is.null(input$ind_var_select) &&
            !is.null(input$other_var_selectx))
      f = wybrane()
      ggplot2::ggplot(f,aes_string(x=input$other_var_selectx,
                            y="Cl"))+geom_point()+geom_smooth(method="glm",method.args=list(family="binomial"))

    }) 
  }
)

希望这有帮助!

相关问题