我正在尝试定义多项逻辑回归的公式,它应该从下拉列表中获取最多 6 个独立变量的输入。 ( SelectInput , Multiple = TRUE )在 R Shiny 中。无法弄清楚如何解决这个问题..
这里是示例代码... 公式
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())})
任何指导都将受到高度赞赏...谢谢
最佳答案
我们可以尝试
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)
}
-数据
ml <- read.dta("https://stats.idre.ucla.edu/stat/data/hsbdemo.dta")
-运行 Shiny
fmnom(ml, depVar = c("prog", "schtyp"), indepVar = c("ses", "read", "write") )
-输出单个自变量
-输出多个自变量
关于r - 如何从R Shiny 的选择输入(multiple = TRUE)动态定义公式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45985140/