簡體   English   中英

使用R Shiny中的動態輸入構建線性回歸

[英]Build Linear Regression with dynamic inputs in R Shiny

我正在嘗試使用線性回歸構建一個簡單的閃亮應用程序,允許用戶選擇lm()函數中使用的獨立變量和因變量,並最終繪制出幾個圖表。 我目前堅持將輸入傳遞給服務器端的lm()函數,並且能夠打印出回歸的摘要。 任何人都可以幫助我解決我的邏輯/代碼出錯的問題。 下面是代碼

library(shiny)
library(data.table)

RegData <- as.data.table(read.table("/home/r2uphp/ShinyApps/IRViews/RegData.tsv", header = TRUE, stringsAsFactors = FALSE))

ui <- fluidPage(
  headerPanel("Regression and Time Series Analysis"), 
  sidebarPanel(
    p("Select the inputs for the Dependent Variable"),
    selectInput(inputId = "DepVar", label = "Dependent Variables", multiple = FALSE, choices = list("AvgIR", "YYYYMM", "SumCount", "AvgLTV", "AvgGFEE", "AvgRTC", "Date")),
    p("Select the inputs for the Independent Variable"),
    selectInput(inputId = "IndVar", label = "Independent Variables", multiple = FALSE, choices = list( "SumCount", "AvgIR", "YYYYMM", "AvgLTV", "AvgGFEE", "AvgRTC", "Date"))
  ),
  mainPanel(
    verbatimTextOutput(outputId = "RegSum"),
    verbatimTextOutput(outputId = "IndPrint"),
    verbatimTextOutput(outputId = "DepPrint")
    #plotOutput("hist")
  )
)

server <- function(input, output) {

    lm1 <- reactive({lm(paste0(input$DepVar) ~ paste0(input$IndVar), data = RegData)})

    output$DepPrint <- renderPrint({input$DepVar})
    output$IndPrint <- renderPrint({input$IndVar})
    output$RegSum <- renderPrint({summary(lm1())})

}

shinyApp(ui = ui, server = server)

這是shinyapp的結果

這是我正在使用的示例數據集:

     YYYYMM      AvgIR SumCount    AvgLTV     AvgGFEE   AvgRTC       Date
 1: 2015-10 0.04106781   180029 0.7531805 0.002424778 319.6837 2015-10-01
 2: 2015-11 0.04036154   160061 0.7380383 0.002722529 312.6314 2015-11-01
 3: 2015-12 0.04001407   145560 0.7392874 0.002425912 313.0351 2015-12-01
 4: 2016-01 0.04034078   147693 0.7396932 0.002600640 315.0238 2016-01-01
 5: 2016-02 0.04055688   142545 0.7345160 0.002449523 310.3950 2016-02-01

提前致謝!

你只需要正確地建立你的公式。 我不確定你認為paste0作用,但這是一個更好的方法

lm1 <- reactive({lm(reformulate(input$IndVar, input$DepVar), data = RegData)})

reformulate()命令將為您構建正確的公式(請注意,自變量在函數中排在第一位。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM