Skip to content

Instantly share code, notes, and snippets.

@resulumit
Last active November 6, 2022 15:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save resulumit/fdf09ee63a4599e21b7a8011cade7c04 to your computer and use it in GitHub Desktop.
Save resulumit/fdf09ee63a4599e21b7a8011cade7c04 to your computer and use it in GitHub Desktop.
A app to regress and visualise, using the mtcars dataset
library(dotwhisker)
library(tidyverse)
shinyApp(
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
sliderInput(inputId = "carb", label = "carb", step = 1, ticks = FALSE,
value = c(min(mtcars$carb, na.rm = TRUE),
max(mtcars$carb, na.rm = TRUE)),
min = min(mtcars$carb, na.rm = TRUE),
max = max(mtcars$carb, na.rm = TRUE)),
sliderInput(inputId = "cyl", label = "cyl", step = 1, ticks = FALSE,
value = c(min(mtcars$cyl, na.rm = TRUE),
max(mtcars$cyl, na.rm = TRUE)),
min = min(mtcars$cyl, na.rm = TRUE),
max = max(mtcars$cyl, na.rm = TRUE)),
sliderInput(inputId = "disp", label = "disp", step = 1, ticks = FALSE,
value = c(min(mtcars$disp, na.rm = TRUE),
max(mtcars$disp, na.rm = TRUE)),
min = min(mtcars$disp, na.rm = TRUE),
max = max(mtcars$disp, na.rm = TRUE)),
sliderInput(inputId = "drat", label = "drat", step = 1, ticks = FALSE,
value = c(min(mtcars$drat, na.rm = TRUE),
max(mtcars$drat, na.rm = TRUE)),
min = min(mtcars$drat, na.rm = TRUE),
max = max(mtcars$drat, na.rm = TRUE)),
sliderInput(inputId = "gear", label = "gear", step = 1, ticks = FALSE,
value = c(min(mtcars$gear, na.rm = TRUE),
max(mtcars$gear, na.rm = TRUE)),
min = min(mtcars$gear, na.rm = TRUE),
max = max(mtcars$gear, na.rm = TRUE)),
sliderInput(inputId = "hp", label = "hp", step = 1, ticks = FALSE,
value = c(min(mtcars$hp, na.rm = TRUE),
max(mtcars$hp, na.rm = TRUE)),
min = min(mtcars$hp, na.rm = TRUE),
max = max(mtcars$hp, na.rm = TRUE)),
sliderInput(inputId = "qsec", label = "qsec", step = 1, ticks = FALSE,
value = c(min(mtcars$qsec, na.rm = TRUE),
max(mtcars$qsec, na.rm = TRUE)),
min = min(mtcars$qsec, na.rm = TRUE),
max = max(mtcars$qsec, na.rm = TRUE)),
sliderInput(inputId = "wt", label = "wt", step = 1, ticks = FALSE,
value = c(min(mtcars$wt, na.rm = TRUE),
max(mtcars$wt, na.rm = TRUE)),
min = min(mtcars$wt, na.rm = TRUE),
max = max(mtcars$wt, na.rm = TRUE))
),
mainPanel(plotOutput("regressionPlot"))
)
),
server = function(input, output) {
output$regressionPlot = renderPlot({
# filter the dataset with input from sliders
df <- mtcars %>%
filter(carb >= input$carb[1] & carb <= input$carb[2],
cyl >= input$cyl[1] & cyl <= input$cyl[2],
disp >= input$disp[1] & disp <= input$disp[2],
drat >= input$drat[1] & drat <= input$drat[2],
gear >= input$gear[1] & gear <= input$gear[2],
hp >= input$hp[1] & hp <= input$hp[2],
qsec >= input$qsec[1] & qsec <= input$qsec[2],
wt >= input$wt[1] & wt <= input$wt[2]
)
# run the regression model
model <- lm(mpg ~ carb + cyl + disp + drat + gear + hp + qsec + wt,
data = df)
# plot the results
dwplot(model, dot_args = list(size = 3.2), whisker_args = list(size = 1.5)) +
theme_bw() +
theme(legend.position = "none",
axis.text=element_text(size = 14),
axis.title=element_text(size = 14)) +
geom_vline(xintercept = 0, colour = "grey60", linetype = 2) +
ggtitle("Predicting Gas Mileage") +
xlab("\nCoefficient")
})
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment