<p>If you search for “simultaneous equations” inside the <a href="https://cran.r-project.org/web/views/Econometrics.html" rel="nofollow noreferrer">Econometrics Task View</a> web page you can find two results: the <a href="https://cran.r-project.org/web/packages/systemfit/index.html" rel="nofollow noreferrer">systemfit</a> and the <a href="https://cran.r-project.org/web/packages/bimets/index.html" rel="nofollow noreferrer">bimets</a> packages.</p>
<p>As you stated, the <a href="https://cran.r-project.org/web/packages/systemfit/index.html" rel="nofollow noreferrer">systemfit</a> package is a powerful tool for econometric estimation of simultaneous systems of linear and nonlinear equations, but it only provides fitting procedures, thus it cannot be used in your example in order to work out a forecast.</p>
<p>On the other hand, the <a href="https://cran.r-project.org/web/packages/bimets/index.html" rel="nofollow noreferrer">bimets</a> package implements, among others, simulation and forecasting procedures, and multiplier analysis; therefore you can easily calculate elasticities.</p>
<p>Take a look also at the Rstudio blog post about SEM in R:
<a href="https://rviews.rstudio.com/2021/01/22/sem-time-series-modeling/" rel="nofollow noreferrer">https://rviews.rstudio.com/2021/01/22/sem-time-series-modeling/</a></p>
<p>An example of a simple Klein model in R follows:</p>
<pre><code>#load library
library(bimets)

#define the Klein model    
kleinModelDef &lt;- &quot;
MODEL

COMMENT&gt; Modified Klein Model 1 of the U.S. Economy with PDL, 
COMMENT&gt; autocorrelation on errors, restrictions and conditional equation evaluations

COMMENT&gt; Consumption with autocorrelation on errors
BEHAVIORAL&gt; cn
TSRANGE 1923 1 1940 1
EQ&gt; cn =  a1 + a2*p + a3*TSLAG(p,1) + a4*(wp+wg) 
COEFF&gt; a1 a2 a3 a4
ERROR&gt; AUTO(2)

COMMENT&gt; Investment with restrictions
BEHAVIORAL&gt; i
TSRANGE 1923 1 1940 1
EQ&gt; i = b1 + b2*p + b3*TSLAG(p,1) + b4*TSLAG(k,1)
COEFF&gt; b1 b2 b3 b4
RESTRICT&gt; b2 + b3 = 1

COMMENT&gt; Demand for Labor with PDL
BEHAVIORAL&gt; wp 
TSRANGE 1923 1 1940 1
EQ&gt; wp = c1 + c2*(y+t-wg) + c3*TSLAG(y+t-wg,1) + c4*time
COEFF&gt; c1 c2 c3 c4
PDL&gt; c3 1 2

COMMENT&gt; Gross National Product
IDENTITY&gt; y
EQ&gt; y = cn + i + g - t

COMMENT&gt; Profits
IDENTITY&gt; p
EQ&gt; p = y - (wp+wg)

COMMENT&gt; Capital Stock with IF switches
IDENTITY&gt; k
EQ&gt; k = TSLAG(k,1) + i
IF&gt; i &gt; 0
IDENTITY&gt; k
EQ&gt; k = TSLAG(k,1) 
IF&gt; i &lt;= 0

END
&quot;

#load the model
kleinModel &lt;- LOAD_MODEL(modelText = kleinModelDef)

