From 4aadd64c6872089261bab84af40d73d087e79369 Mon Sep 17 00:00:00 2001 From: robjhyndman Date: Tue, 25 Oct 2016 11:12:23 +0200 Subject: [PATCH] Added checkresiduals() function --- ChangeLog | 1 + NAMESPACE | 3 ++- R/checkresiduals.R | 38 ++++++++++++++++++++++++++++++++++++++ man/checkresiduals.Rd | 22 ++++++++++++++++++++++ 4 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 R/checkresiduals.R create mode 100644 man/checkresiduals.Rd diff --git a/ChangeLog b/ChangeLog index e3b86263..9246ceb3 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,5 @@ Version 7.4 (?? 2016) + * Added gghistogram() and checkresiduals() * Bug fixes Version 7.3 (12 October 2016) diff --git a/NAMESPACE b/NAMESPACE index 87c93154..5af89ad6 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -37,7 +37,8 @@ ggtsdisplay, ggmonthplot, ggseasonplot, seasonplot, ses, sindexf, splinef, tslm, fourierf,forecast.lm,thetaf, tsdisplay, simulate.ets, simulate.ar, simulate.Arima, simulate.fracdiff, simulate.nnetar, ndiffs, nsdiffs, dm.test,tbats.components, Acf,Pacf,Ccf,ma,CV,BoxCox.lambda,dshw, bats, tbats, msts, getResponse, tsoutliers, tsclean, bizdays, easter,stlm, forecast.stlm, -StatForecast, GeomForecast, geom_forecast, gghistogram) +StatForecast, GeomForecast, geom_forecast, gghistogram, +checkresiduals) S3method(autoplot, acf) S3method(autoplot, ar) diff --git a/R/checkresiduals.R b/R/checkresiduals.R new file mode 100644 index 00000000..6738caed --- /dev/null +++ b/R/checkresiduals.R @@ -0,0 +1,38 @@ +checkresiduals <- function(object, lag) +{ + # Extract residuals + if(is.element("ts",class(object)) | is.element("numeric",class(object)) ) + residuals <- object + else + residuals <- residuals(object) + + # Produce plots + ggtsdisplay(residuals, plot.type="histogram") + + # Check if we have the model + if(is.element("forecast",class(object))) + object <- object$model + if(is.null(object)) + return() + + # Find model df + if(is.element("ets",class(object))) + df <- length(object$par) + else if(is.element("Arima",class(object))) + df <- length(object$coef) + else if(is.element("bats",class(object))) + df <- length(object$parameters$vect) + NROW(object$seed.states) + else + df <- NULL + + # Do Ljung-Box test + if(!is.null(df)) + { + freq <- frequency(residuals) + if(missing(lag)) + lag <- max(df+3, ifelse(freq>1, 2*freq, 10)) + print(Box.test(residuals, fitdf=df, lag=lag, type="Ljung")) + cat(paste("Model df: ",df,". Total lags used: ",lag,"\n\n",sep="")) + } +} + diff --git a/man/checkresiduals.Rd b/man/checkresiduals.Rd new file mode 100644 index 00000000..e4b74e3f --- /dev/null +++ b/man/checkresiduals.Rd @@ -0,0 +1,22 @@ +\name{checkresiduals} +\alias{checkresiduals} +\title{Check that residuals from a time series model look like white noise} +\usage{checkresiduals(object, lag) +} +\arguments{ +\item{object}{Either a time series model, a forecast object, or a time series (assumed to be residuals).} +\item{lag}{Number of lags to use in the Ljung-Box test. If missing, it is set to \code{max(10,df+3)} for non-seasonal data, and \code{max(2m, df+3)} for seasonal data, where \code{df} is the degrees of freedom of the model, and \code{m} is the seasonal period of the data.} +} + +\description{Produces a plot of the residuals, the corresponding ACF and histogram. If the degrees of freedom for the model can be determined, the output from a Ljung-Box test is returned. +} + +\value{None} + +\author{Rob J Hyndman} +\seealso{\code{\link{ggtsdisplay}}, \code{\link[stats]{Box.test}}} +\examples{ +fit <- ets(WWWusage) +checkresiduals(fit) +} +