Course R package

Installing the course R package is straightforward.

install.packages("drat")
drat::addRepo("jr-packages")
install.packages("jrPredictive")

\noindent This R package contains copies of the practicals, solutions and data sets that we require. It will also automatically install any packages, that we use during the course. For example, we will need the caret, mlbench, pROC and splines to name a few. To load the course package, use

library("jrPredictive")

\noindent During this practical we will mainly use the caret package, we should load that package as well

library("caret")

The cars2010 data set

The cars2010 data set contains information about car models in $2010$. The aim is to model the FE variable which is a fuel economy measure based on $13$ predictors. Further information can be found in the help page, help("cars2010", package = "AppliedPredictiveModeling").

The data is part of the AppliedPredictiveModeling package and can be loaded by

data(FuelEconomy, package = "AppliedPredictiveModeling")
head(cars2010)

\noindent There are a lot of questions below marked out by bullet points. Don't worry if you can't finish them all, the intention is that there is material for different backgrounds and levels

Exploring the data

m1 = train(FE ~ EngDispl, method = "lm", data = cars2010)
rstd = rstandard(m1$finalModel)
plot(fitted.values(m1$finalModel), rstd)

\noindent We can add the lines showing where we expect the residuals to fall to aid graphical inspection

abline(h = c(-2, 0, 2), col = 2:3, lty = 2:1)
# There definitely appears to be some trend in the
# residuals.  The curved shape indicates that we
# potentially require some transformation of variables.
# A squared term might help.
plot(cars2010$FE, fitted.values(m1$finalModel), xlab = "FE",
    ylab = "Fitted values", xlim = c(10, 75), ylim = c(10,
        75))
abline(0, 1, col = 3, lty = 2)
# We seem to slightly over estimate more often than not
# in the 25-35 range. For the upper end of the range we
# seem to always under estimate the true values.
qqnorm(rstd)
qqline(rstd)
plot(cars2010$EngDispl, rstd)
abline(h = c(-2, 0, 2), col = 2:3, lty = 1:2)
# We are struggling to justify the assumption of
# normality in the residuals here, all of the diagnostics
# indicate patterns remain in the residuals that are
# currently unexplained by the model.

Extending the model

# We are struggling to justify the assumption of
# normality in the residuals here, all of the diagnostics
# indicate patterns remain in the residuals that are
# currently unexplained by the model
m2 = train(FE ~ poly(EngDispl, 2, raw = TRUE), data = cars2010,
    method = "lm")
# The residual diagnostics indicate a better fit now that
# the quadratic term has been included.
# Perhaps the residuals more closely match the assumption
# of normality under this transformation. However we need
# to be careful about interpretation now as the response
# is on the log scale. Likewise for prediction we need to
# remember to undo the transformation.
m3 = train(FE ~ EngDispl + NumCyl, data = cars2010, method = "lm")

Visualising the model

The jrPredictive package contains a plot3d function to help with viewing these surfaces in 3D.

## points = TRUE to also show the points
plot3d(m3, cars2010$EngDispl, cars2010$NumCyl, cars2010$FE,
    points = FALSE)

\noindent We can also examine just the data interactively, via

threejs::scatterplot3js(cars2010$EngDispl, cars2010$NumCyl,
    cars2010$FE, size = 0.5)
m4 = train(FE ~ EngDispl * NumCyl + I(NumCyl^5), data = cars2010,
    method = "lm")

How is prediction affected in each case? Don't forget to examine residuals, R squared values and the predictive surface.

Other data sets

A couple of other data sets that can be used to try fitting linear regression models. \begin{table}[!h] \centering \begin{tabular}{@{} lll @{}} \hline Data set & Package & Response \ \hline diamonds & ggplot2 & price \ Wage & ISLR & wage \ BostonHousing & mlbench & medv \ \hline \end{tabular} \end{table}



jr-packages/jrPredictive documentation built on Oct. 12, 2020, 11:44 a.m.