25/08/2020

Introduction

This App allows you to explore the mtcars data set.

The member can select the variable that they with to model, the dependent variables they wish to be included in multi-variate regression and the dependent variable they wish for the independent variable to be plotted against.

The App then produces a summary of the model coefficients in table.

The data

The table below shows the data used by the APP

head(mtcars)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

The User interface

The UI code was as follows

library(shiny)

dat <- mtcars

shinyUI(pageWithSidebar(
    headerPanel("Multivariate regression on the motor cars data set"),
    
    sidebarPanel(
        selectInput("dependent", "Dependent Variable:", names(dat)),
        uiOutput("independent")
    ),
    
    mainPanel(tableOutput("regTab"))
))

The server

The server code was as follows

library(shiny)

dat <- mtcars
library(ggplot2)

shinyServer(function(input, output, session) {
    
    output$independent <- renderUI({
        checkboxGroupInput("independent", "Independent Variables:",names(dat)[!names(dat) %in% input$dependent],names(dat)[!names(dat) %in% input$dependent])
    })
    
    runRegression <- reactive({
        lm(as.formula(paste(input$dependent," ~ ",paste(input$independent,collapse="+"))),data=dat)
    })

The server continued

    output$regTab <- renderTable({
        if(!is.null(input$independent)){
            summary(runRegression())$coefficients
        } else {
            print(data.frame(Warning="Please select Model Parameters."))
        }
    }, include.rownames=TRUE)
})

An example of the output

Suppose a member selected the following:

  • Dependent variable: hp
  • Independent variables: mpg, cyl

It would produce the following table:

##              Estimate Std. Error    t value    Pr(>|t|)
## (Intercept) 54.066600  86.093062  0.6280018 0.534917390
## mpg         -2.774769   2.176772 -1.2747177 0.212528465
## cyl         23.978626   7.345949  3.2641973 0.002814958