Good Morning/Afternoon/Evening,

I am a high school teacher and I am building a very basic Computer Generated Equilibrium Model (CGE Model) - code below. I think it would be very cool to show the kids how the 'economy' can come together in one of these models, sort of big picture thinking.

Basically, I have a number of endogenous variables which need to be solved for given a set of linear and/or non-linear equations, constraints which could be linear or non-linear, and a set of known (exogenous) variables.

I am having trouble making this work (could be the package I am trying to use) but more experienced users on this platform might have better insights on how to make this model work.

I have tried hard to structure the model so that it would be easy to add new equations and variables as desired without needing to rebuild the model each time.

```
#_______________________________________________________________________________
#
# STYLISED JOHANSSON CGE MODEL
#
#_______________________________________________________________________________
rm(list=ls(all=TRUE))
######### INPUTS ###############################################################
NUMBER_OFENDOGENOUS_VARIABLES = 14
#======== Packages
if(!require(nleqslv)){#__________
install.packages("nleqslv")
}#_________________________________
library(nleqslv)
######### CACULATIONS ##########################################################
#~~~~~~~~ CGE Model Equations FUNCTION ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CGE_Model_Equations_Funcion <- function(#__________
Endogenous_Base_Variables
){#________________________________________________
#========= Exogenous Inputs ===================
CAPITAL_SUPPLY = 1000
LABOUR_SUPPLY = 5000
TFP_X = 1 # Any Number > 1
TFP_Y = 1 # Any Number > 1
ALPHA_X = 0.5 # Any Number (0, 1)
ALPHA_Y = 0.5 # Any Number (0, 1)
BETA = 0.5 # Any Number (0, 1)
#========= Endogenous Variables ============
Endogenous_Variables_Names <- c("good_X",
"good_Y",
"labour_demand_X",
"labour_demand_Y",
"capital_demand_X",
"capital_demand_Y",
"price_X",
"price_Y",
"wage",
"interest_rate",
"income",
"utility",
"consumption_X",
"consumption_Y")
rownames(Endogenous_Base_Variables) <- Endogenous_Variables_Names
X <- t(Endogenous_Base_Variables) # I need to transpose this so I can extract elements by NAMES (Is this True?)
X <- as.data.frame(X) # Needs to be a data frame so i can extract variables by name
#========= Endogenous Equations =============================================
#-------- Firms and Production ------------------------------------------
X$good_X = TFP_X * (X$labour_demand_X ^ ALPHA_X) * (X$capital_demand_X ^ (1 - ALPHA_X)) # Production Functions
X$good_Y = TFP_Y * (X$labour_demand_Y ^ ALPHA_Y) * (X$capital_demand_Y ^ (1 - ALPHA_Y))
X$labour_demand_X = (ALPHA_X * X$price_X * X$good_X) / X$wage # Factor Demands for both labour and capital accross both goods
X$labour_demand_Y = (ALPHA_Y * X$price_Y * X$good_Y) / X$wage
X$capital_demand_X = ((1 - ALPHA_X) * X$price_X * X$good_X) / X$interest_rate
X$capital_demand_Y = ((1 - ALPHA_Y) * X$price_Y * X$good_Y) / X$interest_rate
#-------- Households and Consumption ------------------------------------
X$income = wage * (X$labour_demand_X + X$labour_demand_Y) +
X$interest_rate * (X$capital_demand_X + X$capital_demand_Y) # This is total household income
X$utility = X$income * (X$consumption_X ^ BETA) * (X$consumption_Y ^ (1 - BETA)) # Household utility function
X$consumption_X = (BETA * X$income) / X$price_X # Total household consumption of both goods
X$consumption_Y = ((1 - BETA) * X$income) / X$price_Y
#-------- Equilibrium Conditions ------------------------------------------
if(#_____ Conditions for a Valid Solution ______________ # I know with FUNCTION OPTIM you can insert contraints like this
X$consumption_X == X$good_X & # Production of goods must equal consumption
X$consumption_Y == X$good_Y &
(X$labour_demand_X + X$labour_demand_Y) == LABOUR_SUPPLY & # the supply of factors of production must equal demand
(X$capital_demand_X + X$capital_demand_Y) == CAPITAL_SUPPLY
){return(t(as.matrix(X))) # the nleqslv needs a single column/vector of results?
}else{#_ _ _A Solution has Not Been Found _ _ _ _ _ _
NA_Matrix <- rep(NA, ncol(X)) # If an NA is given the function tries to solve again?
return(NA_Matrix)
}#_____________________________________________________
}#~~~~~~~ END CGE Model Equations FUNCTION ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#======== Running the CGE Model
Endogenous_Base_Variables <- c(rep(1, NUMBER_OFENDOGENOUS_VARIABLES)) # Starting Values
CGE_Endogenous_Results <- nleqslv(Endogenous_Base_Variables, CGE_Model_Equations_Funcion)
#________ END OF MODEL ########################################################
```

I would really appreciate people's input and help with this. Thank you in advance.