2017-08-31 38 views
2

我想定義一個多項式邏輯迴歸的公式,它應該從下拉列表中輸入最多6個獨立變量。 (SelectInput,Multiple = TRUE)在R Shiny中。無法弄清楚如何解決這個..如何從R選擇輸入(多=真)動態定義公式

下面是示例代碼... 公式

Multiformula <- reactive ({ as.formula(paste(input$outcome,'~'input$predictor) })

型號

MultiModel <- reactive({ 
    multinom(Multiformula(), data = filtered()) 
    }) 

上面的代碼工作單變量,但對於不止一個自變量的方法可能會有所不同。我想下面的,但沒有運氣

indvar6 <- reactive({ 
    filter(forest_data_model[,input$predictor]) 
    }) 

重新定義公式......但它沒有工作

Multiformula <- reactive ({as.formula(paste(input$outcome,'~'indvar6())}) 

任何指導,將不勝感激...謝謝

+0

嘗試'as.formula(膏(輸入$結果,'〜',粘貼(輸入$ p redictor,collapse =「+」)))' – akrun

+0

非常感謝您的快速解決方案...正常工作 – nab

回答

1

我們可以嘗試

library(shiny) 
library(nnet) 
library(foreign) 
fmnom <- function(data = NULL, depVar, indepVar) { 

    ui <- fluidPage(
    headerPanel("Multinomial analysis"), 
    sidebarPanel(
     p("Select inputs for the Dependent Variable"), 
     selectInput(inputId = "dep", label = "Dependent Variables", multiple = FALSE, 
         choices = as.list(depVar)), 
     p("Select input for the Independent Variable"), 
     selectInput(inputId = "indep", label = "Independent Variables", 
        multiple = TRUE, choices = as.list(indepVar), selected = indepVar[1]) 
    ), 
    mainPanel(
     verbatimTextOutput(outputId = "RegOut"), 
     verbatimTextOutput(outputId = "IndPrint"), 
     verbatimTextOutput(outputId = "DepPrint") 

    ) 
) 

    server <- function(input, output) { 

    mlt <- reactive(
        {multinom(reformulate(input$indep, input$dep), data = data)}) 

    output$DepPrint <- renderPrint({input$dep}) 
    output$IndPrint <- renderPrint({input$indep}) 
    output$RegOut <- renderPrint({summary(mlt())}) 

    } 



    shinyApp(ui= ui, server = server) 
} 

-data

ml <- read.dta("https://stats.idre.ucla.edu/stat/data/hsbdemo.dta") 

-run光澤

fmnom(ml, depVar = c("prog", "schtyp"), indepVar = c("ses", "read", "write")) 

-output單個自變量

enter image description here

-output多個獨立變量

enter image description here

+0

我們可以繪製所選變量的效果圖嗎?我在下面的代碼中使用了一個預測器輸出$ plot < - renderPlot(plot <-effect(input $ predictor,model()) plot(plot,style =「stacked」,rug = FALSE,ci.style =「樂隊「,show.strip.values = TRUE, show.fitted = TRUE,multiline = TRUE) }) – nab

+0

@nab請問您可以發佈一個新問題,因爲它不是我清楚 – akrun

+1

當然我張貼一個新的問題 – nab