首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于反应变量的线性模型在with中的预测

基于反应变量的线性模型在with中的预测
EN

Stack Overflow用户
提问于 2016-08-10 21:33:38
回答 1查看 1.8K关注 0票数 4

我想请求一些方法来预测在R中的lm (线性模型),它接受反应变量。

如果你有一个带有y和x的线性模型"lm“,可以对新的数据进行”预测“,给出x的新值。

在下面的工作示例中,我为lm创建了一些(任意的,仅仅是为了使其工作)的反应性y和x值,还创建了一个输入来给出一个不断变化的新值(用作新的x)。

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

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-10 22:10:34

当使用lm()predict时,最好有一个正确命名的data.frame,并使用适当的公式。如果你换了这些零件

代码语言:javascript
复制
results <- reactive({ 
  r <- data.frame(y=y(),x=x())
})

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

代码语言:javascript
复制
newdata <- reactive ({  data.frame(x=x() ) } )

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

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

https://stackoverflow.com/questions/38883938

复制
相关文章

相似问题

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