fitted.marssMLE {MARSS} | R Documentation |
Return fitted values for X(t) and Y(t) in a MARSS model
Description
fitted()
returns the different types of fitted values for and
in a MARSS model. The fitted values are the expected value of the right side of the MARSS equations without the error terms, thus are the model predictions of
or
.
fitted.marssMLE
is a companion function to tsSmooth()
which returns the expected value of the right side of the MARSS equations with the error terms (the Kalman filter and smoother output).
The data go from to
. For brevity, the parameter matrices are shown without a time subscript, however all parameters can be time-varying.
Note that the prediction of conditioned on the data up to time
is not provided since that would require the expected value of
conditioned on data from
to
, which is not output from the Kalman filter or smoother.
Usage
## S3 method for class 'marssMLE'
fitted(object, ...,
type = c("ytt1", "ytT", "xtT", "ytt", "xtt1"),
interval = c("none", "confidence", "prediction"),
level = 0.95,
output = c("data.frame", "matrix"),
fun.kf = c("MARSSkfas", "MARSSkfss"))
Arguments
object |
A |
type |
If |
interval |
If |
level |
Level for the intervals if |
output |
data frame or list of matrices |
fun.kf |
By default, |
... |
Not used. |
Details
In the state-space literature, the two most commonly used fitted values are "ytt1"
and
"ytT"
. The former is the expected value of conditioned on the data 1 to time
. These are known as the innovations and they, plus their variance, are used in the calculation of the likelihood of a MARSS model via the innovations form of the likelihood. The latter,
"ytT"
are the model estimates of the values using all the data; this is the expected value of
conditioned on the data 1 to
. The
"xtT"
along with "ytT"
are used for computing smoothation residuals used in outlier and shock detection. See MARSSresiduals
. For completeness, fitted.marssMLE
will also return the other possible model predictions with different conditioning on the data (1 to ,
, and
), however only
type="ytt1"
(innovations) and "ytT"
and "xtT"
(smoothations) are regularly used. Keep in mind that the fitted "xtT"
is not the smoothed estimate of (unlike
"ytT"
). If you want the smoothed estimate of (i.e. the expected value of
conditioned on all the data), you want the Kalman smoother. See
tsSmooth
.
Fitted versus estimated values: The fitted states at time are predictions from the estimated state at time
conditioned on the data: expected value of
conditioned on the data. They are distinguished from the estimated states at time
which would includes the conditional expected values of the error terms:
. The latter are returned by the Kalman filter and smoother. Analogously, the fitted observations at time
are model predictions from the estimated state at time
conditioned on the data: the expected value of the right side of the
equation without the error term. Like with the states, one can also compute the expected value of the observations at time
conditioned on the data: the expected value of the right side of the
equation with the error term. The latter would just be equal to the data if there are no missing data, but when there are missing data, this is what is used to estimate their values. The expected value of states and observations are provided via
tsSmooth
.
observation fitted values
The model predicted is
, where
is the expected value of the state at time
conditioned on the data from 1 to
(
will be
,
or
). Note, if you are using MARSS for estimating the values for missing data, then you want to use
tsSmooth()
with type="ytT"
not fitted(..., type="ytT")
.
is the expected value of the states at time
conditioned on the data from time 1 to
. If
type="ytT"
, the expected value is conditioned on all the data, i.e. xtT
returned by MARSSkf()
. If type="ytt1"
, then the expected value uses only the data up to time , i.e.
xtt1
returned by MARSSkf()
. These are commonly known as the one step ahead predictions for a state-space model. If type="ytt"
, then the expected value uses the data up to time , i.e.
xtt
returned by MARSSkf()
.
If interval="confidence"
, the standard error and interval is for the predicted . The standard error is
. If
interval="prediction"
, the standard deviation of new iid data sets is returned. The standard deviation of new
is
.
is conditioned on the data from
to
.
will be either
,
or
depending on the value of
type
.
Intervals returned by predict()
are not for the data used in the model but rather new data sets. To evaluate the data used to fit the model for residuals analysis or analysis or model inadequacy, you want the model residuals (and residual se's). Use residuals
for model residuals and their intervals. The intervals for model residuals are narrower because the predictions for were estimated from the model data (so is closer to the data used to estimate the predictions than new independent data will be).
state fitted values
The model predicted given
is
. If you want estimates of the states, rather than the model predictions based on
, go to
tsSmooth()
. Which function you want depends on your objective and application.
used in the prediction is the expected value of the states at time
conditioned on the data from
to
. If
type="xtT"
, this is the expected value at time conditioned on all the data, i.e.
xtT[,t-1]
returned by MARSSkf()
. If type="xtt1"
, it is the expected value conditioned on the data up to time , i.e.
xtt[,t-1]
returned by MARSSkf()
. The predicted state values conditioned on data up to are not provided. This would require the expected value of states at time
conditioned on data up to time
, and this is not output by the Kalman filter. Only conditioning on data up to
and
are output.
If interval="confidence"
, the standard error of the predicted values (meaning the standard error of the expected value of given
) is returned. The standard error of the predicted value is
. If
interval="prediction"
, the standard deviation of given
is output. The latter is
.
is either conditioned on data 1 to
or
depending on
type
.
The intervals returned by fitted.marssMLE()
are for the state predictions based on the state estimate at . These are not typically what one uses or needs–however might be useful for simulation work. If you want confidence intervals for the state estimates, those are provided in
tsSmooth
. If you want to do residuals analysis (for outliers or model inadequacy), you want the residuals intervals provided in residuals()
.
Value
If output="data.frame"
(the default), a data frame with the following columns is returned. If output="matrix"
, the columns are returned as matrices in a list. Information computed from the model has a leading "." in the column name.
If interval="none"
, the following are returned (colnames with .
in front are computed values):
.rownames |
Names of the data or states. |
t |
Time step. |
y |
The data if |
.x |
The expected value of |
.fitted |
Predicted values of observations ( |
If interval = "confidence"
, the following are also returned:
.se |
Standard errors of the predictions. |
.conf.low |
Lower confidence level at |
.conf.up |
Upper confidence level. The interval is approximated using qnorm(1-alpha/2)*.se + .fitted |
The confidence interval is for the predicted value, i.e. for
or
for
where
is the expected value of
conditioned on the data from 1 to
. (
will be
,
or
).
If interval = "prediction"
, the following are also returned:
.sd |
Standard deviation of new |
.lwr |
Lower range at |
.upr |
Upper range at |
The prediction interval is for new or
. If you want to evaluate the observed data or the states estimates for outliers then these are not the intervals that you want. For that you need the residuals intervals provided by
residuals()
.
Author(s)
Eli Holmes, NOAA, Seattle, USA.
See Also
MARSSkf()
, MARSSresiduals()
, residuals()
, predict()
, tsSmooth()
Examples
dat <- t(harborSeal)
dat <- dat[c(2, 11, 12), ]
fit <- MARSS(dat, model = list(Z = factor(c("WA", "OR", "OR"))))
fitted(fit)
# Example of fitting a stochastic level model to the Nile River flow data
# red line is smooothed level estimate
# grey line is one-step-ahead prediction using xtt1
nile <- as.vector(datasets::Nile)
mod.list <- list(
Z = matrix(1), A = matrix(0), R = matrix("r"),
B = matrix(1), U = matrix(0), Q = matrix("q"),
x0 = matrix("pi")
)
fit <- MARSS(nile, model = mod.list, silent = TRUE)
# Plotting
# There are plot methods for marssMLE objects
library(ggplot2)
autoplot(fit)
# Below shows how to make plots manually but all of these can be made
# with autoplot(fit) or plot(fit)
plot(nile, type = "p", pch = 16, col = "blue")
lines(fitted(fit, type="ytT")$.fitted, col = "red", lwd = 2)
lines(fitted(fit, type="ytt1")$.fitted, col = "grey", lwd = 2)
# Make a plot of the model estimate of y(t), i.e., put a line through the points
# Intervals are for new data not the blue dots
# (which were used to fit the model so are not new)
library(ggplot2)
d <- fitted(fit, type = "ytT", interval="confidence", level=0.95)
d2 <- fitted(fit, type = "ytT", interval="prediction", level=0.95)
d$.lwr <- d2$.lwr
d$.upr <- d2$.upr
ggplot(data = d) +
geom_line(aes(t, .fitted), linewidth=1) +
geom_point(aes(t, y), color = "blue", na.rm=TRUE) +
geom_ribbon(aes(x = t, ymin = .conf.low, ymax = .conf.up), alpha = 0.3) +
geom_line(aes(t, .lwr), linetype = 2) +
geom_line(aes(t, .upr), linetype = 2) +
facet_grid(~.rownames) +
xlab("Time Step") + ylab("Count") +
ggtitle("Blue=data, Black=estimate, grey=CI, dash=prediction interval") +
geom_text(x=15, y=7, label="The intervals are for \n new data not the blue dots")