November 20, 2016

Objectives

The objective of this project is to use the data in the 'cars' package available in R to make predictions about the stopping distance of a moving car given its speed. Complete documentation for this presentation and the application can be found at "https://github.com/zcolburn/CourseraShinyApplication".

Summary

Briefly, the user selects a speed using the slider. This triggers the application to fit the 'cars' data to a linear model through the origin (using the function 'lm'). In this model, the stopping distance is the dependent variable. Using the user input (indicated by the vertical green line), the predicted stopping distance is calculated. This value is output as text beneath the graph.

Example plot

An example plot produced by the application is reproduced below.

Example prediction

An example prediction is shown below. Assuming the user selected the example speed '25', the application will run the following code and predict the stopping distance.

exampleSpeed <- 25
# Fit a linear model that passes through the origin.
fit <- lm(dist ~ 0 + speed, data = cars)
# Predict the stopping distance.
prediction <- predict(fit, data.frame("speed"=exampleSpeed))
# Output text.
text <- paste("The predicted stopping distance for a car 
              moving ", exampleSpeed, " mph is ", 
              round(prediction, 0), " feet.")
cat(text)
## The predicted stopping distance for a car 
##               moving  25  mph is  73  feet.