| modelAR | R Documentation |
Experimental function to forecast univariate time series with a user-defined model
modelAR(
y,
p,
P = 1,
FUN,
predict.FUN,
xreg = NULL,
lambda = NULL,
model = NULL,
subset = NULL,
scale.inputs = FALSE,
x = y,
...
)
y |
a numeric vector or univariate time series of class |
p |
Embedding dimension for non-seasonal time series. Number of non-seasonal lags used as inputs. For non-seasonal time series, the default is the optimal number of lags (according to the AIC) for a linear AR(p) model. For seasonal time series, the same method is used but applied to seasonally adjusted data (from an stl decomposition). |
P |
Number of seasonal lags used as inputs. |
FUN |
Function used for model fitting. Must accept argument |
predict.FUN |
Prediction function used to apply |
xreg |
Optionally, a numerical vector or matrix of external regressors,
which must have the same number of rows as |
lambda |
Box-Cox transformation parameter. If |
model |
Output from a previous call to |
subset |
Optional vector specifying a subset of observations to be used
in the fit. Can be an integer index vector or a logical vector the same
length as |
scale.inputs |
If |
x |
Deprecated. Included for backwards compatibility. |
... |
Other arguments passed to |
This is an experimental function and only recommended for advanced users.
The selected model is fitted with lagged values of y as inputs. The inputs
are for lags 1 to p, and lags m to mP where m = frequency(y). If
xreg is provided, its columns are also used as inputs. If there are
missing values in y or xreg, the corresponding rows (and any others
which depend on them as lags) are omitted from the fit. The model is trained
for one-step forecasting. Multi-step forecasts are computed recursively.
Returns an object of class modelAR.
The function summary is used to obtain and print a summary of the
results.
The generic accessor functions fitted.values and residuals
extract useful features of the value returned by modelAR.
model |
A list containing information about the fitted model |
method |
The name of the forecasting method as a character string |
x |
The original time series. |
xreg |
The external regressors used in fitting (if given). |
residuals |
Residuals from the fitted model. That is x minus fitted values. |
fitted |
Fitted values (one-step forecasts) |
... |
Other arguments |
Rob J Hyndman and Gabriel Caceres
## Set up functions
my_lm <- function(x, y) {
structure(lsfit(x,y), class = "lsfit")
}
predict.lsfit <- function(object, newdata = NULL) {
n <- length(object$qr$qt)
if(is.null(newdata)) {
z <- numeric(n)
z[seq_len(object$qr$rank)] <- object$qr$qt[seq_len(object$qr$rank)]
as.numeric(qr.qy(object$qr, z))
} else {
sum(object$coefficients * c(1, newdata))
}
}
# Fit an AR(2) model
fit <- modelAR(
y = lynx,
p = 2,
FUN = my_lm,
predict.FUN = predict.lsfit,
lambda = 0.5,
scale.inputs = TRUE
)
forecast(fit, h = 20) |> autoplot()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.