simFM: Simulate Dynamic Factor Models.

View source: R/SimFM.r

simFMR Documentation

Simulate Dynamic Factor Models.

Description

Simulate data from a linear Gaussian state-space model (latent factor model), with measurement equation

\bm{x}_t = \bm{\Lambda} \bm{f}_{t} + \bm{\xi}_t,\quad \bm{\xi}_t \sim \mathcal{N}(\bm{\mu}, \bm{\Sigma}_{\xi}),

and transition equation

\bm{f}_t = \sum_{p=1}^P\bm{\Phi}_p \bm{f}_{t-p} + \bm{\epsilon}_t,\quad \bm{\epsilon}_t \sim \mathcal{N}(\bm{0}, \bm{\Sigma}_{f}).

for t = 1, ..., T, as is used in, among others, \insertReffranjic2024nowcastingTwoStepSDFM.

Usage

simFM(
  no_of_obs,
  no_of_vars,
  no_of_factors,
  loading_matrix,
  meas_error_mean,
  meas_error_var_cov,
  trans_error_var_cov,
  trans_var_coeff,
  factor_lag_order,
  delay = NULL,
  quarterfy = FALSE,
  quarterly_variable_ratio = 0,
  corr = FALSE,
  beta_param = Inf,
  seed = 20022024,
  burn_in = 1000,
  rescale = TRUE,
  starting_date = NULL,
  check_stationarity = FALSE,
  stationarity_check_threshold = 1e-05,
  parallel = FALSE
)

Arguments

no_of_obs

Integer number of observations.

no_of_vars

Integer number of Variables.

no_of_factors

Integer number of factors.

loading_matrix

Numeric (no_of_vars \times no_of_factors) loading matrix.

meas_error_mean

Numeric vector of the means of the measurement errors.

meas_error_var_cov

Numeric (no_of_vars \times no_of_vars) variance-covariance matrix of the measurement errors.

trans_error_var_cov

Numeric (no_of_factors \times no_of_factors) variance-covariance matrix of the transition errors.

trans_var_coeff

Either a list of length max_factor_lag_order with each entry a numeric (no_of_factors \times no_of_factors) VAR coefficient matrix or a matrix of dimensions (no_of_factors \times (no_of_factors * max_factor_lag_order)) holding the VAR coefficients of the factor VAR process in each (no_of_factors \times no_of_factors) block.

factor_lag_order

Integer order of the VAR process in the transition equation.

delay

Integer vector of delays imposed onto the end of the data (ragged edges).

quarterfy

Logical, whether or not some of the data should be aggregated to quarterly representations.

quarterly_variable_ratio

Ratio of variables ought to be quarterfied.

corr

Logical, whether or not the measurement error should be randomly correlated inside the function using a random correlation matrix with off-diagonal elements governed by a beta-distribution.

beta_param

Parameter of the beta-distribution governing the off-diagonal elements of the variance-covariance matrix of the measurement error.

seed

32-bit unsigned integer seed for all random processes inside the function.

burn_in

Integer burn-in period of the simulated data ought to be discarded at the beginning of the sample.

rescale

Logical, whether or not the variance of the measurement error should be rescaled by the common component to equalise the signal-to-noise ratio.

starting_date

A date type object indicating the start of the dataset. If NULL (default), the function returns matrices with observations along the second dimension (i.e., time in columns). If specified, the function treats the data as a time series and returns a zoo object.

check_stationarity

Logical, whether or not the stationarity properties of the factor VAR process should be checked.

stationarity_check_threshold

Threshold of the stationarity check for when to deem an eigenvalue numerically negative.

parallel

Logical, make use of Eigen internal parallel matrix operations.

Details

The delay vector indicates the number of observations at the end of the sample that will be set to NA for each variable. Here, delay refers to the number of months for monthly data and the number of quarters for quarterly data. For example, consider delay <- c(1, 1) and assume the variable with index 1 will be quarterfied. In that case, the variable with index 1 will be delayed by 1 quarter, i.e., it will be missing 3 observations at the end of the panel. The variable with index 2 will be delayed by 1 month, i.e., it will be missing 1 observation at the end of the panel. This convention differs from the delay object of the SimulData class this function returns. There, delay represents the number of months since the most recent publication. For monthly variables, these values coincide, but for quarterly variables they are inherently different.

