## Introduction

In my last Time Series article, “Components of Time Series Data,” I discussed the trend, seasonal and cyclical components of time series data. Here I will discuss an effective method for performing analysis of this data. The Unobserved Components Model (UCM) (Harvey (1989)) performs a time series decomposition into components such as trend, seasonal, cycle, and the regression effects due to predictor series. It can be represented as follows:

The components T_{t}, C_{t}, and S_{t} represent the *trend*, *seasonal*, and *cyclical* components, respectively (they are usually represented by Greek letters); the term AR_{t} represents an unobserved *autoregressive* component; the term e_{t} gives the contribution of *regression* variables with fixed or time-varying regression coefficients. The *irregular* or *error terms* are assumed to be independently and identically distributed as Standard Normal distributions.

## Trend

For simplicity, trends are as the natural tendency of the series to increase or decrease or remain constant over a period of time in the absence of any other influencing variable.

UCM can model trend in two ways. The first is the random walk model implying that trend remains roughly constant over the time period of the series. The second relies on locally linear trend having an upward or downward slope.

## Cycle

Cycles in a time series data exist when the data exhibit rises and falls that are not of fixed period. The duration of these fluctuations is usually at least 2 years.

UCM provides two basic ways of modeling the unobserved cyclical component: a deterministic (non-stochastic) trigonometric cycle (or cycles) and a stochastic trigonometric cycle.

## Seasonality

A seasonal pattern exists when there is a consistent pattern of variation influenced by seasonal factors (e.g., the quarter of the year, or day of the week, etc.).

UCM provides two ways to deal with the unobserved seasonal component: a Stochastic Dummy Variable Seasonal model and a Deterministic Dummy Variable Seasonal model

## Autoregressive Component

Rather than modeling the cyclical nature of a time series via either the deterministic cyclical model or the stochastic cyclical model, one can use the rather straightforward specification

where the unobserved autoregressive component follows a first-order autoregression with -1 < ρ < 1. This autoregression, despite its simplicity, can capture many of the movements in time series data that represent business cycle inertia and that are present in many business and economic time series.

For a detailed discussion of all the above three factors see referenced article above.

## rucm-Package

The R-package **rucm** is written keeping in mind the easier specification of UCM in SAS using PROC UCM. **rucm** provides a wrapper function called ucm containing arguments specifying the formula for predictor variables, and other decomposition components such as level, slope, season, cycle, etc. to run UCM. ucm can also handle cases where we want to fix the variance of any of the above decomposition components.

To install rucm:

`#install.packages("rucm")`

`library(rucm)`

Note: In *RStudio*, select **Tools | Install Packages…** and enter **rucm**.

For help and the list of values that are returned see the help(package = rucm) or ?ucm.

Here we work with the Nile data which comes along with the datasets package, measures the annual flow of the river Nile at Ashwan, in south Egypt, between 1871 and 1970.

To model the level of Nile annual flow:

`modelNile <- ucm(formula = Nile~0, data = Nile, level = TRUE)`

`modelNile`

#Printing method for class ucm

`Call:ucm(formula = Nile ~ 0, data = Nile, level = TRUE) Parameter estimates:NULL Estimated variance:Irregular_Variance Level_Variance 15098.517 1469.175`

`plot(Nile, ylab = "Flow of Nile")`

`lines(modelNile$s.level, col = "blue")`

`legend("topright", legend = c("Observed flow","S_level"), col = c("black","blue"), lty = 1)`

The formula argument in the ucm function takes an argument of the form as.formula in R. For multivariate UC Models, the rhs of the formula should contain the independent variables. If the model is univariate, we write a 0 in the rhs of the formula specification. Slope, seasonality, and cyclicity can be included by using slope = TRUE, season = TRUE, cycle = TRUE, specifying the seasonal and cyclical lengths of the series in the arguments season.length and cycle.period, respectively.

ucm returns an object of class ucm having the estimate of predictors, estimated variances, time series of unobserved components (level, slope, whatever is included), and time series of the variances of these components.

To forecast the time series, we use the predict function supplying the model name and number of periods to forecast in n.ahead.

`modelNile <- ucm(formula = Nile~0, data = Nile, level = TRUE, slope = TRUE)`

`predict(modelNile$model, n.ahead = 12)`

# Forecasting

`Time Series:Start = 1971 End = 1982 Frequency = 1 [1] 779.5699 776.1597 772.7496 769.3395 765.9293 762.5192 759.1091 755.6989 752.2888[10] 748.8786 745.4685 742.0584`

## References

- Harvey A. (1989).
*Forecasting, structural time series models and the Kalman filter*. Cambridge New York: Cambridge University Press - Helske J (2014).
**KFAS**:*Kalman filter and Smoothers for Exponential Family State Space Models*. R package version 1.0.4-1, URL: http://CRAN.R-project.org/package=KFAS. *Hyndsight*. URL: http://robjhyndman.com/hyndsight/cyclicts/.- SAS Institute Inc (2010).
*SAS/ETS 9.22 User’s Guide*. SAS Institute Inc., Cary, NC. URL: http://support.sas.com/documentation/cdl/en/etsug/60372/PDF/default/etsug.pdf. - Selukar R (2011). “State Space Modeling Using SAS”.
*Journal of Statistical Software*,**41**(12), 1-13. URL: http://www.jstatsoft.org/v41/i12/. - Petris G, Petrone S (2011). “State Space Models in R”.
*Journal of Statistical Software*,**41**(4), 1-25. URL: http://www.jstatsoft.org/v41/i04/.

Copyright© 2014, Jeffrey Strickland

All rights reserved. This article or any portion thereof may not be reproduced or used in any manner whatsoever without the express written permission of the author except for the use of brief quotations in a review.

**Authored by:
**

**Jeffrey Strickland, Ph.D.**Jeffrey Strickland, Ph.D., is the Author of * Predictive Analytics Using R* and a Senior Analytics Scientist with Clarity Solution Group. He has performed predictive modeling, simulation and analysis for the Department of Defense, NASA, the Missile Defense Agency, and the Financial and Insurance Industries for over 20 years. Jeff is a Certified Modeling and Simulation professional (CMSP) and an Associate Systems Engineering Professional (ASEP). He has published nearly 200 blogs on LinkedIn, is also a frequently invited guest speaker and the author of 20 books including:

__Operations Research using Open-Source Tools__*Discrete Event simulation using ExtendSim**Crime Analysis and Mapping**Missile Flight Simulation**Mathematical Modeling of Warfare and Combat Phenomenon**Predictive Modeling and Analytics**Using Math to Defeat the Enemy**Verification and Validation for Modeling and Simulation**Simulation Conceptual Modeling**System Engineering Process and Practices*

Connect with __Jeffrey Strickland
__Contact

__Jeffrey Strickland__

Categories: Articles, Education & Training, Featured, Jeffrey Strickland

Hi Jeffrey Strickland, if we use predictors/Independent variables in the UCM, we cant predict the future values, there is some error. Could you provide some information on how to resolve it?

LikeLike