#define data
kleinModelData &lt;- list(  
  cn  =TIMESERIES(39.8,41.9,45,49.2,50.6,52.6,55.1,56.2,57.3,57.8,
                  55,50.9,45.6,46.5,48.7,51.3,57.7,58.7,57.5,61.6,65,69.7,  
                  START=c(1920,1),FREQ=1),
  g   =TIMESERIES(4.6,6.6,6.1,5.7,6.6,6.5,6.6,7.6,7.9,8.1,9.4,10.7,
                  10.2,9.3,10,10.5,10.3,11,13,14.4,15.4,22.3,   
                  START=c(1920,1),FREQ=1),
  i   =TIMESERIES(2.7,-.2,1.9,5.2,3,5.1,5.6,4.2,3,5.1,1,-3.4,-6.2,
                  -5.1,-3,-1.3,2.1,2,-1.9,1.3,3.3,4.9,  
                  START=c(1920,1),FREQ=1),
  k   =TIMESERIES(182.8,182.6,184.5,189.7,192.7,197.8,203.4,207.6,
                  210.6,215.7,216.7,213.3,207.1,202,199,197.7,199.8,
                  201.8,199.9,201.2,204.5,209.4,    
                  START=c(1920,1),FREQ=1),
  p   =TIMESERIES(12.7,12.4,16.9,18.4,19.4,20.1,19.6,19.8,21.1,21.7,
                  15.6,11.4,7,11.2,12.3,14,17.6,17.3,15.3,19,21.1,23.5, 
                  START=c(1920,1),FREQ=1),
  wp  =TIMESERIES(28.8,25.5,29.3,34.1,33.9,35.4,37.4,37.9,39.2,41.3,
                  37.9,34.5,29,28.5,30.6,33.2,36.8,41,38.2,41.6,45,53.3,    
                  START=c(1920,1),FREQ=1),
  y   =TIMESERIES(43.7,40.6,49.1,55.4,56.4,58.7,60.3,61.3,64,67,57.7,
                  50.7,41.3,45.3,48.9,53.3,61.8,65,61.2,68.4,74.1,85.3, 
                  START=c(1920,1),FREQ=1),
  t   =TIMESERIES(3.4,7.7,3.9,4.7,3.8,5.5,7,6.7,4.2,4,7.7,7.5,8.3,5.4,
                  6.8,7.2,8.3,6.7,7.4,8.9,9.6,11.6, 
                  START=c(1920,1),FREQ=1),
  time=TIMESERIES(NA,-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0,
                  1,2,3,4,5,6,7,8,9,10, 
                  START=c(1920,1),FREQ=1),
  wg  =TIMESERIES(2.2,2.7,2.9,2.9,3.1,3.2,3.3,3.6,3.7,4,4.2,4.8,
                  5.3,5.6,6,6.1,7.4,6.7,7.7,7.8,8,8.5,  
                  START=c(1920,1),FREQ=1)
);

#load time series into the model object
kleinModel &lt;- LOAD_MODEL_DATA(kleinModel,kleinModelData)
## Load model data &quot;kleinModelData&quot; into model &quot;kleinModelDef&quot;...
## ...LOAD MODEL DATA OK

#estimate the model
kleinModel &lt;- ESTIMATE(kleinModel)

#In order to forecast the model up to 1944, 
#we need to extend exogenous variables up to 1944
kleinModel<span class="math-container">$modelData &lt;- within(kleinModel$</span>modelData,{
    wg    = TSEXTEND(wg,  UPTO=c(1944,1),EXTMODE='CONSTANT')
    t     = TSEXTEND(t,   UPTO=c(1944,1),EXTMODE='LINEAR')
    g     = TSEXTEND(g,   UPTO=c(1944,1),EXTMODE='CONSTANT')
    k     = TSEXTEND(k,   UPTO=c(1944,1),EXTMODE='LINEAR')
    time  = TSEXTEND(time,UPTO=c(1944,1),EXTMODE='LINEAR')
  })

#forecast model
kleinModel &lt;- SIMULATE(kleinModel
                      ,simType='FORECAST'
                      ,TSRANGE=c(1941,1,1944,1)
                      ,simConvergence=0.00001
                      ,simIterLimit=100
                      ,quietly=TRUE
  ) 

#get forecasted GNP
TABIT(kleinModel<span class="math-container">$simulation$</span>y)
## 
##       DATE, PER, kleinModel<span class="math-container">$simulation$</span>y
## 
##       1941, 1  ,  125.3      
##       1942, 1  ,  172.5      
##       1943, 1  ,  185.6      
##       1944, 1  ,  141.1
</code></pre>
<p><a href="https://i.sstatic.net/0ceDu.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/0ceDu.png" alt="enter image description here" /></a>
<a href="https://i.sstatic.net/Iwhdw.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Iwhdw.png" alt="enter image description here" /></a></p>

CC BY-SA 4.0
2021-02-06T16:28:37.157Z
2021-02-06T16:28:37.157Z
Andrew
<p>I use the <code>systemfit</code> package to evaluate Simultaneous Equations Models (for example 2SLS). But I do not understand how to predict endogenous variables using the new values of exogenous variables using this package.</p>

<p>Therefore, I would like to know how to make prediction (including prediction intervals) using models of simultaneous equations in the "systemfit" package or in some other way (maybe someone knows other packages for this).</p>

<p>I am also interested in how I can calculate the elastic coefficients of models.</p>

<p>I will be grateful to the literature on the topic of modeling simultaneous equations in R.</p>

