I provide a simplified example of a function focused on setting up and solving a basic Computable General Equilibrium (CGE) model. The function consists of the following components:
nleqslv()), the function adjusts the economic variables
until all equilibrium conditions are satisfied.The model allows for the analysis of different scenarios by changing input values or adding shocks to the parameters, such as introducing a tax policy or a subsidy. This makes it possible to see how changes in external factors affect economic outcomes, helping analyze the impact of policy changes.
The own developed function simulate_keynesian_model in R
is designed to simulate a simple Keynesian economic model. It calculates
the equilibrium level of output (income) based on several inputs,
including consumption, investment, government spending, and taxes.
Here’s a technical description of how the function works:
c0 and c1 represent the autonomous
consumption and marginal propensity to consume, respectively. These
define how much consumption will increase with an increase in disposable
income.I0 is autonomous investment, representing investment
that doesn’t depend on income.G stands for government spending.T represents taxes.iterations and tolerance set the maximum
number of iterations and the acceptable tolerance level to check for
equilibrium (convergence).Y (output) and
Y_old (previous output) to track income over
iterations.Y_new, the
function checks if the difference between Y and
Y_old is smaller than tolerance. If so, it
assumes equilibrium is reached and stops iterating. This prevents an
infinite loop and defines equilibrium as when the change in output from
one iteration to the next is very small.When equilibrium is reached, the function returns the equilibrium
values of total output (Y), consumption (C),
investment (I), and government spending
(G).
To use this function, you would set values for each input parameter, run the function, and observe the calculated equilibrium values. This simple model iteratively computes the equilibrium output level using the Keynesian framework, making it a powerful and flexible base for basic economic modeling and simulation in R.
For simplicity, let’s consider above basic Keynesian model with the
three main equations for output (Y), consumption
(C), and investment (I):
Where: - \(Y\) is the total output or income. - \(C\) is consumption. - \(I\) is investment. - \(G\) is government spending. - \(T\) is taxes. - \(c_0\) and \(c_1\) are constants for autonomous consumption and marginal propensity to consume, respectively.
Here’s the sample R function that takes parameters as input and iterates to find the equilibrium level of output:
# Define the model function
simulate_keynesian_model <- function(c0, c1, I0, G, T, iterations = 100, tolerance = 1e-5) {
# Initialize variables
Y <- 0 # Initial guess for output
Y_old <- -1 # For checking convergence
# Iterative process to reach equilibrium
for (i in 1:iterations) {
# Calculate disposable income
Y_d <- Y - T
# Calculate consumption based on disposable income
C <- c0 + c1 * Y_d
# Total output (Income identity)
Y_new <- C + I0 + G
# Check for convergence
if (abs(Y_new - Y_old) < tolerance) {
cat("Equilibrium reached after", i, "iterations\n")
break
}
# Update variables for next iteration
Y_old <- Y
Y <- Y_new
}
# Output the results
return(list(Equilibrium_Y = Y, Consumption = C, Investment = I0, Government_Spending = G))
}
With this function in place, we can now simulate the model by
specifying values for c0, c1, I0,
G, and T. For example:
# Define parameters
c0 <- 100 # Autonomous consumption
c1 <- 0.8 # Marginal propensity to consume
I0 <- 200 # Autonomous investment
G <- 150 # Government spending
T <- 50 # Taxes
# Simulate the model
results <- simulate_keynesian_model(c0, c1, I0, G, T)
## Equilibrium reached after 84 iterations
print(results)
## $Equilibrium_Y
## [1] 2050
##
## $Consumption
## [1] 1700
##
## $Investment
## [1] 200
##
## $Government_Spending
## [1] 150
tolerance
parameter sets the convergence threshold to determine when equilibrium
is reached.To replicate more complex functions, we could expand the function by adding: - Dynamic behavior: Model dynamics over time by tracking past values of variables. - Multiple Sectors: Include equations for multiple sectors or variables, such as wages, prices, and capital accumulation. - Stochastic Shocks: Introduce random shocks to variables for stochastic simulations.
This basic function provides an example of a starting point in building and customizing our own economic models in R.