\[ \begin{aligned} \text{consumption}\quad C_t &= \beta_0 + \alpha_1 P_t + \alpha_2 P_{t-1} + \alpha_3\bigl(W^p_t + W^g_t\bigr) + \varepsilon_{1t},\\ \text{investment}\quad I_t &= \beta_0 + \beta_1 P_t + \beta_2 P_{t-1} + \beta_3 K_{t-1} + \varepsilon_{2t},\\ \text{private wages}\quad W^p_t &= \gamma_0 + \gamma_1 Y_t + \gamma_2 Y_{t-1} + \gamma_3 A_t + \varepsilon_{3t}. \end{aligned} \]
The \(\alpha\)’s, \(\beta\)’s, and \(\gamma\)’s are the coefficients of the model. \(P_t\) is the private profits, \(W^p_t\) is the private wages, \(W^g_t\) is the government wages, \(K_{t-1}\) is the capital stock at time \(t-1\), and \(A_t\) is a time trend. The \(\varepsilon_{it}\) are stochastic error terms.
\[ \begin{aligned} \text{equilibrium demand}\quad Y_t &= C_t + I_t + G_t \end{aligned} \]
\(Y_t\) is the equilibrium demand, \(C_t\) is the consumption, \(I_t\) is the investment, and \(G_t\) is the government spending.
\[ \begin{aligned} \text{equilibrium demand}\quad Y_t &= C_t + I_t + G_t\\ \text{private profits}\quad P_t &= Y_t - T_t - W^p_t\\ \text{capital stock}\quad K_t &= K_{t-1} + I_t \end{aligned} \]
Here, \(T_t\) represents taxes, \(K_t\) denotes the capital stock at time \(t\), and \(G_t\) signifies government spending.
To estimate and forecast this model, we need to specify the equations, the exogenous variables, and the date ranges for estimation and forecasting.
equations <- "consumption ~ profits + profits.L(1) + total_wages,
investment ~ profits + profits.L(1) + capital_stock.L(1),
wages ~ gdp + gdp.L(1) + time_trend,
gdp == (n_consumption/n_gdp)*consumption + (n_investment/n_gdp)*investment + (n_government/n_gdp)*government,
profits == (n_gdp/n_profits)*gdp - (n_taxes/n_profits)*taxes - (n_wages/n_profits)*wages,
capital_stock == 1.001*capital_stock.L(1) + 0.0002*investment,
total_wages == (n_wages/n_total_wages)*wages + (n_government_wages/n_total_wages)*government_wages"
exogenous_variables <- c("government", "government_wages", "taxes", "time_trend")The equations string contains the model equations, while
exogenous_variables lists the exogenous variables. The
.L(1) notation indicates a lag of one period. For further
details on the syntax, please refer to the equation syntax documentation.
The system_of_equations function is used to create a
system of equations object, which can then be used for estimation and
forecasting.
library(koma)
sys_eq <- system_of_equations(
equations = equations,
exogenous_variables = exogenous_variables
)
print(sys_eq)
##
## ── System of Equations ─────────────────────────────────────────────────────────
## consumption ~ constant + profits + profits.L(1) + total_wages
## investment ~ constant + profits + profits.L(1) + capital_stock.L(1)
## wages ~ constant + gdp + gdp.L(1) + time_trend
## gdp == (n_consumption/n_gdp) * consumption + (n_investment/n_gdp) * investment + (n_government/n_gdp) * government
## profits == (n_gdp/n_profits) * gdp-(n_taxes/n_profits) * taxes-(n_wages/n_profits) * wages
## capital_stock == 1.001 * capital_stock.L(1) + 0.0002 * investment
## total_wages == (n_wages/n_total_wages) * wages + (n_government_wages/n_total_wages) * government_wagesNow, we need to specify the dates for estimation and forecasting.
We will use the klein dataset, which contains the
required variables for the model. This dataset is a list of standard
time series (ts) objects, where each object corresponds to
a variable in the model. You can explore the dataset by using the
?klein command to view its documentation and structure.
To prepare the dataset for use with the koma package, we
need to convert the ts objects to the ets
format using the as_ets function.
The model is estimated in growth rates. The klein
dataset is in levels. We use the ets to tell the model that
the data is in levels and how to convert it to growth rates.
ets is generalized to accept any additional attributes.
Below we use this feature to save the series’ value_type (real or
nominal).
# Some series in the klein dataset are nominal. We need to deflate them using the GDP deflator.
klein$profits <- klein$n_profits / (klein$d_gdp / 100)
klein$capital_stock <- klein$n_capital_stock / (klein$d_gdp / 100)
klein$wages <- klein$n_wages / (klein$d_gdp / 100)
klein$government_wages <- klein$n_government_wages / (klein$d_gdp / 100)
klein$taxes <- klein$n_taxes / (klein$d_gdp / 100)
real_series <- c(
"gdp", "consumption", "investment", "profits", "capital_stock",
"government", "wages", "government_wages", "taxes"
)
real_ts_data <- lapply(real_series, function(x) {
as_ets(klein[[x]],
series_type = "level", method = "diff_log", value_type = "real"
)
})
names(real_ts_data) <- real_series
real_ts_data$total_wages <- real_ts_data$government_wages + real_ts_data$wages
# net exports can be negative, we thus want to use percentage change instead of log difference
real_ts_data$net_exports <- as_ets(klein$net_exports,
series_type = "level", method = "percentage", value_type = "real"
)
# To compute the weights in the model, we include the nominal series in the dataset as well.
nominal_series <- c(
"n_gdp", "n_investment", "n_government", "n_government_wages",
"n_taxes", "n_profits", "n_wages", "n_consumption"
)
nominal_ts_data <- lapply(nominal_series, function(x) {
as_ets(klein[[x]],
series_type = "level", method = "diff_log", value_type = "nominal"
)
})
names(nominal_ts_data) <- nominal_series
nominal_ts_data$n_total_wages <- nominal_ts_data$n_government_wages + nominal_ts_data$n_wages
# Combining the real and nominal series
ts_data <- c(real_ts_data, nominal_ts_data)
# Adding the time trend
ts_data$time_trend <- ets(seq_len(length(klein$taxes)),
start = stats::start(ts_data$taxes), frequency = 4,
series_type = "level", method = "none", value_type = NA
)With the data prepared, we can now proceed to estimate the model. Before forecasting, however, we need to truncate the endogenous variables to ensure they only include data up to the forecast start date.
The names of the list elements should match the variable names used
in the equations. Now, we can estimate the model using the
estimate function.
estimates <- estimate(
sys_eq,
ts_data = ts_data,
dates = dates
)
##
## ── Gibbs Sampler Settings ──────────────────────────────────────────────────────
## ── System Wide Settings ──
## • Number of draws (`ndraws`): 2000
## • Burn-in ratio (`burnin_ratio`): 0.5
## • Burn-in (`burnin`): 1000
## • Store frequency (`nstore`): 1
## • Number of saved draws (`nsave`): 1000
## • Tau (`tau`): 1.1
##
##
## ── Estimation ──────────────────────────────────────────────────────────────────
print(estimates)
##
## ── Estimates ───────────────────────────────────────────────────────────────────
## consumption ~ - 0.12 + 0.13 * profits - 0.03 * profits.L(1) + 1.12 * total_wages
## investment ~ - 0.86 + 0.39 * profits + 0.16 * profits.L(1) + 1.56 * capital_stock.L(1)
## wages ~ - 0.64 + 0.96 * gdp + 0.43 * gdp.L(1) + 0 * time_trend
## gdp == (n_consumption/n_gdp) * consumption + (n_investment/n_gdp) * investment + (n_government/n_gdp) * government
## profits == (n_gdp/n_profits) * gdp - (n_taxes/n_profits) * taxes - (n_wages/n_profits) * wages
## capital_stock == 1.001 * capital_stock.L(1) + 0.0002 * investment
## total_wages == (n_wages/n_total_wages) * wages + (n_government_wages/n_total_wages) * government_wagessummary(estimates)
##
## =================================================================
## consumption investment wages
## -----------------------------------------------------------------
## constant -0.12 -0.86 -0.64
## [-0.32; 0.06] [-1.83; 0.03] [-1.28; -0.01]
## profits.L(1) -0.03 0.16
## [-0.05; -0.00] [ 0.08; 0.24]
## profits 0.13 0.39
## [ 0.09; 0.16] [ 0.21; 0.58]
## total_wages 1.12
## [ 0.97; 1.26]
## capital_stock.L(1) 1.56
## [ 0.50; 2.65]
## gdp.L(1) 0.43
## [ 0.31; 0.56]
## time_trend 0.00
## [-0.00; 0.01]
## gdp 0.96
## [ 0.81; 1.15]
## =================================================================
## Posterior mean (90% credible interval: [5.0%, 95.0%])
## Estimation period: 1990 Q1 - 2020 Q4To see how to run the estimation in parallel, refer to the parallel estimation vignette.
To forecast the model, we can use the forecast function.
This function will generate forecasts for the specified date range.
forecasts <- forecast(
estimates,
dates = dates
)
##
## ── Forecast ────────────────────────────────────────────────────────────────────
## Warning: ! Forecast draws raised warnings:
## • 1000 of 1000 draws: Identity "capital_stock" could not be checked.
## • 1 of 1000 draws: Identity "gdp" not satisfied at horizon "11, 12".
## • 1 of 1000 draws: Identity "profits" not satisfied at horizon "7, 8, 9, 10,
## 11, 12".
## • 1 of 1000 draws: Identity "total_wages" not satisfied at horizon "10, 11,
## 12".
print(forecasts)
## <koma_ts>
## attributes:
## series_type: list[11]
## method: list[11]
## value_type: list[11]
## anker: list[11]
##
## series:
## consumption investment wages gdp profits
## 2021 Q1 -1.3654 -1.0003 -0.3861 -0.8718 -7.6481
## 2021 Q2 0.2386 3.0421 0.0309 0.5201 4.5010
## 2021 Q3 1.4052 6.3084 1.8710 2.0141 13.7587
## 2021 Q4 14.0767 27.6797 13.4137 14.4263 98.3033
## 2022 Q1 76.7002 136.2135 72.3942 75.8827 511.8596
## 2022 Q2 398.7779 692.1419 374.9925 392.3388 2645.5496
## 2022 Q3 2060.2266 3559.4974 1935.5124 2024.4129 13648.2366
## 2022 Q4 10631.0658 18351.0683 9985.3735 10443.1684 70401.1437
## 2023 Q1 54830.4542 94630.8088 51498.2328 53857.4707 363063.9316
## 2023 Q2 282748.7814 487976.7603 265564.4776 277727.8187 1872206.1308
## 2023 Q3 1458036.8439 2516308.6652 1369422.2427 1432141.9543 9654280.7502
## 2023 Q4 7518552.7297 12975652.4966 7061599.0357 7385018.8612 49783495.1183
## capital_stock total_wages government government_wages taxes
## 2021 Q1 2.5034 -0.4191 1.2758 -0.5982 0.8997
## 2021 Q2 2.5065 -0.0086 -1.0791 -0.2234 9.1809
## 2021 Q3 2.5103 1.7122 -0.3873 0.8500 0.3182
## 2021 Q4 2.5183 11.1725 -0.0708 -0.9966 4.6251
## 2022 Q1 2.5481 60.9985 -0.8533 -0.8763 3.5975
## 2022 Q2 2.6891 316.4554 -0.3752 -1.3801 -1.3602
## 2022 Q3 3.4037 1634.5538 0.3883 0.4571 -5.3551
## 2022 Q4 7.0773 8432.4440 1.3228 0.5938 -6.2183
## 2023 Q1 26.0105 43488.8555 1.2402 0.8097 -6.4787
## 2023 Q2 123.6319 224261.4691 0.7229 0.9474 -2.6579
## 2023 Q3 627.0172 1156436.6826 1.3819 1.4833 -2.8205
## 2023 Q4 3222.7748 5963311.0629 0.8931 1.2959 -2.1251
## time_trend
## 2021 Q1 205
## 2021 Q2 206
## 2021 Q3 207
## 2021 Q4 208
## 2022 Q1 209
## 2022 Q2 210
## 2022 Q3 211
## 2022 Q4 212
## 2023 Q1 213
## 2023 Q2 214
## 2023 Q3 215
## 2023 Q4 216