fittestPolyRKF: Automatic fitting and prediction of polynomial regression...

Description Usage Arguments Details Value Author(s) References See Also Examples

View source: R/fittestPolyRKF.r

Description

The function predicts and returns the next n consecutive values of a univariate time series using the best evaluated polynomial regression model automatically fitted with Kalman filter. It also evaluates the fitness of the produced model, using AICc, AIC, BIC and logLik criteria, and its prediction accuracy, using the MSE, NMSE, MAPE, sMAPE and maximal error accuracy measures.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
fittestPolyRKF(
  timeseries,
  timeseries.test = NULL,
  h = NULL,
  na.action = stats::na.omit,
  level = 0.9,
  order = NULL,
  minorder = 0,
  maxorder = 5,
  initQ = NULL,
  filtered = TRUE,
  rank.by = c("MSE", "NMSE", "MAPE", "sMAPE", "MaxError", "AIC", "AICc", "BIC",
    "logLik", "errors", "fitness")
)

Arguments

timeseries

A vector or univariate time series which contains the values used for fitting a polynomial regression model with Kalman filter. ~~Describe timeseries here~~

timeseries.test

A vector or univariate time series containing a continuation for timeseries with actual values. It is used as a testing set and base for calculation of prediction error measures. Ignored if NULL.

h

Number of consecutive values of the time series to be predicted. If h is NULL, the number of consecutive values to be predicted is assumed to be equal to the length of timeseries.test. Required when timeseries.test is NULL.

na.action

A function for treating missing values in timeseries and timeseries.test. The default function is na.omit, which omits any missing values found in timeseries or timeseries.test.

level

Confidence level for prediction intervals. See the predict.SSModel function in the KFAS package. ~~Describe na.action here~~

order

A numeric integer value corresponding to the order of polynomial regression to be fitted. If NULL, the order of the polynomial regression returned by the function is automatically selected within the interval minorder:maxorder. See 'Details'.

minorder

A numeric integer value corresponding to the minimum order of candidate polynomial regression to be fitted and evaluated. Ignored if order is provided. See 'Details'.

maxorder

A numeric integer value corresponding to the maximal order of candidate polynomial regression to be fitted and evaluated. Ignored if order is provided. See 'Details'.

initQ

Numeric argument regarding the initial values for the covariance of disturbances parameter to be optimized over. The initial values to be optimized are set to rep(initQ,(order+1)). See the Q argument of the SSModel function in the KFAS package and the examples in KFAS. If NULL, initQ is automatically set. See 'Details'.

filtered

If filtered is TRUE, Kalman filtered time series observations are used for prediction, otherwise, Kalman smoothed observations are used for prediction.

rank.by

Character string. Criteria used for ranking candidate models generated using different options of values for order and/or initQ. Ignored if both order and initQ are provided. See 'Details'.

Details

The polynomial regression model produced and returned by the function is generated and represented as state space model (SSModel) based on code from the dlmodeler package. See dlmodeler.polynomial. The model is optimized using the Kalman filter and functions of the KFAS package (see fitSSM).

If order is NULL, it is automatically selected. For that, a set of candidate polynomial regression state space models of orders from minorder to maxorder is generated and evaluated. Also, if initQ is NULL, it is automatically set as either log(stats::var(timeseries)) or 0. For that, candidate models receive different initial parameterization of initQ during the model optimization process. The value options of order and/or initQ which generate the best ranked candidate polynomial regression model acoording to the criteria in rank.by are selected.

The ranking criteria in rank.by may be set as a prediction error measure (such as MSE, NMSE, MAPE, sMAPE or MAXError), or as a fitness criteria (such as AIC, AICc, BIC or logLik). In the former case, the candidate models are used for time series prediction and the error measures are calculated by means of a cross-validation process. In the latter case, the candidate models are fitted and fitness criteria are calculated based on all observations in timeseries.

If rank.by is set as "errors" or "fitness", the candidate models are ranked by all the mentioned prediction error measures or fitness criteria, respectively. The wheight of the ranking criteria is equally distributed. In this case, a rank.position.sum criterion is produced for ranking the candidate models. The rank.position.sum criterion is calculated as the sum of the rank positions of a model (1 = 1st position = better ranked model, 2 = 2nd position, etc.) on each calculated ranking criteria.

Value

A list with components:

model

An object of class "SSModel" containing the best evaluated polynomial regression model fitted with Kalman Filter.

order

The order argument provided (or automatically selected) for the best evaluated polynomial regression model fitted with Kalman Filter.

initQ

The initQ argument provided (or automatically selected) for optimization of the best evaluated polynomial regression model fitted with Kalman Filter.

AICc

Numeric value of the computed AICc criterion of the best evaluated model.

AIC

Numeric value of the computed AIC criterion of the best evaluated model.

BIC

Numeric value of the computed BIC criterion of the best evaluated model.

logLik

Numeric value of the computed log-likelihood of the best evaluated model.

pred

A list with the components mean, lower and upper, containing the predictions of the best evaluated model and the lower and upper limits for prediction intervals, respectively. All components are time series. See predict.SSModel.

MSE

Numeric value of the resulting MSE error of prediction. Require timeseries.test.

NMSE

Numeric value of the resulting NMSE error of prediction. Require timeseries.test.

MAPE

Numeric value of the resulting MAPE error of prediction. Require timeseries.test.

sMAPE

Numeric value of the resulting sMAPE error of prediction. Require timeseries.test.

MaxError

Numeric value of the maximal error of prediction. Require timeseries.test.

rank.val

Data.frame with the fitness or prediction accuracy criteria computed for all candidate polynomial regression with Kalman filter models ranked by rank.by. It has the attribute "ranked.models", which is a list of objects of class "SSModel" containing all the candidate polynomial regression models fitted with Kalman Filter, also ranked by rank.by. Only provided if order or initQ were automatically selected.

rank.by

Ranking criteria used for ranking candidate models and producing rank.val.

Author(s)

Rebecca Pontes Salles

References

R.J. Hyndman and G. Athanasopoulos, 2013, Forecasting: principles and practice. OTexts.

R.H. Shumway and D.S. Stoffer, 2010, Time Series Analysis and Its Applications: With R Examples. 3rd ed. 2011 edition ed. New York, Springer.

See Also

fittestPolyR, fittestLM ~

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
data(CATS,CATS.cont)
fPolyRKF <- fittestPolyRKF(CATS[,1],CATS.cont[,1])
#predicted values
pred <- fPolyRKF$pred

#extracting Kalman filtered and smoothed time series from the best fitted model
fs <- KFAS::KFS(fPolyRKF$model,filtering=c("state","mean"),smoothing=c("state","mean"))
f <- fitted(fs, filtered = TRUE) #Kalman filtered time  series
s <- fitted(fs) #Kalman smoothed time  series
#plotting the time series data
plot(c(CATS[,1],CATS.cont[,1]),type='o',lwd=2,xlim=c(960,1000),ylim=c(0,200),
 xlab="Time",ylab="PRKF")
#plotting the Kalman filtered time series
lines(f,col='red',lty=2,lwd=2)
#plotting the Kalman smoothed time series
lines(s,col='green',lty=2,lwd=2)
#plotting predicted values
lines(ts(pred$mean,start=981),lwd=2,col='blue')
#plotting prediction intervals
lines(ts(pred$lower,start=981),lwd=2,col='light blue')
lines(ts(pred$upper,start=981),lwd=2,col='light blue')

RebeccaSalles/TSPred documentation built on April 6, 2021, 2:44 a.m.