Course Project for Developing Data Products

hmchang17
Sun Sep 14 01:10:37 2014

Predicting the value of mpg for cars

In this project, I decided to implement the course project of the other course of Data Science Specialization Regression Models into a shiny app. In this project, we will use the data set mtcars to build a regression model for predicting mpg (miles per gallon) of the user's car.

Outline of this pitch

  • server.R
  • ui.R
  • Output

Computation

After loading the mtcars data set, we build the regression model for mpg with three influential independent variables, including am, qsec, wt.

bestfit <- lm(mpg ~ am + qsec + wt, data = mtcars)
predictMpg <- function(am,wt,qsec){
  round(predict(bestfit, list(am=as.factor(am), wt=wt, qsec=qsec )),digits = 3)}

server.R

Next, assign the input values to interactive part of the code using the shiny syntax. I decided to deliver textual and graphical outcome. So the user will get the predicted value of mpg of his/her car. In order to make this result look more custimized, the user is required to enter the model of his/her car, such as Toyota Yaris.

output$mpg <- renderText(paste("...", {input$model},"...",{predictMpg(input$am,input$wt,input$qsec)}))

output$mpgPlot <- renderPlot({
    qplot(y=mpg, x=qsec/wt, ...
    geom_point(...,colour="purple", size=5)
})

ui.R

There are four input values in server.R, hense ui.R need to declare four input options. The output widgets are put in the main panel. Note that the id of the input and output widgets should be identical to server.R. Then add proper introductions and label names. The shiny app is up and running.

sidebarPanel(
  textInput(inputId="model", ...), 
  sliderInput('wt',...), 
  sliderInput('qsec',...),
  radioButtons(inputId = "am", choices=list(
    "Automatic"="automatic", 
    "Manual"="manual"))
mainPanel(verbatimTextOutput("mpg"), plotOutput('mpgPlot'))