CC BY-SA 4.0
2019-12-16T23:26:13.967Z
You sohuld try Cross Validated @ stats.stackexchange.com
Pedro Cavalcante
CC BY-SA 4.0
2019-12-17T23:25:12.133Z
But this topic is about econometrics
Vitalii
CC BY-SA 4.0
2019-12-18T07:26:38.450Z
@Pedrocavalcante CV usually rejects software related questions unless they contain an underlying statistical question.
Maarten Punt
CC BY-SA 4.0
2019-12-18T07:30:10.403Z
@Vitalii your question on how to calculate elasticities in such models may fit at CV provided you are not solely interested in which code to type in R. There are a bunch of great econometricians there too.
Maarten Punt
CC BY-SA 4.0
2021-02-07T18:21:39.787Z
@PedroCavalcante, as an active user of Cross Validated I can confirm what Maarten Punt said.
Richard Hardy
<econometrics><r><simulations>
Simultaneous Equations Models in R
Vitalii
# Simultaneous Equations Models in R

(Asked by: username_0 on 2019-12-16)

I use the `systemfit` package to evaluate Simultaneous Equations Models (for example 2SLS). But I do not understand how to predict endogenous variables using the new values of exogenous variables using this package.

Therefore, I would like to know how to make prediction (including prediction intervals) using models of simultaneous equations in the "systemfit" package or in some other way (maybe someone knows other packages for this).

I am also interested in how I can calculate the elastic coefficients of models.

I will be grateful to the literature on the topic of modeling simultaneous equations in R.

**Tags:** econometrics, r, simulations
> **Comments:**
> **username_1**: You sohuld try Cross Validated @ stats.stackexchange.com  
> **username_0**: But this topic is about econometrics  
> **username_2**: @Pedrocavalcante CV usually rejects software related questions unless they contain an underlying statistical question.  
> **username_2**: @Vitalii your question on how to calculate elasticities in such models may fit at CV provided you are not solely interested in which code to type in R. There are a bunch of great econometricians there too.  
> **username_3**: @PedroCavalcante, as an active user of Cross Validated I can confirm what Maarten Punt said.  

---

## 1 answers

### Answer by username_4 on 2021-02-06. Score: 3 | Accepted answer

If you search for “simultaneous equations” inside the Econometrics Task View web page you can find two results: the systemfit and the bimets packages.
As you stated, the systemfit package is a powerful tool for econometric estimation of simultaneous systems of linear and nonlinear equations, but it only provides fitting procedures, thus it cannot be used in your example in order to work out a forecast.
On the other hand, the bimets package implements, among others, simulation and forecasting procedures, and multiplier analysis; therefore you can easily calculate elasticities.
Take a look also at the Rstudio blog post about SEM in R:
https://rviews.rstudio.com/2021/01/22/sem-time-series-modeling/
An example of a simple Klein model in R follows:
#load library
library(bimets)

#define the Klein model    
kleinModelDef <- "
MODEL

COMMENT> Modified Klein Model 1 of the U.S. Economy with PDL, 
COMMENT> autocorrelation on errors, restrictions and conditional equation evaluations

COMMENT> Consumption with autocorrelation on errors
BEHAVIORAL> cn
TSRANGE 1923 1 1940 1
EQ> cn =  a1 + a2*p + a3*TSLAG(p,1) + a4*(wp+wg) 
COEFF> a1 a2 a3 a4
ERROR> AUTO(2)

COMMENT> Investment with restrictions
BEHAVIORAL> i
TSRANGE 1923 1 1940 1
EQ> i = b1 + b2*p + b3*TSLAG(p,1) + b4*TSLAG(k,1)
COEFF> b1 b2 b3 b4
RESTRICT> b2 + b3 = 1

COMMENT> Demand for Labor with PDL
BEHAVIORAL> wp 
TSRANGE 1923 1 1940 1
EQ> wp = c1 + c2*(y+t-wg) + c3*TSLAG(y+t-wg,1) + c4*time
COEFF> c1 c2 c3 c4
PDL> c3 1 2

COMMENT> Gross National Product
IDENTITY> y
EQ> y = cn + i + g - t

COMMENT> Profits
IDENTITY> p
EQ> p = y - (wp+wg)

COMMENT> Capital Stock with IF switches
IDENTITY> k
EQ> k = TSLAG(k,1) + i
IF> i > 0
IDENTITY> k
EQ> k = TSLAG(k,1) 
IF> i <= 0

END
"

#load the model
kleinModel <- LOAD_MODEL(modelText = kleinModelDef)

