knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
This guide explains how to use the godley
package to create the DIS model — a model with private bank money, inventories and disequilibrium (of a kind) in the goods market, as described by Wynne Godley and Marc Lavoie in Chapter 9 of Monetary Economics. An Integrated Approach to Credit, Money, Income, Production and Wealth.
library(godley)
Start by creating an empty SFC model:
# Create empty model model_dis <- create_model(name = "SFC DIS")
Define the variables for the model:
# Add variables model_dis <- model_dis |> add_variable("rl", init = 0.025) |> add_variable("pr", init = 1) |> add_variable("W", init = 0.75) |> add_variable("add", init = 0.02) |> add_variable("alpha0", init = 15) |> add_variable("alpha1", init = 0.8) |> add_variable("alpha2", init = 0.1) |> add_variable("beta", init = 0.75) |> add_variable("epsilon", init = 0.75) |> add_variable("gamma", init = 0.25) |> add_variable("phi", init = 0.25) |> add_variable("sigma_T", init = 0.15) |> add_variable("y", init = .001) |> add_variable("p", init = .001) |> add_variable("NHUC", init = .001) |> add_variable("s_E", init = .001) |> add_variable("inv_T") |> add_variable("inv_E") |> add_variable("inv") |> add_variable("s") |> add_variable("c") |> add_variable("N") |> add_variable("WB") |> add_variable("UC") |> add_variable("INV") |> add_variable("S") |> add_variable("EF") |> add_variable("Ld") |> add_variable("Ls") |> add_variable("Ms") |> add_variable("rm") |> add_variable("EFb") |> add_variable("Mh") |> add_variable("YD") |> add_variable("C") |> add_variable("ydhs") |> add_variable("mh") |> add_variable("ydhs_E")
Establish the relationships between variables by adding equations:
# Add equations model_dis <- model_dis |> add_equation("y = s_E + inv_E - inv[-1]") |> add_equation("inv_T = sigma_T * s_E") |> add_equation("inv_E = inv[-1] + gamma * (inv_T - inv[-1])") |> add_equation("inv = inv[-1] + (y - s)") |> add_equation("s_E = beta * s[-1] + (1 - beta) * s_E[-1]") |> add_equation("s = c") |> add_equation("N = y / pr") |> add_equation("WB = N * W") |> add_equation("UC = WB / y") |> add_equation("INV = inv * UC") |> add_equation("S = p * s") |> add_equation("p = (1 + phi) * NHUC") |> add_equation("NHUC = (1 - sigma_T) * UC + sigma_T * (1 + rl[-1]) * UC[-1]") |> add_equation("EF = S - WB + (INV - INV[-1]) - rl[-1] * INV[-1]") |> add_equation("Ld = INV") |> add_equation("Ls = Ld") |> add_equation("Ms = Ls") |> add_equation("rm = rl - add") |> add_equation("EFb = rl[-1] * Ls[-1] - rm[-1] * Mh[-1]") |> add_equation("YD = WB + EF + EFb + rm[-1] * Mh[-1]") |> add_equation("Mh = Mh[-1] + YD - C") |> add_equation("ydhs = c + (mh - mh[-1])") |> add_equation("C = c * p") |> add_equation("mh = Mh / p") |> add_equation("c = alpha0 + alpha1 * ydhs_E + alpha2 * mh[-1]") |> add_equation("ydhs_E = epsilon * ydhs[-1] + (1 - epsilon) * ydhs_E[-1]") |> add_equation("Mh = Ms", hidden = TRUE)
Now, you can simulate the model (in this example, the baseline scenario over 100 periods using the Gauss method):
# Simulate model model_dis <- simulate_scenario(model_dis, scenario = "baseline", max_iter = 350, periods = 100, hidden_tol = 0.1, tol = 1e-08, method = "Gauss" )
With the simulation estimated, you can create a plot to visualize the results for the variables of interest:
# Plot results plot_simulation( model = model_dis, scenario = c("baseline"), from = 1, to = 40, expressions = c("ydhs", "c") )
# Plot results plot_simulation( model = model_dis, scenario = c("baseline"), from = 1, to = 40, expressions = c( "delta_inv = inv - dplyr::lag(inv)", "delta_inv_E = inv_E - dplyr::lag(inv_E)" ) )
Note: The above example uses the new pipe operator (|>
), which requires R 4.1 or later.
With godley
package we can simulate how shocks affect the economy (specifically, how they impact the base scenario).
The initial example demonstrates the effect of a one-shot increase in the costing margin.
First, initialize an empty shock object:
# Create empty shock shock_dis <- create_shock()
Define the shock by adding an appropriate equation:
# Add shock equation shock_dis <- add_shock(shock_dis, variable = "phi", value = 0.35, desc = "One-shot increase in the costing margin", start = 5, end = 40 )
Integrate the shock into the model by creating a new scenario:
# Create new scenario with this shock model_dis <- add_scenario(model_dis, name = "expansion1", origin = "baseline", origin_start = 1, origin_end = 100, shock = shock_dis )
Simulate the scenario with the shock applied:
# Simulate shock model_dis <- simulate_scenario(model_dis, scenario = "expansion1", max_iter = 350, periods = 100, hidden_tol = 0.1, tol = 1e-08, method = "Gauss" )
Display the results on the plot:
# Plot results plot_simulation( model = model_dis, scenario = c("expansion1"), from = 1, to = 40, expressions = c("c", "ydhs") )
# Plot results plot_simulation( model = model_dis, scenario = c("expansion1"), from = 1, to = 40, expressions = c( "delta_inv = inv - dplyr::lag(inv)", "delta_inv_E = inv_E - dplyr::lag(inv_E)" ) )
Another example conveys the impact of an increase in the target inventory-to-sales ratio.
First, initialize an empty shock object:
# Create empty shock shock_dis <- create_shock()
Add an appropriate equation:
# Add shock equation shock_dis <- add_shock(shock_dis, variable = "sigma_T", value = 0.25, desc = "Increase in the target inventories to sales ratio", start = 5, end = 50 )
Integrate the shock into the model by creating a new scenario:
# Create new scenario with this shock model_dis <- add_scenario(model_dis, name = "expansion2", origin = "baseline", origin_start = 1, origin_end = 100, shock = shock_dis )
Simulate the model with the shock applied:
# Simulate shock model_dis <- simulate_scenario(model_dis, scenario = "expansion2", max_iter = 350, periods = 100, hidden_tol = 0.1, tol = 1e-08, method = "Gauss" )
Plot the results:
# Plot results plot_simulation( model = model_dis, scenario = c("expansion2"), from = 1, to = 40, expressions = c("c", "ydhs") )
# Plot results plot_simulation( model = model_dis, scenario = c("expansion2"), from = 1, to = 40, expressions = c( "delta_inv = inv - dplyr::lag(inv)", "delta_inv_E = inv_E - dplyr::lag(inv_E)" ) )
For more details on DIS model, refer to Chapter 9 of Monetary Economics. An Integrated Approach to Credit, Money, Income, Production and Wealth.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.