# Examples of fitting smooth trend DFA models

#### 2021-09-28

In addition to fitting conventional DFA models with trends modeled as random walks (or ARMA processes), we can also construct models where underlying trends are treated as smooth trends (B-splines, P-splines, or Gaussian processes).

Let’s load the necessary packages:

library(bayesdfa)
library(ggplot2)
library(dplyr)
library(rstan)
chains = 1
iter = 10

## Data simulation

The sim_dfa function normally simulates loadings $$\sim N(0,1)$$, but here we will simulate time series that are more similar with loadings $$\sim N(1,0.1)$$

set.seed(1)
s = sim_dfa(num_trends = 1, num_years = 1000, num_ts = 4,
loadings_matrix = matrix(nrow = 4, ncol = 1, rnorm(4 * 1,
1, 0.1)), sigma=0.05)
matplot(t(s\$y_sim), type="l")

## Comparing approaches

All of the smooth trend methods are flexible and able to capture the wiggliness of latent trends. Based on our experience, the B-spline and P-spline models will generally fit faster than the Gaussian predicitve process models (because they omit a critical matrix inversion step). The full rank Gaussian process models tend to be faster than the predictive process models. All of these approaches can be compared using cross validation, or similar predictive performance criterion.