```
library(UComp)
#> Loading required package: Rcpp
```

Here there are some examples run with package UComp, for the automatic modelling of Unobserved Components models a la Harvey (1989). The absolute novelty of this package is that it provides an automatic identification of UC models, though particular models may be estimated, if desired. The package also perform outlier detection, may identify cycles of period longer than one year and allows for models with exogenous inputs.

The library is composed of just a few functions to perform a comprehensive analysis of time series:

- UC: runs everything
- UCsetup: creates UComp objects
- UCmodel: runs UCsetup, estimates/identifies model and produce forecasts
- UCvalidate: runs diagnositics checks and show estimation table
- UCfilter: filters the data and computes states and their covariance matrices
- UCsmooth: as UCfilter, but smoothing the data
- UCdisturb: runs the disturbance smoother
- UCcomponents: estimate components (trend, seasonal, irregular, etc.)
- UChp: estimate Hodrick-Prescott filter

The inputs to UC, UCsetup and UCmodel are the same and control how the rest of functions work (check the manual and examples below).

Now, the examples. First, just load the library and some data (monthly Spanish industrial production index) from January 2000 to December 2019. the last year of data is reserved to check forecasting performance.

```
load(url("https://github.com/djpedregal/djpedregal.github.io/blob/master/ipi.Rdata?raw=true"))
= ts(y[1 : 208], start = 2000, frequency = 12)
ipi plot(ipi)
```

The call could be as simple as the next to estimate the optimal model and plot the components. The output shows the estimated models with their information criteria values and the table with the estimates and some diagnostics tests.

`= UC(log(ipi)) m `

`plot(m)`

A different call, more complex, would be as follows (just one model is estimated, the forecasting horizon is fixed at 24 months, verbose output is avoided, see the full set of options in the documentation). As can be easily seen, the diagnostics tests indicate that this model is completely inappropriate:

`= UC(log(ipi), model = "rw/equal/arma(0,0)", h = 24, verbose = FALSE) m `

Something that is very interesting in this case is the estimation with Easter and calendar effects (already loaded in matrix u) and with outlier detection. Check out the diagnostics and now to see that this model is much more adequate:

`= UC(log(ipi), u = u, outlier = 4) m `

`plot(m)`

Beta(1) and Beta(2) are the parameter estimates for Easter and the calendar effects and there is one oulier detected in observations 108 as a level shift.

Finally, forecasts and their variances are stored in fields yFor and yForV, respectively. The following plot shows forecasts and their 95% confidence bands.

```
plot(m$yFor, ylim = c(4.3, 4.85))
lines(m$yFor + 2 * sqrt(m$yForV), col = "red")
lines(m$yFor - 2 * sqrt(m$yForV), col = "red")
```