Learn R Programming

onlineforecast (version 1.0.2)

residuals.data.frame: Calculate the residuals given a forecast matrix and the observations.

Description

Calculate the residuals given a forecast matrix and the observations.

Usage

# S3 method for data.frame
residuals(object, y, ...)

# S3 method for matrix residuals(object, y, ...)

# S3 method for list residuals(object, y, ...)

# S3 method for forecastmodel_fit residuals(object, ...)

Value

If object is a matrix or data.frame: a data.frame with the residuals for each horizon. If object is a list: A list with residuals from each element.

Arguments

object

The forecast matrix (a data.frame with kxx as column names, Yhat in returned fits).

y

The observations vector.

...

Not used.

Details

Simply give the forecast matrix and the observations to get the residuals for each horizon in the forecast matrix.

The residuals returned are synced with the observations (i.e. k0) and the columns are names "hxx" (not kxx) to indicate this and will not be lagged in plot_ts().

Examples

Run this code
# ?? list example
# Just a vector to be forecasted
n <- 100
D <- data.list()
D$t <- 1:n
D$y <- c(filter(rnorm(n), 0.95, "recursive"))
plot(D$y, type="l")

# Generate a forecast matrix with a simple persistence model
D$Yhat <- persistence(D$y, kseq=1:4)

# The residuals for each horizon
D$Resid <- residuals(D$Yhat, D$y)
D$Resid
# Note the names of the columns
names(D$Resid)
# which means that they are aligned with the observations and will not be lagged in the plot
plot_ts(D, c("y|Yhat","Resid"))

# Check that it matches (the forecasts is lagged in the plot_ts
# such that the forecast for t+k is at t+k (and not t))
plot_ts(D, c("y|Yhat","Resid"), xlim=c(1,10), kseq=1,
        plotfun=function(x,...){lines(x,...,type="b")})

# Just for fun, see the auto-correlation function of the persistence 
acf(D$Resid$h1, na.action=na.pass)
acf(D$Resid$h4, na.action=na.pass)

Run the code above in your browser using DataLab