Regression Analysis with an interactive Shiny app

Raul Moya
2023-06-21

First Slide

Shiny app developed for the assignment of course Developing Data Products of Johns Hopkins University

Aimed to help on solving the question if an automatic or manual transmission is better for fuel consumption, techniques of regression analysis are solved.

  • Linear regressor with lm() function, where variables are dynamically added
  • Correlation anaysis between the variables with cor() function in a nXn matrix
  • The weighted residuals, the usual residuals rescaled by the square root of the weights specified in the call to lm.

The user of the app can instantly verify the effect of adding additional variables to the analysis.

The code is available in https://github.com/rmmoya/regression_analysis_shinyapp

Some of the code in server.R

The variables to be added to the linear regressor are dynamically added from the selected choice with the following code:

if(is.null(input$variable)){
      fit <- lm(mpg ~ factor(am), mtcars)
    }
    else{
      fit <- lm(as.formula(paste("mpg ~ factor(am) + ", paste(input$variable, collapse = "+"))), mtcars)
    }

Correlation analysis in a matrix form that can be visualized in the app:

  mtcars %>% 
      select('mpg', 'am', 'hp', 'wt', 'cyl') %>%
      pairs(upper.panel = panel.cor, lower.panel = panel.smooth, diag.panel = panel.hist, gap = 1/4,
            main = "Correlation analysis to discard dependent variables")

Residual analysis

A residual analysis is plotted with the selected variables:

fit <- lm(as.formula(paste(“mpg ~ factor(am) + ”, paste(input$variable, collapse = “+”))), mtcars)

plot of chunk unnamed-chunk-3

Correlation Analysis

This graphical representation of the correlation between all the selected variables and mpg and am is intended to help on the identification on the most relevant relationships to add to the linear regressor for the multivariate analysis.

plot of chunk unnamed-chunk-4

Table

The user can browse the data used in the analysis in one separated tab:

mtcars[, c(“mpg”, “am”, input$variable), drop = FALSE]

                     mpg am cyl    wt  hp
Mazda RX4           21.0  1   6 2.620 110
Mazda RX4 Wag       21.0  1   6 2.875 110
Datsun 710          22.8  1   4 2.320  93
Hornet 4 Drive      21.4  0   6 3.215 110
Hornet Sportabout   18.7  0   8 3.440 175
Valiant             18.1  0   6 3.460 105
Duster 360          14.3  0   8 3.570 245
Merc 240D           24.4  0   4 3.190  62
Merc 230            22.8  0   4 3.150  95
Merc 280            19.2  0   6 3.440 123
Merc 280C           17.8  0   6 3.440 123
Merc 450SE          16.4  0   8 4.070 180
Merc 450SL          17.3  0   8 3.730 180
Merc 450SLC         15.2  0   8 3.780 180
Cadillac Fleetwood  10.4  0   8 5.250 205
Lincoln Continental 10.4  0   8 5.424 215
Chrysler Imperial   14.7  0   8 5.345 230
Fiat 128            32.4  1   4 2.200  66
Honda Civic         30.4  1   4 1.615  52
Toyota Corolla      33.9  1   4 1.835  65
Toyota Corona       21.5  0   4 2.465  97
Dodge Challenger    15.5  0   8 3.520 150
AMC Javelin         15.2  0   8 3.435 150
Camaro Z28          13.3  0   8 3.840 245
Pontiac Firebird    19.2  0   8 3.845 175
Fiat X1-9           27.3  1   4 1.935  66
Porsche 914-2       26.0  1   4 2.140  91
Lotus Europa        30.4  1   4 1.513 113
Ford Pantera L      15.8  1   8 3.170 264
Ferrari Dino        19.7  1   6 2.770 175
Maserati Bora       15.0  1   8 3.570 335
Volvo 142E          21.4  1   4 2.780 109