首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ShinyApp中的回归

ShinyApp中的回归
EN

Stack Overflow用户
提问于 2018-12-20 22:56:11
回答 1查看 117关注 0票数 0

我正在尝试为shinyApp编写一段代码,在该代码中,我选择一个响应变量(只有一个选择框),然后在checkbox中选择我想要的所有协变量。此外,我想指定我的响应变量(高斯、泊松、二项式等)的分布族。我不知道在主面板和服务器中写什么。有人能帮帮我吗?

代码语言:javascript
复制
library(shiny) 
library(plotly)
data(iris)

ui<-fluidPage(
  titlePanel("Regression"),
  sidebarPanel(
    selectInput("var",label="Choose the response variable:",choice=names(iris), selectize=FALSE),
    tags$hr(),
    checkboxGroupInput(inputId ="ind_var",label = "Select at least two independent variables:", choices = names(iris)),

    mainPanel(

    )
  )
)

server<-function(input,output){


}
shinyApp(ui, server)
EN

回答 1

Stack Overflow用户

发布于 2018-12-21 00:58:04

也许这个小例子可以帮助你。

如果您想要动态创建公式,则必须将其包装到reformulateas.formula中。

此shinyApp示例打印出线性回归的摘要,并根据该模型绘制预测:

代码语言:javascript
复制
library(shiny) 
library(plotly)
data(iris)

ui<-fluidPage(
  titlePanel("Regression"),
  sidebarPanel(
    selectInput("var",label="Choose the response variable:",choice=names(iris), selectize=FALSE),
    tags$hr(),
    checkboxGroupInput(inputId ="ind_var", label = "Select at least two independent variables:", choices = names(iris), selected = "Species"),

    mainPanel(
      plotOutput("plot"),
      verbatimTextOutput("summary")

    )
  )
)

server<-function(input,output){

  lmR <- reactive({
    req(input$var)
    req(input$ind_var)

    ## Option 1 ---------------------
    # formula = as.formula(paste(input$var, " ~ ", paste(input$ind_var, collapse= "+")))
    # stats::lm(formula=formula, data=iris)

    # browser()
    ## Option 2 ---------------------               
    stats::lm(formula=reformulate(input$ind_var, input$var), data=iris)
  })

  output$summary <- renderPrint(width="400", {
    lmR()
  })

  output$plot <- renderPlot({
    plot(predict(lmR()))    
  })

}

shinyApp(ui, server)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53871071

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档