#define data
kleinModelData <- list(  
  cn  =TIMESERIES(39.8,41.9,45,49.2,50.6,52.6,55.1,56.2,57.3,57.8,
                  55,50.9,45.6,46.5,48.7,51.3,57.7,58.7,57.5,61.6,65,69.7,  
                  START=c(1920,1),FREQ=1),
  g   =TIMESERIES(4.6,6.6,6.1,5.7,6.6,6.5,6.6,7.6,7.9,8.1,9.4,10.7,
                  10.2,9.3,10,10.5,10.3,11,13,14.4,15.4,22.3,   
                  START=c(1920,1),FREQ=1),
  i   =TIMESERIES(2.7,-.2,1.9,5.2,3,5.1,5.6,4.2,3,5.1,1,-3.4,-6.2,
                  -5.1,-3,-1.3,2.1,2,-1.9,1.3,3.3,4.9,  
                  START=c(1920,1),FREQ=1),
  k   =TIMESERIES(182.8,182.6,184.5,189.7,192.7,197.8,203.4,207.6,
                  210.6,215.7,216.7,213.3,207.1,202,199,197.7,199.8,
                  201.8,199.9,201.2,204.5,209.4,    
                  START=c(1920,1),FREQ=1),
  p   =TIMESERIES(12.7,12.4,16.9,18.4,19.4,20.1,19.6,19.8,21.1,21.7,
                  15.6,11.4,7,11.2,12.3,14,17.6,17.3,15.3,19,21.1,23.5, 
                  START=c(1920,1),FREQ=1),
  wp  =TIMESERIES(28.8,25.5,29.3,34.1,33.9,35.4,37.4,37.9,39.2,41.3,
                  37.9,34.5,29,28.5,30.6,33.2,36.8,41,38.2,41.6,45,53.3,    
                  START=c(1920,1),FREQ=1),
  y   =TIMESERIES(43.7,40.6,49.1,55.4,56.4,58.7,60.3,61.3,64,67,57.7,
                  50.7,41.3,45.3,48.9,53.3,61.8,65,61.2,68.4,74.1,85.3, 
                  START=c(1920,1),FREQ=1),
  t   =TIMESERIES(3.4,7.7,3.9,4.7,3.8,5.5,7,6.7,4.2,4,7.7,7.5,8.3,5.4,
                  6.8,7.2,8.3,6.7,7.4,8.9,9.6,11.6, 
                  START=c(1920,1),FREQ=1),
  time=TIMESERIES(NA,-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0,
                  1,2,3,4,5,6,7,8,9,10, 
                  START=c(1920,1),FREQ=1),
  wg  =TIMESERIES(2.2,2.7,2.9,2.9,3.1,3.2,3.3,3.6,3.7,4,4.2,4.8,
                  5.3,5.6,6,6.1,7.4,6.7,7.7,7.8,8,8.5,  
                  START=c(1920,1),FREQ=1)
);

#load time series into the model object
kleinModel <- LOAD_MODEL_DATA(kleinModel,kleinModelData)
## Load model data "kleinModelData" into model "kleinModelDef"...
## ...LOAD MODEL DATA OK

#estimate the model
kleinModel <- ESTIMATE(kleinModel)

#In order to forecast the model up to 1944, 
#we need to extend exogenous variables up to 1944
kleinModel$modelData <- within(kleinModel$modelData,{
    wg    = TSEXTEND(wg,  UPTO=c(1944,1),EXTMODE='CONSTANT')
    t     = TSEXTEND(t,   UPTO=c(1944,1),EXTMODE='LINEAR')
    g     = TSEXTEND(g,   UPTO=c(1944,1),EXTMODE='CONSTANT')
    k     = TSEXTEND(k,   UPTO=c(1944,1),EXTMODE='LINEAR')
    time  = TSEXTEND(time,UPTO=c(1944,1),EXTMODE='LINEAR')
  })

#forecast model
kleinModel <- SIMULATE(kleinModel
                      ,simType='FORECAST'
                      ,TSRANGE=c(1941,1,1944,1)
                      ,simConvergence=0.00001
                      ,simIterLimit=100
                      ,quietly=TRUE
  ) 

#get forecasted GNP
TABIT(kleinModel$simulation$y)
## 
##       DATE, PER, kleinModel$simulation$y
## 
##       1941, 1  ,  125.3      
##       1942, 1  ,  172.5      
##       1943, 1  ,  185.6      
##       1944, 1  ,  141.1


---

