# Estimating Klein’s Model I

# Equations of Klein’s Model

## Stochastic equations

\[ \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.

## Equilibrium condition

\[ \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.

## Identities

\[ \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.

# Defining Klein’s Model in R

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.

# Creating the SEM

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_wages

# Defing the Estimation and Forecast Dates

Now, we need to specify the dates for estimation and forecasting.

    dates <- list(
      estimation = list(start = c(1990, 1), end = c(2020, 4)),
      forecast = list(start = c(2021, 1), end = c(2023, 4)),
      dynamic_weights = list(start = c(1990, 1), end = c(2020, 4))
    )

# Preparing the Data

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.

    ?klein

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.

    ts_data[sys_eq$endogenous_variables] <-
      lapply(sys_eq$endogenous_variables, function(x) {
        stats::window(ts_data[[x]], end = c(2020, 4))
      })

# Estimating the Model

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 ──────────────────────────────────────────────────────────────────
    ##
    ## ── ⚠ MCMC Acceptance Probability Warnings ──────────────────────────────────────
    ## • investment: 61.2%
    ## • wages: 60.0%
    ##
    ## ℹ Some acceptance probabilities are outside the recommended range (20%-60%).
    ## Consider revising the equations, tuning each equation's tau, or adjusting your priors.
    print(estimates)
    ##
    ## ── Estimates ───────────────────────────────────────────────────────────────────
    ##   consumption ~  - 0.11  +  0.12 * profits - 0.03 * profits.L(1)  +  1.11 * total_wages
    ##    investment ~  - 0.86  +  0.38 * profits  +  0.15 * profits.L(1)  +  1.57 * capital_stock.L(1)
    ##         wages ~  - 0.63  +  0.94 * gdp  +  0.42 * 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_wages

    summary(estimates)
    ##
    ## ==================================================================
    ##                     consumption     investment      wages
    ## ------------------------------------------------------------------
    ## constant             -0.11           -0.86           -0.63
    ##                     [-0.31;  0.07]  [-1.73; -0.01]  [-1.24; -0.04]
    ## profits.L(1)         -0.03            0.15
    ##                     [-0.05; -0.00]  [ 0.07;  0.24]
    ## profits               0.12            0.38
    ##                     [ 0.09;  0.16]  [ 0.21;  0.55]
    ## total_wages           1.11
    ##                     [ 0.97;  1.26]
    ## capital_stock.L(1)                    1.57
    ##                                     [ 0.54;  2.61]
    ## gdp.L(1)                                              0.42
    ##                                                     [ 0.30;  0.54]
    ## time_trend                                            0.00
    ##                                                     [-0.00;  0.01]
    ## gdp                                                   0.94
    ##                                                     [ 0.79;  1.10]
    ## ==================================================================
    ## Posterior mean (90% credible interval: [5.0%, 95.0%])
    ## Estimation period: 1990 Q1 - 2020 Q4

To see how to run the estimation in parallel, refer to the parallel estimation vignette.

# Forecasting

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 ────────────────────────────────────────────────────────────────────
    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 capital_stock
    ## 2021 Q1     -1.5774    -1.2003 -0.5310 -1.0500 -8.9464        2.5034
    ## 2021 Q2     -0.4905     2.0575 -0.6205 -0.1465  0.0599        2.5063
    ## 2021 Q3     -1.6419     1.1173 -1.0260 -0.9666 -6.1573        2.5090
    ## 2021 Q4     -1.0336     1.5051 -0.7795 -0.4332 -1.9251        2.5118
    ## 2022 Q1     -0.8413     2.2681 -0.4939 -0.3039 -1.5806        2.5148
    ## 2022 Q2     -1.1360     1.8952 -0.5740 -0.4848 -2.7348        2.5177
    ## 2022 Q3     -1.9255     0.4481 -1.2995 -1.1414 -6.4549        2.5203
    ## 2022 Q4     -2.3396    -0.0335 -1.7466 -1.3417 -6.7648        2.5228
    ## 2023 Q1     -2.3892    -0.1311 -1.9157 -1.4071 -6.7796        2.5253
    ## 2023 Q2     -2.0960     0.2219 -1.7716 -1.2377 -5.8912        2.5279
    ## 2023 Q3     -2.3788    -0.4989 -1.8915 -1.4417 -7.5382        2.5303
    ## 2023 Q4     -2.0227    -0.1006 -1.7160 -1.2170 -5.9322        2.5328
    ##         total_wages government government_wages   taxes time_trend
    ## 2021 Q1     -0.5414     1.2758          -0.5982  0.8997        205
    ## 2021 Q2     -0.5587    -1.0791          -0.2234  9.1809        206
    ## 2021 Q3     -0.7342    -0.3873           0.8500  0.3182        207
    ## 2021 Q4     -0.8133    -0.0708          -0.9966  4.6251        208
    ## 2022 Q1     -0.5534    -0.8533          -0.8763  3.5975        209
    ## 2022 Q2     -0.6994    -0.3752          -1.3801 -1.3602        210
    ## 2022 Q3     -1.0263     0.3883           0.4571 -5.3551        211
    ## 2022 Q4     -1.3826     1.3228           0.5938 -6.2183        212
    ## 2023 Q1     -1.4918     1.2402           0.8097 -6.4787        213
    ## 2023 Q2     -1.3487     0.7229           0.9474 -2.6579        214
    ## 2023 Q3     -1.3666     1.3819           1.4833 -2.8205        215
    ## 2023 Q4     -1.2476     0.8931           1.2959 -2.1251        216

    plot(forecasts, variables = "gdp")