Title: | Consistent Economic Trend Cycle Decomposition |
---|---|
Description: | Determining potential output and the output gap - two inherently unobservable variables - is a major challenge for macroeconomists. 'sectorgap' features a flexible modeling and estimation framework for a multivariate Bayesian state space model identifying economic output fluctuations consistent with subsectors of the economy. The proposed model is able to capture various correlations between output and a set of aggregate as well as subsector indicators. Estimation of the latent states and parameters is achieved using a simple Gibbs sampling procedure and various plotting options facilitate the assessment of the results. For details on the methodology and an illustrative example, see Streicher (2024) <https://www.research-collection.ethz.ch/handle/20.500.11850/653682>. |
Authors: | Sina Streicher [aut, cre] |
Maintainer: | Sina Streicher <[email protected]> |
License: | GPL-3 |
Version: | 0.1.0 |
Built: | 2025-02-21 05:30:24 UTC |
Source: | https://github.com/sinast3000/sectorgap |
Computes estimation results for the MCMC sampling output for a specific HPDI and evaluation function (e.g. mean or median).
compute_mcmc_results( model, settings, mcmc, data, HPDIprob = NULL, fit = NULL, ... )
compute_mcmc_results( model, settings, mcmc, data, HPDIprob = NULL, fit = NULL, ... )
model |
state space model object, returned by the function
|
settings |
list with model setting, in the format returned by the
function |
mcmc |
list with draws of parameters and states (including burnin phase) |
data |
list with at least two named components: |
HPDIprob |
probability of highest posterior density interval, optional
if |
fit |
(optional) an object of class |
... |
additional arguments (in case |
If fit
is supplied, the arguments
model, settings, mcmc
will be taken from this object.
An object of class ss_fit
.
A dataset containing quarterly Swiss economic data, sourced on November 20, 2023.
data_ch
data_ch
A list object with two lists. The first list cotains all untransformed endogenous variables:
Gross domestic product at 2020 prices, in million
value added in sector A: Goods-producing industries, at 2020 prices in million
value added in sector B: Service industries, at 2020 prices in million
value added in sector C: Government and adjustments,at 2020 prices in million
expenditure side sector i: Total consumption, at 2020 prices in million
expenditure side sector ii: Investment, at 2020 prices in million
expenditure side sector iii: Exports, at 2020 prices in million
expenditure side sector iv: Imports, at 2020 prices in million
full-time equivalent empoyment in sector A: Goods-producing industries, in thousand
full-time equivalent empoyment in sector B: Service industries, in thousand
full-time equivalent empoyment in government sector, in thousand
full-time equivalent empoyment, in thousand
ILO unemployment rate, in percent
consumer price inflation, year on year in percent
KOF Swiss Economic Institute, ETH Zurich, Switzerland
Defines a state space model for the provided settings and data.
define_ssmodel(settings, data)
define_ssmodel(settings, data)
settings |
list with model setting, in the format returned by the
function |
data |
list with at least two named components: |
data
is preferably the output of funtion prepare_data
.
A state space model object of class ss_model
, which consists
of an object returned by the function SSModel
of
the package KFAS
and in addition a list item called names
which contains information on the parameters to be estimated.
data("data_ch") settings <- initialize_settings() data <- prepate_data( settings = settings, tsl = data_ch$tsl, tsl_n = data_ch$tsl_n ) model <- define_ssmodel( settings = settings, data = data )
data("data_ch") settings <- initialize_settings() data <- prepate_data( settings = settings, tsl = data_ch$tsl, tsl_n = data_ch$tsl_n ) model <- define_ssmodel( settings = settings, data = data )
Estimates the parameters and states of a multi-dimensional state space model by Bayesian methods using a Gibbs sampling procedure.
estimate_ssmodel( model, settings, data, prior = initialize_prior(model), R = 10000, burnin = 0.5, thin = 1, HPDIprob = 0.68, fit = NULL )
estimate_ssmodel( model, settings, data, prior = initialize_prior(model), R = 10000, burnin = 0.5, thin = 1, HPDIprob = 0.68, fit = NULL )
model |
state space model object, returned by the function
|
settings |
list with model setting, in the format returned by the
function |
data |
list with at least two named components: |
prior |
list of matrices, each list item corresponds to one endogenous
variable. See |
R |
number of draws, the default is |
burnin |
share of draws as burnin period, the default is |
thin |
thinning parameter defining how many draws are discarded.
|
HPDIprob |
probability of highest posterior density interval, the
default is |
fit |
already fitted object of class |
If fit
is supplied, the function will continue drawing
R
additional repetitions. In this case, all input variables except
for fit
and R
are ignored.
An object of class ss_fit
.
data("data_ch") settings <- initialize_settings() data <- prepate_data( settings = settings, tsl = data_ch$tsl, tsl_n = data_ch$tsl_n ) model <- define_ssmodel( settings = settings, data = data ) prior <- initialize_prior( model = model, settings = settings ) fit <- estimate_ssmodel( model = model, settings = settings, data = data, prior = prior, R = 100 )
data("data_ch") settings <- initialize_settings() data <- prepate_data( settings = settings, tsl = data_ch$tsl, tsl_n = data_ch$tsl_n ) model <- define_ssmodel( settings = settings, data = data ) prior <- initialize_prior( model = model, settings = settings ) fit <- estimate_ssmodel( model = model, settings = settings, data = data, prior = prior, R = 100 )
Initializes the prior distributions.
initialize_prior(model, settings, lambda_d = 100, lambda_t = 100, df = 6)
initialize_prior(model, settings, lambda_d = 100, lambda_t = 100, df = 6)
model |
state space model object, returned by the function
|
settings |
list with model setting, in the format returned by the
function |
lambda_d |
drift smoothing constant (default: 100) |
lambda_t |
trend smoothing constant (default: 100) |
df |
degrees of freedom for inverse gamma distributions |
All loadings and autoregressive parameters are assumed to be normal with mean zero and variance 1000.
All variance parameters are assumed to be inverse gamma distributed. The cycle variance has prior mean 1, and the trend variances have prior mean 1/100.
The normal distribution is parametrized via mean and variance.
the inverse gamma distribution is parametrized degrees of freedom
nu
and scale s
.
The mean of the inverse gamma distribution is given by
beta / (alpha - 1) = beta / 2 = s
, where
s = 2 beta, nu = 2 alpha
.
A data frame with one row per parameter and the following columns:
variable |
name of endogneous variable of equation |
parameter_name |
name of parameter |
par1 |
first parameter of specified distribution, mean for normal parameters and scale for inverse gamma parameters |
par2 |
second parameter of specified distribution, variance for normal parameters and degrees of freedom for inverse gamma parameters |
ini |
initial value for Gibbs sampler, i.e. mean of distribution given
|
distribution |
name of prior distribution |
data("data_ch") settings <- initialize_settings() data <- prepate_data( settings = settings, tsl = data_ch$tsl, tsl_n = data_ch$tsl_n ) model <- define_ssmodel( settings = settings, data = data ) prior <- initialize_prior( model = model, settings = settings )
data("data_ch") settings <- initialize_settings() data <- prepate_data( settings = settings, tsl = data_ch$tsl, tsl_n = data_ch$tsl_n ) model <- define_ssmodel( settings = settings, data = data ) prior <- initialize_prior( model = model, settings = settings )
Initializes settings with a basic example.
initialize_settings( FUN_transform = function(x) 100 * log(x), FUN_transform_inv = function(x) exp(x/100), DFUN_transform_inv = function(x) 1/100 * exp(x/100) )
initialize_settings( FUN_transform = function(x) 100 * log(x), FUN_transform_inv = function(x) exp(x/100), DFUN_transform_inv = function(x) 1/100 * exp(x/100) )
FUN_transform |
transformation function, the default is
|
FUN_transform_inv |
inverse transformation function, the default is
|
DFUN_transform_inv |
derivative of inverse transformation function, the
default is |
A nested list with settings for the following groups:
agg |
settings for the aggregate variable |
group1 |
settings for |
group2 |
settings for |
subgroup1 |
settings for |
agggroup |
settings for a group of variables that all load on the same variable |
misc |
settings for variables that require individual settings |
Each group contains at least the following list items:
trend |
|
cycle |
|
transform |
logical indicating if the transformation function should be applied to the variable or group of variables |
variable |
variable name(s) |
variable_label |
variable label(s) |
label |
label of group |
The blocks group1, group2, subgroup1
additionally contain the
following list items:
corr |
|
load_name |
name of the variable that all variables in the group load
(for |
load_lag |
lags of the of the variable that all variables in the group
load (for |
constr_drift |
logical indicating if constraints for the drifts should be enforced |
constr_trends |
logical indicating if constraints for the trends should be enforced |
constr_trends_linear |
logical indicating if constraints for the
trends are linear or nonlinear, the default is |
variable_neg |
variable names that are negative and thus need to be subtracted when constructing weights |
The block subgroup1
additionally contain the
following list item:
match_group1 |
a character vector of the same length as
|
Checks if settings are a valid object of class settings
.
is.settings(x, dfl = NULL, return.logical = FALSE)
is.settings(x, dfl = NULL, return.logical = FALSE)
x |
settings object |
dfl |
list of data frames, returned by function |
return.logical |
If |
A logical value or nothing, depending on the value of return.logical
.
Creates a set of time series, density, or trace plots.
## S3 method for class 'ss_fit' plot( x, plot_type = "timeseries", estimate = "median", data = data, n_col = 3, n_sep = 5, file_path = NULL, title = TRUE, save = FALSE, device = "jpg", width = 10, height = 3, units = "in", highlighted_area = NULL, plot_start = NULL, plot_end = NULL, alpha = 0.05, include_burnin = FALSE, ... )
## S3 method for class 'ss_fit' plot( x, plot_type = "timeseries", estimate = "median", data = data, n_col = 3, n_sep = 5, file_path = NULL, title = TRUE, save = FALSE, device = "jpg", width = 10, height = 3, units = "in", highlighted_area = NULL, plot_start = NULL, plot_end = NULL, alpha = 0.05, include_burnin = FALSE, ... )
x |
object of class |
plot_type |
type of plots, options are
|
estimate |
character specifying the posterior estimate. Valid options
are |
data |
list with at least two named components: |
n_col |
number of columns for grid plots |
n_sep |
increments of x axis ticks in years |
file_path |
file path for plots |
title |
boolean indicating if plots should contain titles |
save |
boolean indicating if plots should be saved, if |
device |
character string with format used in |
width |
plot width in |
height |
plot height in |
units |
units for plot size ( |
highlighted_area |
data frame with two columns called |
plot_start |
start of x axis in years, e.g., |
plot_end |
end of x axis in years, e.g., |
alpha |
cut off value for posterior (only used if
|
include_burnin |
logical indicating if burnin phase should be included
(only used if |
... |
ignored |
nothing
Prepares the required input data, it performs the transformations to the raw data and computes the necessary weights for the constraints.
prepate_data( settings, tsl, tsl_n = NULL, tsl_p = NULL, ts_start = NULL, ts_end = NULL, extend_weights = FALSE )
prepate_data( settings, tsl, tsl_n = NULL, tsl_p = NULL, ts_start = NULL, ts_end = NULL, extend_weights = FALSE )
settings |
list with model setting, in the format returned by the
function |
tsl |
time series list with all untransformed endogenous series |
tsl_n |
time series list with nominal level series for aggregate output
|
tsl_p |
time series list with price series for aggregate output
|
ts_start |
start date, e.g. |
ts_end |
end date, e.g. |
extend_weights |
logical indicating if missing weights at beginning/end of sample should be filled with the last/first available value |
Either tsl_n
or tsl_p
must be supplied.
Weights are forward/backward extended with the first/last value if the supplied time series do not cover the entire period.
A list with five components:
tsm |
multiple time series object with all (transformed) endogeneous variables |
real |
multiple time series object with real series of
|
nominal |
multiple time series object with nominal series of
|
prices |
multiple time series object with price series of
|
weights_growth |
list of multiple time series objects with weights for
the growth constraints, i.e., for series |
weights_level |
list of multiple time series objects with weights for
the non linear level constraints, i.e., for series
|
data("data_ch") settings <- initialize_settings() data <- prepate_data( settings = settings, tsl = data_ch$tsl, tsl_n = data_ch$tsl_n )
data("data_ch") settings <- initialize_settings() data <- prepate_data( settings = settings, tsl = data_ch$tsl, tsl_n = data_ch$tsl_n )
prior
objectPrints the model specifications of an object of class
prior
.
## S3 method for class 'prior' print(x, call = TRUE, check = TRUE, ...)
## S3 method for class 'prior' print(x, call = TRUE, check = TRUE, ...)
x |
object of class |
call |
logical, if |
check |
logical, if |
... |
ignored. |
No return value
settings
objectPrints the model settings.
## S3 method for class 'settings' print(x, call = TRUE, check = TRUE, ...)
## S3 method for class 'settings' print(x, call = TRUE, check = TRUE, ...)
x |
object of class |
call |
logical, if |
check |
logical, if |
... |
ignored. |
No return value
ss_fit
object.Prints the model specifications of an object of class ss_fit
.
## S3 method for class 'ss_fit' print(x, call = TRUE, check = TRUE, ...)
## S3 method for class 'ss_fit' print(x, call = TRUE, check = TRUE, ...)
x |
object of class |
call |
logical, if |
check |
logical, if |
... |
ignored. |
No return value
ss_model
objectPrints the model specifications of an object of class
ss_model
.
## S3 method for class 'ss_model' print(x, call = TRUE, check = TRUE, ...)
## S3 method for class 'ss_model' print(x, call = TRUE, check = TRUE, ...)
x |
object of class |
call |
logical, if |
check |
logical, if |
... |
ignored. |
No return value
Recession periods in Switzerland since 1990.
recessions_ch
recessions_ch
A data frame with two columns:
start date of recession, in quarters
end date of recession, in quarters
Recession periods in the United States since 1960.
recessions_us
recessions_us
A data frame with two columns:
start date of recession, in quarters
end date of recession, in quarters
National Bureau of Economic Research (NBER)
Formats the output series into a tibble in long format and computes contribution series.
transform_results( fit, data, settings, estimate = "median", HPDIprob = 0.68, transformed = TRUE )
transform_results( fit, data, settings, estimate = "median", HPDIprob = 0.68, transformed = TRUE )
fit |
fitted object |
data |
list with at least two named components: |
settings |
list with model setting, in the format returned by the
function |
estimate |
character specifying the posterior estimate. Valid options
are |
HPDIprob |
probability of highest posterior density interval, the
default is |
transformed |
boolean indicating if the transformed series should be used. |
data
is preferably the output of funtion prepare_data
.
A data frame with results in long format.
data("data_ch") settings <- initialize_settings() data <- prepate_data( settings = settings, tsl = data_ch$tsl, tsl_n = data_ch$tsl_n ) model <- define_ssmodel( settings = settings, data = data ) prior <- initialize_prior( model = model, settings = settings ) fit <- estimate_ssmodel( model = model, settings = settings, data = data, prior = prior, R = 100 ) df <- transform_results( fit = fit, data = data, estimate = "median" )
data("data_ch") settings <- initialize_settings() data <- prepate_data( settings = settings, tsl = data_ch$tsl, tsl_n = data_ch$tsl_n ) model <- define_ssmodel( settings = settings, data = data ) prior <- initialize_prior( model = model, settings = settings ) fit <- estimate_ssmodel( model = model, settings = settings, data = data, prior = prior, R = 100 ) df <- transform_results( fit = fit, data = data, estimate = "median" )