predict.marssMLE {MARSS} | R Documentation |
predict and forecast MARSS MLE objects
Description
This function will return the modeled value of \mathbf{y}_t
or \mathbf{x}_t
conditioned on the data (either the data used to fit the model or data in newdata
). For \mathbf{y}_t
, this is \mathbf{Z}_t \mathbf{x}_t^T+\mathbf{a}_t+\mathbf{D}_t\mathbf{d}_t
. For \mathbf{x}_t
, this is \mathbf{B}_t \mathbf{x}_{t-1}^T+\mathbf{u}_t+\mathbf{C}_t\mathbf{c}_{t}
. \mathbf{x}_t^T
is the smoothed state estimate at time t
conditioned on all the data (either data used to fit the model or the optional data passed into newdata
).
If you want the estimate of \mathbf{x}_t
conditioned on all the data (i.e. output from the Kalman filter or smoother), then use tsSmooth()
. Note that the prediction of \mathbf{x}_t
conditioned on the data up to time t
is not provided since that would require the estimate of \mathbf{x}_t
conditioned on data 1 to t+1
, which is not output from the Kalman filter or smoother.
If h
is passed in, predict(object)
will return a forecast h
steps past the end of the model data. predict(object)
returns a marssPredict
object which can be passed to plot()
or ggplot2::autoplot()
for automatic plotting of predictions and forecasts with intervals.
Usage
## S3 method for class 'marssMLE'
predict(object, n.ahead = 0,
level = c(0.80, 0.95),
type = c("ytt1", "ytT", "xtT", "ytt", "xtt1"),
newdata = list(t=NULL, y=NULL, c=NULL, d=NULL),
interval = c("none", "confidence", "prediction"),
fun.kf = c("MARSSkfas", "MARSSkfss"),
x0 = "reestimate", ...)
Arguments
object |
A |
n.ahead |
Number of steps ahead to forecast. If |
level |
Level for the intervals if |
type |
|
newdata |
An optional list with new |
interval |
If |
fun.kf |
Only if you want to change the default Kalman filter. Can be ignored. |
x0 |
If "reestimate" (the default), then the initial value for the states is re-estimated. If |
... |
Other arguments. Not used. |
Details
Forecasts n.ahead != 0
The type="xtT"
forecast is the states forecast conditioned on all the data. If n.ahead !=0
, then 'data' that is being conditioned on is the original data (model data) plus any data in newdata$y
for the h forecast time steps. Note, typically forecasts would not have data, since they are forecasts, but predict.marssMLE()
allows you to specify data for the forecast time steps if you need to. If the model includes covariates (\mathbf{c}
and/or \mathbf{d}
matrices passed into the model
list in the MARSS()
call), then c
and/or d
must be passed into newdata
.
The type="ytT"
forecast is the expected value of NEW data (\mathbf{Y}
) conditioned on the data used for fitting. The data used for fitting is the same as for type="xtT"
(above). The \mathbf{y}
forecast is Z xtT[,T+i] + A + D d[,T+i]
.
If the model has time-varying parameters, the value of the parameters at the last time step are used for the forecast.
Model predictions n.ahead == 0
If newdata
is not passed in, then the model data (\mathbf{y}
) and \mathbf{c}
and \mathbf{d}
(if part of model) are used for the predictions. fitted(object, type="ytT")
is the internal function for model predictions in that case.
If newdata
is passed in, then the predictions are computed using newdata
but with the MARSS model estimated from the original data, essentially the Kalman filter/smoother is run using the estimated MARSS model but with data (and \mathbf{c}
and \mathbf{d}
if in the model) in newdata
. y
, c
and d
in the newdata
list must all have the same number of columns (time-steps) and the length of t
in newdata
must be the same as the number of columns and must be sequential.
For type="ytT"
, the predictions are conceptually the same as predictions returned by predict.lm
for a linear regression. The confidence interval is the interval for the expected value of NEW data. The prediction interval is the interval for NEW data. Prediction intervals will always be wider (or equal if R=0) to confidence intervals. The difference is that the uncertainty in predict.lm
comes from parameter uncertainty and the data error while in predict.marssMLE
, the uncertainty is from \mathbf{x}
uncertainty and data error. Parameter uncertainty does not enter the interval calculations; parameters are treated as known at their point estimates. This is not specific to the MARSS package. This is how prediction and confidence intervals are presented for MARSS models in the literature, i.e. no parameter uncertainty.
t
innewdata
:If the model has time-varying parameters,
t
innewdata
removes any ambiguity as to which parameter values (time steps) will be used for prediction. In this case,t
specifies which time values of the parameters you want to use. If you leave offt
, then it is assumed thatt
starts at the first time step in the data used to fit the original model. If the model is time-constant,t
is used to set the time step values (used for plotting, etc.).- The model has
\mathbf{c}
and/or\mathbf{d}
: -
c
and/ord
must be included innewdata
. Ify
(new data) is not innewdata
, it is assumed to be absent (all NA). That is, the default behavior ify
is absent butc
and/ord
is present isy="none"
. If you want to use the original data used to fit the model, then pass iny="model"
innewdata
. Pass int
innewdata
if it is ambiguous which time steps of the model data to use. - The model has time-varying parameters:
You have to pass in
t
innewdata
to specify what parameter values to use. If anyt > T
(T
equals the last time step in the model data), then it is assumed that you want to use the parameter values at the last time step of the original time series for values beyond the last time step. See examples.y
,c
andd
innewdata
have more time steps than the original data:If the model has time-varying parameters, you will need to pass in
t
. If the model is time-constant, thent
is assumed to start at the first time step in the original data but you can pass int
to change that. It will not change the prediction, but will change the t column in the output.
x0 estimation If you are passing in y
in newdata
, then it is likely that you will need to re-estimate the \mathbf{x}
initial condition. The default behavior of predict.marssMLE
. Use x0 = "use.model"
to use the initial values in the estimated model (object
).
Value
A list with the following components:
method |
The method used for fitting, e.g. MARSS kem. |
model |
The |
newdata |
The |
level |
The confidence or prediction intervals |
pred |
A data frame the predictions or forecasts along with the intervals. |
type |
The |
t |
The time steps in the pred data frame. |
n.ahead and h |
The number of forecast time steps. |
x0 |
The x0 used for the predictions. |
tinitx |
The tinitx used. |
The pred data frame has the following columns:
.rownames |
Names of the data or states. |
t |
Time step. |
y |
The data if |
xtT |
The estimate of |
xtt |
The estimate of |
estimate |
Model predicted values of observations ( |
If intervals are returned, the following are added to the data frame:
se |
Standard errors of the predictions. |
Lo ... |
Lower confidence level at |
Hi ... |
Upper confidence level. The interval is approximated using qnorm(1-alpha/2)*se + prediction. |
Author(s)
Eli Holmes, NOAA, Seattle, USA.
See Also
plot.marssPredict()
, fitted.marssMLE()
Examples
dat <- t(harborSealWA)
dat <- dat[2:4,] #remove the year row
fit <- MARSS(dat, model=list(R="diagonal and equal"))
# 2 steps ahead forecast
fr <- predict(fit, type="ytT", n.ahead=2)
plot(fr)
# use model data with the estimated initial values (at t=0) for
# initial values at t=9
# This would be a somewhat strange thing to do and the value at t=10 will look wrong.
fr <- predict(fit, newdata=list(t=10:20, y=dat[,10:20]), x0 = "use.model")
plot(fr)
# pass in new data and give it new t; initial conditions will be estimated
fr <- predict(fit, newdata=list(t=23:33, y=matrix(10,3,11)))
plot(fr, ylim=c(8,12))
# Covariate example
fulldat <- lakeWAplanktonTrans
years <- fulldat[,"Year"]>=1965 & fulldat[,"Year"]<1975
dat <- t(fulldat[years,c("Greens", "Bluegreens")])
dat <- zscore(dat)
covariates <- rbind(
Temp = fulldat[years, "Temp"],
TP = fulldat[years, "TP"])
covariates <- zscore(covariates)
A <- U <- "zero"
B <- Z <- "identity"
R <- diag(0.16,2)
Q <- "equalvarcov"
C <- "unconstrained"
model.list <- list(B=B,U=U,Q=Q,Z=Z,A=A,R=R,C=C,c=covariates)
fit <- MARSS(dat, model=model.list)
# Use a new c (covariate) but no data.
fr <- predict(fit, newdata=list(c=matrix(5,2,10)), x0="use.model")
plot(fr)
# Use first 10 time steps of model data
plot(predict(fit, newdata=list(y=dat[,1:10], c=matrix(5,2,10))))
# Use all model data but new covariates
# Why does it look so awful? Because this is a one-step ahead
# prediction and there is no info on what the c will be at t
plot(predict(fit, newdata=list(y=dat, c=matrix(5,2,120))))
# Use all model data but new covariates with ytT type
# this looks better because is uses all the c data to estimate (so knows what c is at t and beyond)
plot(predict(fit, newdata=list(y=dat, c=matrix(5,2,120)), type="ytT"))
# Use no data; cannot estimate initial conditions without data
# so x0 must be "use.model"
fr <- predict(fit, newdata=list(c=matrix(5,2,22)), x0="use.model")
plot(fr)
# forecast with covariates
# n.ahead and the number column in your covariates in newdata must match
plot(predict(fit, newdata=list(c=matrix(5,2,10)), n.ahead=10))
# forecast with covariates and only show last 10 steps of original data
plot(predict(fit, newdata=list(c=matrix(5,2,10)), n.ahead=10), include=10)