r - 如何基于 Shiny 的 react 变量的线性模型进行预测

标签 r shiny reactive-programming linear-regression predict

我想请求一些在 R 中使用 lm(线性模型)进行预测的方法,它接受 react 变量。
如果您有一个包含 y 和 x 的线性模型“lm”,则可以对新数据进行“预测”,给出 x 的新值。我想在 Shiny 的应用程序中对响应式(Reactive) y 和 x 执行此操作

在下面的工作示例中,我以某种方式(任意地,只是为了使其工作)为 lm 创建了 react 性 y 和 x 值,以及一个输入来给出不断变化的新值(用作新的 x)。

目标是考虑 y(), x() 正确获得新(输入)x 的预测 y 。

library(shiny)
library(EnvStats)
ui <- fluidPage ( 
  sidebarLayout(
  sidebarPanel (
  numericInput('variable1', 'new x', 0.1, min = 0, max = 100, step = 0.1)
  ),
  mainPanel (plotOutput('plot1') )
  )
  )
server <- function(input, output){
#  Initial data and linear regression that should be reactive,
# the dependency on input$variable1<1 is just an example to work with a lm based on reactive data. 
  y<- reactive (
    if (input$variable1<1)
    { y <- c(3.1, 3.25, 3.5, 4, 3.5, 5, 5.5) }
    else 
     { y <- c(.1, .25, .5, 1, 1.5, 2, 2.5) }
  )
  x<- reactive (
    if (input$variable1>=1)
    { x <- c(.1, .332, .631, .972, 1.201, 1.584, 1.912) }
    else 
    { x <- c(.1, .3,  .631, .972, 2.201, 2.584, 2.912) }
  )

output$plot1 <- renderPlot({
  # UNCORRECTED INITIAL VERSION some reactive functions are unnecessary 
  #  results <- reactive({ 
  #    r <- data.frame(y(),x())
  #  })
  #  lmod <- reactive ({ 
  #    mod1 <- lm(y()~ x(), data = results() 
  #    )
  #   x <-reactive ({ x <- input$variable1  })
  #  newdata <- reactive ({  data.frame(x() ) } )
  #  newdata.pred <- reactive ({  predict(lmod(),newdata(),level=1)
  #  })
  #  segments(input$variable1, 0, input$variable1, newdata.pred(), col = "red")
  # CORRECTED AFTER MRFLICK
    plot(x(),y())
    results <- data.frame(y=y(),x=x()) # reactive is not necessary because 
    lmod <- lm(y~x, data = results)    #of the reactive context (renderPlot)
    abline(lmod)  
    x <- input$variable1 
    newdata <- data.frame(x=x ) 
    newdata.pred2 <- predict(lmod,newdata,se.fit=TRUE)
    ci<- pointwise(newdata.pred2, coverage = 0.95, simultaneous = TRUE)

    newdata.pred <- predict(lmod,newdata)

        segments(input$variable1, 0, input$variable1, newdata.pred, col = "red")
        points(input$variable1, ci$lower, col = "magenta") 
        points(input$variable1, ci$upper, col = "magenta")
        text(input$variable1, newdata.pred, labels=paste("predicted",signif(newdata.pred, 3) ), pos =4, cex = 1.2)
        text(input$variable1, ci$upper, labels=paste("upper limit of pointwise confidence intervals",
                                                                     signif(ci$upper, 3) ), pos =4, cex = 1.2)
        text(input$variable1, ci$lower, labels=paste("lower limit of pointwise confidence intervals",
                                                        signif(ci$lower, 3) ), pos =4, cex = 1.2)
} )
} # end server
  shinyApp(ui, server)

最佳答案

最好使用 lm()predict 获得正确命名的 data.frame 并使用正确的公式。如果你改变这些部分

results <- reactive({ 
  r <- data.frame(y=y(),x=x())
})

lmod <- reactive ({ 
  mod1 <- lm(y~x, data = results() )
 })

newdata <- reactive ({  data.frame(x=x() ) } )

我想你会得到你想要的行为。现在模型拟合 data.frame 和预测 data.frame 都有一个名为 x 的列,并且 lm() 中使用的公式清楚地列出了 x 作为用于预测 y

的变量

关于r - 如何基于 Shiny 的 react 变量的线性模型进行预测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38883938/

相关文章:

r - geom_violin() - 图形边框以一种奇怪的方式弯曲

r - 如何在 RStudio 中运行单行

R-日期时间变量在 ifelse 后丢失格式

R Markdown 文件没有给我输出

html - 删除 Shiny 应用程序标题中的 div

javascript - Rxjs - 当缓存为空时消费 API 输出并重新查询

r - Shiny /传单 map 未呈现

javascript - 将 Server.R 中的值访问到 .js 文件中

java - 使 RxJava 运算符链并发

android - RxJava - 如何在另一个流等待第一个项目时缓冲流中的所有项目?