Shiny App: Stopping Distance of Cars

Padma
26th December, 2019

Introduction

  • In this assignment we use “cars” dataset.
  • This dataset contains data about different car speeds and the corresponding stopping distances
  • In this assignment we apply linear model to the cars data
  • We then allow user to select a speed
  • Based on the speed we predict the stopping distance

User Interface Code

  • In the User Interface code, we allow the user to select speed through sliderinput
sliderInput("speed",
            "Enter Speed of car:",
            min = 4,
            max = 25,
            value = 15)

Server Code

  • In the server code, we apply linear model to the cars data
  • We then use the speed given by the user and predict the stopping distance based on the LM
data("cars", package = "datasets")
linearmodel <- lm(dist ~ cars$speed, data = cars)
linearmodel

Call:
lm(formula = dist ~ cars$speed, data = cars)

Coefficients:
(Intercept)   cars$speed  
    -17.579        3.932  

Reactive Output in Main Panel

  • In the Main panel we display the estimated stopping distance
  • We also plot the cars data ( stopping distance vs speed)
  • We highlight the points that correspond to the user given speed.
  • We then show the regression line based on linear model
  • The estimated stopping distance is actually the value derived from this regression line
coef_intercept <- coef(linearmodel)[1]
coef_speed <- coef(linearmodel)[2]
output$distance <- renderText({
  coef_intercept+ coef_speed*input$speed
})