如何基于闪亮的反应变量线性模型进行预测
how to predict based on linear models with reactive variables in shiny
我想请求一些方法来预测 R 中的 lm(线性模型),它接受反应变量。
如果您有一个包含 y 和 x 的线性模型 "lm",则可以为新数据完成 "predict",为 x 提供新值。我想在闪亮的应用程序中对反应性 y 和 x 执行此操作
在下面的工作示例中,我以某种方式(任意地,只是为了使其工作)为 lm 创建了反应性 y 和 x 值,以及一个输入以提供不断变化的新值(用作新 x)。
objective 是根据 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 中的 lm(线性模型),它接受反应变量。
如果您有一个包含 y 和 x 的线性模型 "lm",则可以为新数据完成 "predict",为 x 提供新值。我想在闪亮的应用程序中对反应性 y 和 x 执行此操作
在下面的工作示例中,我以某种方式(任意地,只是为了使其工作)为 lm 创建了反应性 y 和 x 值,以及一个输入以提供不断变化的新值(用作新 x)。
objective 是根据 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