Skip to content

Commit

Permalink
Final version corrected, ver. 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
k41m4n committed Sep 3, 2020
1 parent f5e5d3d commit 60777bf
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 24 deletions.
Binary file added Figures/CKbook.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed Figures/cover.jpg
Binary file not shown.
26 changes: 14 additions & 12 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ library(knitr)
```
This repository provides code in R reproducing examples of the states space models presented in book ["An Introduction to State Space Time Series Analysis"](http://www.ssfpack.com/CKbook.html) by Jacques J.F. Commandeur and Siem Jan Koopman.

![](Figures/cover.jpg)
![](Figures/CKbook.png)

The code is provided in file "SSM.R"" and split into sections coresponding to the following parts of the book:
The repository uses extensively the [package KFAS](https://cran.r-project.org/web/packages/KFAS/index.html) of Jouni Helske which includes computationally efficient functions for Kalman filtering, smoothing,forecasting, and simulation of multivariate exponential family state space models. Additionally, some own functions has been created to facilitate the calculation and presentation of diagnostics.

The code is provided in file "SSM.R"" and split into sections corresponding to the following parts of the book:

* Introduction
* Chapter 2 The Local Level Model
Expand All @@ -31,7 +33,7 @@ The code is provided in file "SSM.R"" and split into sections coresponding to th
* Chapter 8 General Treatment of Univariate State Space Models
* Chapter 9 Multivariate Time Series Analysis

In R Studio, each section of the code can be executed with keys CTRL+ALT+T, after placing a coursor in that section. Please make sure to execute the first section of the code including all the functions that are used by the other sections of the code.
In R Studio, each section of the code can be executed with keys CTRL+ALT+T, after placing a cursor in that section. Please make sure to execute the first section of the code including the own defined functions that are used by the other sections of the code.

```{r message=FALSE, warning=FALSE, echo=FALSE}
if(!(require(normtest))){install.packages('normtest')}
Expand Down Expand Up @@ -231,13 +233,13 @@ initValOpt2 <- function(formula = "log(rep(x, 3))", model_ = model, updatefn_ =

Below, the code of the stochastic level and slope model of chapter 3 is shown as an example.

Loading data:
Loading data on the UK drivers killed or seriously injured (KSI):
```{r message=FALSE, warning=FALSE}
dataUKdriversKSI <- log(read.table("UKdriversKSI.txt")) %>%
ts(start = 1969, frequency = 12)
```

Defining the model:
Defining the model using function `SSModelof()` of the KFAS package:
```{r message=FALSE, warning=FALSE}
model <- SSModel(dataUKdriversKSI ~ SSMtrend(degree = 2,
Q = list(matrix(NA), matrix(NA))), H = matrix(NA))
Expand All @@ -254,7 +256,7 @@ Providing the number of diffuse initial values in the state:
d <- q <- 2
```

Defining the number of estimated hyperparameters (i.e. disturbance variances):
Defining the number of estimated hyperparameters (two state disturbance variances + irregular disturbance variance):
```{r message=FALSE, warning=FALSE}
w <- 3
```
Expand All @@ -273,7 +275,7 @@ Providing the number of observations:
n <- 192
```

Fitting the model:
Fitting the model using function `fitSSM` and extracting the output using function `KFS` of the KFAS package:
```{r message=FALSE, warning=FALSE}
fit <- fitSSM(model, inits = log(c(0.001, 0001, 0001)), method = "BFGS")
outKFS <- KFS(fit$model, smoothing = c("state", "mean", "disturbance"))
Expand All @@ -299,13 +301,13 @@ Extracting the maximum likelihood estimate of the state disturbance variance:
(Q <- fit$model$Q)
```

Extracting the initial values of the smoothed estimates of states:
Extracting the initial values of the smoothed estimates of states using function `coef()` of the KFAS package:
```{r message=FALSE, warning=FALSE}
smoothEstStat <- coef(outKFS)
(initSmoothEstStat <- smoothEstStat[1,])
```

Extracting the values for trend (stochastic level + slope):
Extracting the values for trend (stochastic level + slope) using function `signal()` of the KFAS package:
```{r message=FALSE, warning=FALSE}
trend <-signal(outKFS, states = "trend")$signal
head(trend, 24)
Expand All @@ -330,7 +332,7 @@ legend("topleft",leg = "stochastic slope",
```

Extracting auxiliary irregular residuals (non-standardised):
Extracting auxiliary irregular residuals (non-standardised) using function `residuals` of the KFAS package:
```{r message=FALSE, warning=FALSE}
irregResid <- residuals(outKFS, "pearson")
```
Expand All @@ -344,7 +346,7 @@ title(main = "Figure 3.3. Irregular component of stochastic trend model", cex.ma
legend("topright",leg = "irregular",cex = 0.5, lty = 2, horiz = T)
```

Calculating the diagnostic for one-step-ahead prediction residuals (standardised):
Extracting one-step-ahead prediction residuals (standardised) using function `rstandard()` of the KFAS package and calculating diagnostic for these residuals using own defined functions `qStatistic`, `rStatistic`, `hStatistic` and `nStatistic`:
```{r message=FALSE, warning=FALSE}
predResid <- rstandard(outKFS)
qStat <- qStatistic(predResid, k, w)
Expand All @@ -353,7 +355,7 @@ hStat <- hStatistic(predResid, d)
nStat <- nStatistic(predResid, d)
```

Showing Table 3.2 Diagnostic tests for the local linear trend model applied to the log of the UK drivers KSI:
Showing Table 3.2 Diagnostic tests for the local linear trend model applied to the log of the UK drivers KSI using own defined function `dTable`:
```{r message=FALSE, warning=FALSE}
title = "Table 3.2 Diagnostic tests for the local linear trend model applied to \n
the log of the UK drivers KSI"
Expand Down
26 changes: 14 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@

This repository provides code in R reproducing examples of the states space models presented in book ["An Introduction to State Space Time Series Analysis"](http://www.ssfpack.com/CKbook.html) by Jacques J.F. Commandeur and Siem Jan Koopman.

![](Figures/cover.jpg)
![](Figures/CKbook.png)

The code is provided in file "SSM.R"" and split into sections coresponding to the following parts of the book:
The repository uses extensively the [package KFAS](https://cran.r-project.org/web/packages/KFAS/index.html) of Jouni Helske which includes computationally efficient functions for Kalman filtering, smoothing,forecasting, and simulation of multivariate exponential family state space models. Additionally, some own functions has been created to facilitate the calculation and presentation of diagnostics.

The code is provided in file "SSM.R"" and split into sections corresponding to the following parts of the book:

- Introduction
- Chapter 2 The Local Level Model
Expand All @@ -15,18 +17,18 @@ The code is provided in file "SSM.R"" and split into sections coresponding to th
- Chapter 8 General Treatment of Univariate State Space Models
- Chapter 9 Multivariate Time Series Analysis

In R Studio, each section of the code can be executed with keys CTRL+ALT+T, after placing a coursor in that section. Please make sure to execute the first section of the code including all the functions that are used by the other sections of the code.
In R Studio, each section of the code can be executed with keys CTRL+ALT+T, after placing a cursor in that section. Please make sure to execute the first section of the code including the own defined functions that are used by the other sections of the code.

Below, the code of the stochastic level and slope model of chapter 3 is shown as an example.

Loading data:
Loading data on the UK drivers killed or seriously injured (KSI):

``` r
dataUKdriversKSI <- log(read.table("UKdriversKSI.txt")) %>%
ts(start = 1969, frequency = 12)
```

Defining the model:
Defining the model using function `SSModelof()` of the KFAS package:

``` r
model <- SSModel(dataUKdriversKSI ~ SSMtrend(degree = 2,
Expand Down Expand Up @@ -62,7 +64,7 @@ Providing the number of diffuse initial values in the state:
d <- q <- 2
```

Defining the number of estimated hyperparameters (i.e. disturbance variances):
Defining the number of estimated hyperparameters (two state disturbance variances + irregular disturbance variance):

``` r
w <- 3
Expand All @@ -86,7 +88,7 @@ Providing the number of observations:
n <- 192
```

Fitting the model:
Fitting the model using function `fitSSM` and extracting the output using function `KFS` of the KFAS package:

``` r
fit <- fitSSM(model, inits = log(c(0.001, 0001, 0001)), method = "BFGS")
Expand Down Expand Up @@ -128,7 +130,7 @@ Extracting the maximum likelihood estimate of the state disturbance variance:
#> [2,] 0.0000000 2.774437e-09
```

Extracting the initial values of the smoothed estimates of states:
Extracting the initial values of the smoothed estimates of states using function `coef()` of the KFAS package:

``` r
smoothEstStat <- coef(outKFS)
Expand All @@ -137,7 +139,7 @@ smoothEstStat <- coef(outKFS)
#> 7.4157359290 0.0002893677
```

Extracting the values for trend (stochastic level + slope):
Extracting the values for trend (stochastic level + slope) using function `signal()` of the KFAS package:

``` r
trend <-signal(outKFS, states = "trend")$signal
Expand Down Expand Up @@ -174,7 +176,7 @@ legend("topleft",leg = "stochastic slope",

![](Figures/unnamed-chunk-17-1.png)

Extracting auxiliary irregular residuals (non-standardised):
Extracting auxiliary irregular residuals (non-standardised) using function `residuals` of the KFAS package:

``` r
irregResid <- residuals(outKFS, "pearson")
Expand All @@ -191,7 +193,7 @@ legend("topright",leg = "irregular",cex = 0.5, lty = 2, horiz = T)

![](Figures/unnamed-chunk-19-1.png)

Calculating the diagnostic for one-step-ahead prediction residuals (standardised):
Extracting one-step-ahead prediction residuals (standardised) using function `rstandard()` of the KFAS package and calculating diagnostic for these residuals using own defined functions `qStatistic`, `rStatistic`, `hStatistic` and `nStatistic`:

``` r
predResid <- rstandard(outKFS)
Expand All @@ -201,7 +203,7 @@ hStat <- hStatistic(predResid, d)
nStat <- nStatistic(predResid, d)
```

Showing Table 3.2 Diagnostic tests for the local linear trend model applied to the log of the UK drivers KSI:
Showing Table 3.2 Diagnostic tests for the local linear trend model applied to the log of the UK drivers KSI using own defined function `dTable`:

``` r
title = "Table 3.2 Diagnostic tests for the local linear trend model applied to \n
Expand Down

0 comments on commit 60777bf

Please sign in to comment.