If quarterfy = TRUE, floor(quarterly_variable_ratio * no_of_vars) variables will be aggregated to a quarterly representation using the geometric mean according to \insertRefMariano2003new_coincidentTwoStepSDFM.

If corr = TRUE, the matrix meas_error_var_cov is internally replaced by a random variance-covariance matrix: \tilde{\bm{\Sigma}}:=\bm{S}\bm{R}\bm{S}, where \bm{S} is a diagonal matrix with entries equal to sqrt(diag(meas_error_var_cov)) and \bm{R} is a random correlation matrix. \bm{R} is drawn according to \insertReflewandowski2009generatingTwoStepSDFM (see also https://stats.stackexchange.com/questions/2746/how-to-efficiently-generate-random-positive-semidefinite-correlation-matrices). The parameter beta_param governs the degree of cross-correlation of the off-diagonal elements. For more information see the literature cited above.

The random draws of the fundamental error terms are drawn within the ⁠C++⁠ backend. Therefore, seed must be provided and set.seed() will not guarantee reproduceability.

Value

Returns a SimulData containing the following elements:

data

If starting_date is provided, a zoo object, else, a (no_of_vars \times no_of_obs) numeric matrix holding the simulated data.

factors

If starting_date is provided, a zoo object, else a (no_of_factors \times no_of_obs) numeric matrix holding the simulated latent factors.

trans_var_coeff

Numeric (no_of_factors \times (no_of_factors * factor_lag_order)) factor VAR coefficient matrix.

loading_matrix

Numeric factor loading matrix.

meas_error

If starting_date is provided, a zoo object, else a (no_of_vars \times no_of_obs) numeric matrix holding the fundamental measurement errors.

meas_error_var_cov

Numeric measurement error variance-covariance matrix.

trans_error_var_cov

Numeric transition error variance-covariance matrix.

frequency

Integer vector of variable frequencies.

delay

Integer vector of variable delays, measured as the number of months since the latest available observation.

Author(s)

Domenic Franjic

References

\insertRef

Mariano2003new_coincidentTwoStepSDFM

\insertRef

lewandowski2009generatingTwoStepSDFM

\insertRef

franjic2024nowcastingTwoStepSDFM

Examples

seed <- 02102025
set.seed(seed)
no_of_obs <- 100
no_of_vars <- 50
no_of_factors <- 3
trans_error_var_cov <- diag(1, no_of_factors)
loading_matrix <- matrix(round(rnorm(no_of_vars * no_of_factors)), no_of_vars, no_of_factors)
meas_error_mean <- rep(0, no_of_vars)
meas_error_var_cov <- diag(1, no_of_vars)
trans_var_coeff <- cbind(diag(0.5, no_of_factors), -diag(0.25, no_of_factors))
factor_lag_order <- 2
delay <- c(floor(rexp(no_of_vars, 1)))
quarterfy <- FALSE
quarterly_variable_ratio  <- 0
corr <- TRUE
beta_param <- 2
burn_in <- 999
starting_date <- "1970-01-01"
rescale <- TRUE
check_stationarity <- TRUE
stationarity_check_threshold <- 1e-10
factor_model <- simFM(no_of_obs = no_of_obs, no_of_vars = no_of_vars,
                      no_of_factors = no_of_factors, loading_matrix = loading_matrix,
                      meas_error_mean = meas_error_mean, 
                      meas_error_var_cov = meas_error_var_cov,
                      trans_error_var_cov = trans_error_var_cov, 
                      trans_var_coeff = trans_var_coeff,
                      factor_lag_order = factor_lag_order, delay = delay, 
                      quarterfy = quarterfy, 
                      quarterly_variable_ratio  = quarterly_variable_ratio, corr = corr,
                      beta_param = beta_param, seed = seed, burn_in = burn_in, 
                      starting_date = starting_date, rescale = rescale, 
                      check_stationarity = check_stationarity,
                      stationarity_check_threshold = stationarity_check_threshold)
print(factor_model)
spca_plots <- plot(factor_model)
spca_plots$`Factor Time Series Plots`
spca_plots$`Loading Matrix Heatmap`
spca_plots$`Meas. Error Var.-Cov. Matrix Heatmap`
spca_plots$`Meas. Error Var.-Cov. Eigenvalue Plot`
spca_plots$`Data Var.-Cov. Matrix Heatmap`
spca_plots$`Data Var.-Cov. Eigenvalue Plot`


TwoStepSDFM documentation built on May 19, 2026, 9:07 a.m.