Regresión en la aplicación shinyApp con selección dinámica de variables.
Quiero realizar regresiones lineales de la variable Feature_A y quiero que el usuario seleccione dinámicamente la otra variable. También quiero mostrar estadísticas sobre el ajuste general de mi modelo predictivo, R2 ajustado, cada coeficiente estimado del modelo y los p-valores de los coeficientes.
A continuación está lo que pude descubrir. No hace falta decir que no funciona. He estado luchando con esto y cualquier ayuda será muy apreciada.
library(shiny)
library(ggplot2)
library(dplyr)
library(purrr)
Feature_A <- c(1, 2, 1, 4, 2)
Feature_B <- c(4, 5, 6, 6, 6)
Feature_C <- c(22, 4, 3, 1, 5)
df <- data.frame(Feature_A, Feature_B, Feature_C)
ui <- fluidPage(
titlePanel(title = h4("Regression")),
sidebarLayout(
sidebarPanel(
selectInput('ip', 'Select an Explanatory Variable', names(df)),
actionButton(inputId = "btn1", label = "Regression Plot"),
actionButton(inputId = "btn2", label = "Show Stats")
),
mainPanel(
"main panel",
regOutput("regplot"),
verbatimTextOutput("summary")
)
)
)
server <- function(input, output, session) {
lm_fit <- lm(Feature_A ~ input$ip, data = df)
summary_stats <- eventReactive(input$btn2, {
summary(lm_fit)
})
regression_plot <- eventReactive(input$btn1, {
ggplot(data = df, aes(x = input$ip, y = Feature_A)) +
geom_point(color = 'blue') +
geom_smooth(method = "lm", se = FALSE)
})
output$regplot <- renderPlot({
regression_plot()
})
output$summary <- renderPrint({
summary_stats()
})
}
shinyApp(ui, server)
davy.ai
Para realizar regresiones lineales de Feature_A y permitir al usuario seleccionar la otra variable de forma dinámica, podemos crear un
selectInput
en elsidebarPanel
de la interfaz de Shiny y utilizar la variable seleccionada como entrada para el código de regresión en la función del servidor. Para mostrar las estadísticas sobre el ajuste del modelo predictivo, podemos utilizarsummary()
en el objetolm
y mostrarlo utilizandorenderPrint()
en la salida de Shiny.Aquí tienes un código actualizado que debería funcionar:
Ten en cuenta que utilizamos
paste0(input$input_var)
para obtener el nombre de la columna de forma dinámica a partir de la variable seleccionada enselectInput
. Además, creamos dos funciones reactivasregression_plot
ysummary_stats
que se activan mediante los botones de acción en el panel lateral. Por último, utilizamosrenderPlot
yrenderPrint
para mostrar la salida de estas funciones en la interfaz de Shiny.