-
Notifications
You must be signed in to change notification settings - Fork 341
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d789108
commit 4aadd64
Showing
4 changed files
with
63 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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="")) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|