Coursera - Developing data products course project

Andrei Keino
2018/07/17

Presenting the “Predict Horsepower from MPG for data from mtcars dataset with either the linear regression model or polynomial regression model” Shiny application.

This Shiny application shows the data fitting of Horsepower (hp) vs Miles Per Gallon (mpg) from the mtcars dataset . Fitting performed with two models:

  • red color - linear regression model
  • blue color - polynomial regression model

You can choose the MPG value to predict HP by moving the “What is the MPG of the car?” slider. You can choose either show or not the the linear regression fit line and the polynomial fit line, You can also choose the fitting polynom power by the numeric input “Polynom power” The polynom power value can be any integer in range [2:15]

For more details on Predict Horsepower from MPG for data from mtcars dataset with either the linear regression model or polynomial regression model" Shiny application please visit https://andrei-keino.shinyapps.io/developing_data_products_course_project/

The code for the application:

Server.R file part 1.

library(shiny)

shinyServer(function(input, output) {
    mtcars$mpgsp <- ifelse(mtcars$mpg - 20 > 0, mtcars$mpg - 20, 0)
    # Fit Models
    model1 <- lm(hp ~ mpg, data = mtcars)
    fit_p <-  reactive({
        power <- input$polynom_power
        lm(hp ~ poly(mpg, power), data = mtcars)
    })

    model1pred <- reactive({
        mpgInput <- input$sliderMPG
        predict(model1, newdata = data.frame(mpg = mpgInput))
    })

    model2pred <- reactive({
        mpgInput <- input$sliderMPG
        predict(fit_p(), newdata = data.frame(mpg = mpgInput))
    })


    output$plot1 <- renderPlot({
        mpgInput <- input$sliderMPG

```

Server.R file part 2.

        plot(mtcars$mpg, mtcars$hp, xlab = "Miles Per Gallon", 
             ylab = "Horsepower", bty = "n", pch = 16,
             xlim = c(10, 35), ylim = c(50, 350))
        if(input$showModel1){
            abline(model1, col = "red", lwd = 2)
        }
        if(input$showModel2){
            x <- seq(min(mtcars$mpg), max(mtcars$mpg), length.out=200)
            y <- predict(fit_p(), newdata = data.frame(mpg = x))
            lines(x, y, col = "blue", lwd = 2)

            legend(25, 250, c("Linear regression prediction", "Polynomial regression prediction"), pch = 16, 
                   col = c("red", "blue"), bty = "n", cex = 1.2)
        }
        points(mpgInput, model1pred(), col = "red", pch = 16, cex = 2)
        points(mpgInput, model2pred(), col = "blue", pch = 16, cex = 2)

    })
    output$pred1 <- renderText({
        model1pred()
    })
    output$pred2 <- renderText({
        model2pred()
    })
}
)

ui.R file.

library(shiny)

shinyUI(fluidPage(
    titlePanel("Predict Horsepower from MPG for data from mtcars dataset with either the linear regression model \
                or polynomial regression model."),


    sidebarLayout(
        sidebarPanel(
            sliderInput("sliderMPG", "What is the MPG of the car?", 10, 35, value = 20),
            checkboxInput("showModel1", "Show/Hide Model 1", value = TRUE),
            checkboxInput("showModel2", "Show/Hide Model 2", value = TRUE),
            numericInput('polynom_power', 'Polynom power', 2, min = 2, max = 15)
        ),

        mainPanel(
            plotOutput("plot1"),
            h3("Predicted Horsepower from Model 1:"),
            textOutput("pred1"),
            h3("Predicted Horsepower from Model 2:"),
            textOutput("pred2"),
            helpText("This Shiny application shows the data fitting of Horsepower (hp) vs Miles Per Gallon (mpg) from the mtcars dataset .",
                     "Fitting performed with two models: one by red color - linear regression model, the blue one - polynomial regression model",
                     "You can choose the MPG value to predict HP by moving the \"What is the MPG of the car?\" slider.",
                     "You can choose either show or not the the linear regression fit line and the polynomial fit line, ",
                     "You can also choose the fitting polynom power by the numeric input \"Polynom power\" ",
                     "The polynom power value can be any integer in range [2:15]")
        )
    )
)
)

The example output for the application: page 1

model1 <- lm(hp ~ mpg, data = mtcars)
power <- 5
fit_p <- lm(hp ~ poly(mpg, power), data = mtcars)
mpgInput <- 25
model1pred <- predict(model1, newdata = data.frame(mpg=mpgInput))
model2pred <- predict(fit_p, newdata = data.frame(mpg=mpgInput))

The example output for the application: page 2

plot(mtcars$mpg, mtcars$hp, xlab = "Miles Per Gallon", ylab = "Horsepower", bty = "n", pch = 16, 
     xlim = c(10, 35), ylim = c(50, 350))
abline(model1, col = "red", lwd = 2)
points(mpgInput, model1pred, col = "red", pch = 16, cex = 2)
points(mpgInput, model2pred, col = "blue", pch = 16, cex = 2)
x <- seq(min(mtcars$mpg), max(mtcars$mpg), length.out=200)
y <- predict(fit_p, newdata = data.frame(mpg = x))
lines(x, y, col = "blue", lwd = 2)
legend(25, 250, c("Linear regression prediction", "Polynomial regression prediction"), pch = 16, 
       col = c("red", "blue"), bty = "n", cex = 1.2)

plot of chunk unnamed-chunk-5

print(paste0("Predicted Horsepower from Model 1: ", model1pred))
[1] "Predicted Horsepower from Model 1: 103.339039410815"
print(paste0("Predicted Horsepower from Model 2: ", model2pred))
[1] "Predicted Horsepower from Model 2: 82.1408395060124"