Predict fitted values from OLS multivariate or Machine Learning

Hello !

i would like to do from shiny prediction from a mutivariate ols model lm(var3 ~ var2 + var1) by plugging numerical values from the fitted model (var2_hat, var1_hat) to get prediced forecast var3_hat. I did several tests but could not reach my goal. If needed i can provide an example but if any one has a pedagogical example reproducible code to learn that would be great. I will then extend it to machine learning examples and share it. Thank you and have a nice end of year !

Cheers,

result <-lm(var3~var2+var1)
newData <- data.frame(var2 = 17, var3 = 42)
predict(result, newData)

Hello

Thank you very much !
Of course it works. I was insert the code lines in a wider program where i insert input (input$var2, input$var1) to get output from predict function in an eventreactive function but got simply no responses nor error messages.
I ll try again tomorrow and let you know if possible except if you have some pedagogical code lines like yesterday. Many thanks !

Hello !

Here is my code. I'm not satisfy :
*ols: whatever the nb of explanatory var i choose, only the first one is selected + less importantly i would like the choice to determine which is Y which is X here and for prediction
*predict: predict returns more than 1 value for dep variable while newdata() returns correctly only one + the prediction is not refreshed when i modify ind variables + less importantly: i want to plug a figure not to select
Many thanks for any help !

HERE BELOW IS MY CODE:

set.seed(123)

var1 <- sample(1:15, 50, replace = TRUE)
var2 <- sample(16:30, 50, replace = TRUE)
var3 <- sample(31:45, 50, replace = TRUE)
df<- data.frame( var1 ,var2 ,var3)

ui= fluidPage(

titlePanel(title = h4("SUPERMARCO NEEDS HELP")),
sidebarLayout(
sidebarPanel(
selectInput('varY', 'Select an Dependent Variable', names(df)),
selectInput('varX', 'Select an Explanatory Variable', names(df), multiple=TRUE),
actionButton(inputId = "go1",label="Plot"),
actionButton(inputId = "go2",label="Results"),

selectizeInput('VAR2', 'Select X2 TO FORECAST Y',selected = "20", choices = (df$var2), multiple=FALSE),
selectizeInput('VAR3', 'Insert a value for X3 TO FORECAST Y', selected = "40", choices = (df$var3), multiple=FALSE),
#numericInput("VAR3", "Select X3 TO FORECAST Y", 40),
actionButton("updateF", "FORECAST")

		),




mainPanel("main panel", plotOutput("rplot"), verbatimTextOutput("summary"), verbatimTextOutput("x_updated"))

))
server = function(input, output,session) {

lm_fit <- reactive({ lm(as.formula(paste0(input$varY ,"~", input$varX)), data=df) })

summary_stats <- eventReactive(input$go2,{ summary(lm_fit()) })

rplot<- eventReactive(input$go1, { plot(lm(as.formula(paste0(input$varY ,"~", input$varX)), data=df))
})

output$rplot <- renderPlot({ rplot() })
output$summary <- renderPrint({ summary_stats() })

result <- lm(var1 ~ var2 + var3 , data = df)
newdata <- eventReactive( input$updateF, {data.frame(input$VAR2 ,input$VAR3)})
output$x_updated <- renderPrint({ predict(result, newdata()) })

}

shinyApp(ui,server)

Hello, i think my question was too long :wink:
Here is below my problem since 3 days: how to make prediction reactive. I provide two examples that do no work. Thanks !

OR

result <- lm(var1 ~ var2 + var3 , data = df)
newdata <- reactive({ data.frame( input$VAR2 , input$VAR3) })
pred <- reactive({ predict(result,newdata()) })
observeEvent(input$updateF, { output$Pred <- renderText({ pred() }) })

As a guess, you don't want the parentheses after newData.

Thanks a lot for response.
Let me see and tell you. The thing is when I do it on R it works with figures but when I do it on shiny in a reactive environment with button and figures (new data) to enter, either I got nothing or an error message telling me even if new data has one row the object returns the same number rows of of the data set.
Cheers

I believe that's what's going on is that the extra parentheses tell R to look for a function...which doesn't exist. Since it can't find the new data it's using the existing data.