Interactive Shiny App for Creating Linear Regression Models

SZB
02/07/2021

Purpose and Structure of App

The purpose of this Shiny app is to allow the user to easily experiment with predictor selection for a linear regression model. This app is easier for users than, for example, repeating the lm function in the console with different predictors. The mtcars dataset is chosen and mpg is selected to be the outcome/response.

The app contains 2 tabs:

  • The Documentation tab:

    This tab contains an explanation of how to use the app.

  • The Model tab:

    This tab contains a residual plot for mpg based on a linear model that includes the selected predictors. If no predictors are chosen, the linear model is a simple intercept-only model. There are no interaction terms between any of the selected predictors. The coefficients for each selected predictor are shown, including the p-values as an indicator of whether the coefficient terms are statistically significant. Lastly, the model R2 and MSE values are shown.

Example using the App - Residual Plot

Assume the following predictors have been selected: cyl, disp and hp:

predVar <- data.frame(Var = c("cyl","disp","hp","drat","wt","qsec","vs","am",
            "gear","carb"), Log=rep(TRUE,10))
indSelected <- c(1,2,3)
data(mtcars)
mtcars$cyl <- factor(mtcars$cyl)
mtcars$gear <- factor(mtcars$gear)
mtcars$carb <- factor(mtcars$carb)
if(sum(indSelected)==0){
      lmModel <- lm("mpg ~ 1",data=mtcars)
      } else {
      lmModel <- lm(paste(c("mpg ~ 1",predVar$Var[indSelected]),sep="+",
                          collapse="+"),data=mtcars)
      }
res <- resid(lmModel)
plot(mtcars$mpg, res, ylab="Residuals", xlab="mpg", main="Residuals for mtcars mpg") 
abline(h=0,col="red")   

plot of chunk unnamed-chunk-2

Example using the App - Predictor Coefficients

predVar <- data.frame(Var = c("cyl","disp","hp","drat","wt","qsec","vs","am",
            "gear","carb"), Log=rep(TRUE,10))
indSelected <- c(1,2,3)
data(mtcars)
mtcars$cyl <- factor(mtcars$cyl)
mtcars$gear <- factor(mtcars$gear)
mtcars$carb <- factor(mtcars$carb)
if(sum(indSelected)==0){
      lmModel <- lm("mpg ~ 1",data=mtcars)
      } else {
      lmModel <- lm(paste(c("mpg ~ 1",predVar$Var[indSelected]),sep="+",
                          collapse="+"),data=mtcars)
      }
modelSummary <- summary(lmModel)$coefficients
modelSummary
               Estimate Std. Error    t value     Pr(>|t|)
(Intercept) 31.14773045 1.76712089 17.6262589 2.440860e-16
cyl6        -4.04719095 1.68944337 -2.3955766 2.379338e-02
cyl8        -2.43192602 3.23978314 -0.7506447 4.593589e-01
disp        -0.02603750 0.01042104 -2.4985504 1.885645e-02
hp          -0.02113604 0.01418844 -1.4896660 1.479018e-01

Example using the App - R-squared and MSE

predVar <- data.frame(Var = c("cyl","disp","hp","drat","wt","qsec","vs","am",
            "gear","carb"), Log=rep(TRUE,10))
indSelected <- c(1,2,3)
data(mtcars)
mtcars$cyl <- factor(mtcars$cyl)
mtcars$gear <- factor(mtcars$gear)
mtcars$carb <- factor(mtcars$carb)
if(sum(indSelected)==0){
      lmModel <- lm("mpg ~ 1",data=mtcars)
      } else {
      lmModel <- lm(paste(c("mpg ~ 1",predVar$Var[indSelected]),sep="+",
                          collapse="+"),data=mtcars)
      }
roundR2 <- round(summary(lmModel)$r.squared,digit=3)
R2 <- paste("Model R^2 is: ",roundR2)
roundMSE <- round(mean(lmModel$residuals^2),digit=3)
MSE <- paste("Model MSE is: ",roundMSE)
R2
[1] "Model R^2 is:  0.8"
MSE
[1] "Model MSE is:  7.035"