MPG predictor Shiny App

Author: Ody Liagkas

Date: 2024/07/05

link: https://odyliagkas.shinyapps.io/MPG-predictor/

controls: on the app

Pitch:

This is an application that allows you to predict the MPG of a car that you might want to buy.

It uses 3 models:

  • MPG ~ Weight (in 1000 lbs)

  • MPG ~ Weight and HorsePower

  • MPG ~ Weight, HorsePower and Number of Cylinders

ui.R:

library(shiny)

shinyUI(fluidPage(
    
    titlePanel("Predicting MPG based on Weight, HorsePower, and Cylinders from mtcars dataset."),
    
    sidebarLayout(
        sidebarPanel(
            sliderInput("sliderWeight", "Weight in 1000 lbs:", 1.55, 5.33, value = 3.217),
            sliderInput("sliderHP", "Horsepower:", 52, 335, value = 150),
            checkboxInput("showModel1", "Show/Hide Model 1", value = TRUE),
            checkboxInput("showModel2", "Show/Hide Model 2", value = TRUE),
            checkboxInput("showModel3", "Show/Hide Model 3", value = TRUE),
            numericInput('cylinders', 'No. of Cylinders:', 6, min = 4, max = 8, step = 2),
            br(),
            h4("Controls:"),
            p("Sliders  –> The first slider is to set the weight of your car while the second to set the HP"),
            p("Checkboxes –> Each checkbox that is selected makes the respective line of each model appear or disappear on the graph"),
            p("Numeric Input –> You can ONLY put the numbers 4, 6, 8! If you put anything else it will crash.")
        ),
        mainPanel(
            plotOutput("mpgPlot"),
            verbatimTextOutput("pred1"),
            verbatimTextOutput("pred2"),
            verbatimTextOutput("pred3")
        )
    )
))

server.R:

library(shiny)

shinyServer(function(input, output) {
    
    model1 <- reactive({
        lm(mpg ~ wt, data = mtcars)
    })
    
    model2 <- reactive({
        lm(mpg ~ wt + hp, data = mtcars)
    })
    
    model3 <- reactive({
        lm(mpg ~ wt + hp + as.factor(cyl), data = mtcars)
    })
    
    model1pred <- reactive({
        weightInput <- input$sliderWeight
        predict(model1(), newdata = data.frame(wt = weightInput))
    })
    
    model2pred <- reactive({
        weightInput <- input$sliderWeight
        hpInput <- input$sliderHP  
        predict(model2(), newdata = data.frame(wt = weightInput, hp = hpInput))  
    })
    
    model3pred <- reactive({
        weightInput <- input$sliderWeight
        cylinderInput <- input$cylinders
        hpInput <- input$sliderHP  
        predict(model3(), newdata = data.frame(wt = weightInput, hp = hpInput, cyl = cylinderInput))  
    })
    
    output$mpgPlot <- renderPlot({
        plot(mtcars$wt, mtcars$mpg, xlab = "Weight in 1000 lbs", 
             ylab = "Miles Per Gallon", type = "n",
             xlim = c(min(mtcars$wt), max(mtcars$wt)),
             ylim = c(min(mtcars$mpg), max(mtcars$mpg)))
        
        wt_range <- seq(min(mtcars$wt), max(mtcars$wt), length.out = 100)
        
        if(input$showModel1){
            model1_line <- predict(model1(), newdata = data.frame(wt = wt_range))
            lines(wt_range, model1_line, col = "darkgreen", lwd = 2)
            points(input$sliderWeight, model1pred(), col = "darkgreen", pch = 19)
        }
        if(input$showModel2){
            hpInput <- input$sliderHP
            model2_line <- predict(model2(), newdata = data.frame(wt = wt_range, hp = hpInput))
            lines(wt_range, model2_line, col = "yellow", lwd = 2)
            points(input$sliderWeight, model2pred(), col = "yellow", pch = 19)
        }
        if(input$showModel3){
            hpInput <- input$sliderHP
            cylinderInput <- input$cylinders
            model3_line <- predict(model3(), newdata = data.frame(wt = wt_range, hp = hpInput, cyl = cylinderInput))
            lines(wt_range, model3_line, col = "red", lwd = 2)
            points(input$sliderWeight, model3pred(), col = "red", pch = 19)
        }
        
        legend("topright", legend = c("WT", "WT+HP", "WT+HP+CYL"), 
               col = c("darkgreen", "yellow", "red"), lwd = 2, bty = "n")
    })
    
    output$pred1 <- renderPrint({
        if(input$showModel1) {
            paste("Prediction for Model 1:", model1pred())
        }
    })
    
    output$pred2 <- renderPrint({
        if(input$showModel2) {
            paste("Prediction for Model 2:", model2pred())
        }
    })
    
    output$pred3 <- renderPrint({
        if(input$showModel3) {
            paste("Prediction for Model 3:", model3pred())
        }
    })
})

Results:

The results of this app show you what the predicted value of the MPG will be for your car based on each model you have selected to appear.

On the right, under the plot you can see the predicted value for the MPG, and you can see it change in real time while you change the parameters on the left.

The legend shows which line corresponds to which model and which variables each model takes into consideration while making its prediction