Skip to content

Commit

Permalink
Added checkresiduals() function
Browse files Browse the repository at this point in the history
  • Loading branch information
robjhyndman committed Oct 25, 2016
1 parent d789108 commit 4aadd64
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
1 change: 1 addition & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Version 7.4 (?? 2016)
* Added gghistogram() and checkresiduals()
* Bug fixes

Version 7.3 (12 October 2016)
Expand Down
3 changes: 2 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
38 changes: 38 additions & 0 deletions R/checkresiduals.R
Original file line number Diff line number Diff line change
@@ -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=""))
}
}

22 changes: 22 additions & 0 deletions man/checkresiduals.Rd
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 4aadd64

Please sign in to comment.