xyplot.lm()
: Linear model diagnosticstactile provides an xyplot()
method for lm
objects – usually generated with stats::lm()
or
stats::glm()
. It provides the same type of diagnostic plots
that stats::plot.lm()
covers, with some small modifications
This method is provided so that a user may use lattice graphics
throughout out a
document, say for a lab report, and produce consistent graphic
material that can be changed on a general level (for instance by using
lattice.options()
).
We fit a simple linear model
<- lm(Sepal.Length ~ Sepal.Width, data = iris) fit
Here, we fist use the default option and plot the diagnostics using
stats::plot()
par(mfrow = c(3, 2), cex = 1)
plot(fit, which = 1:6)
par(mfrow = c(1, 1))
Or instead use the method provided by tactile.
library(tactile)
xyplot(fit, which = 1:6)
You will see that the results are similar. The benefits, however, of
using xyplot.lm()
from tactile are that
lattice
are respected
(mostly), andpar(mfrow = c(2, 2))
. The
plots are arranged via gridExtra::grid.arrange()
automatically and arguments nrow
and ncol
have
been made accessible in the xyplot.lm()
call to enable
manual specifications.xyplot.Arima
: ARIMA Model DiagnosticsThis function is similar to xyplot.lm()
but is modeled
after stats::tsdiag()
. First, we look at the output from
the original.
<- arima(lh, c(0, 0, 1))
fit tsdiag(fit)
To use the method from tactile, we just call xyplot()
on
the model fit. The most prominent difference here is that we’ve added a
Q-Q plot of the standardized residuals as well, but also that we’re
correcting the Ljung–Box test to account for the fact that we’ve fit a
model (see
this).
xyplot(fit)
xyplot.forecast()
: Plotting forecasts with tactileRobert Hyndman’s excellent forecast package has built-in functions for plotting forecasts. These have been reworked to use lattice graphics in tactile, and also try to place the forecasts on the time scale of the original data (if such is provided).
For this example, we user USAccDeaths
, a time series
giving the monthly totals of accidental deaths in the USA. We begin by
separating the series into a training and test set.
library(forecast)
#> Registered S3 method overwritten by 'quantmod':
#> method from
#> as.zoo.data.frame zoo
<- window(USAccDeaths, c(1973, 1), c(1977, 12))
train <- window(USAccDeaths, c(1978, 1), c(1978, 12)) test
Then we fit the model and plot the results.
<- arima(train, order = c(0, 1, 1), seasonal = list(order = c(0, 1, 1)))
fit <- forecast(fit, 12)
fcast1 xyplot(fcast1, test,
grid = TRUE, auto.key = list(corner = c(0, 0.99)),
ci_key = list(title = "PI Level")
)
A so called fan plot can be achieved by increasing the number of
prediction intervals in the call to forecast
. We also
switch to a separate color palette by using the ci_pal
argument.
# A fan plot
<- forecast(fit, 12, level = seq(0, 95, 10))
fcast2 xyplot(fcast2, test, ci_pal = heat.colors(100))