Linear model generator for the mtcars dataset

Gil Huesca

9/28/2020

Introduction

This presentation introduce you to the linear model generator for the mtcars data set. The data was extracted from the 1974 Motor Trend US magazine, and comprises fuel consumption and 10 aspects of automobile design and performance for 32 automobiles (1973–74 models). It has 32 observations on 11 (numeric) variables.

(from the dataset description)

Instructions

You find the app in https://gilhuesca.shinyapps.io/GilShinyFirstAppDDPCourse/.

These are the steps to use the application

  1. You find the variables of the mtcars dataset in the side bar panel.
  2. The first set is for you to set the dependent variable.
  3. The second set is for you to set the predictor.
  4. After you choose both variables (y and x in your model), some calculations will be displayed in the main panel.
  5. First, a linear model is generated and the formula used is displayed.
  6. Then, the coefficients of the model are displayed
  7. Finally, a plot with the two variables and the line representing the model is displayed.

Note: If both variables are the same, no model is generated and no plot is displayed.

Example

If you choose mpg as dependent variable and disp as predictor you will get the following output.

Lineal model for: mpg~disp

  yy <- mtcars[,which(names(mtcars)=="mpg")]
  xx <- mtcars[,which(names(mtcars)=="disp")]
  lm <- lm(mpg~disp,data = mtcars)
  paste("Intercept: ",lm$coefficients[1]," *** ","disp",": ",lm$coefficients[2],sep="")
## [1] "Intercept: 29.5998547561639 *** disp: -0.0412151199627861"

Plot

  plot(xx,yy)
  lines(xx,fitted(lm),col="green")