Dear statalist members
I am new to Mata language, in the code below i simulate a VAR(2) using Stata and it works fine, does anyone know how to do the last part concerning the forvalues loop in Mata ,
the two equation for VAR(2) : y1 and y2 are simulated individually in STATA , in matrix form the values for y1 and y2 are derived as :
y=cons+A1*l.y+A2*l.2y+ u
where y is vector containing y1 and y2 / A1, A2 are matrices defined in Mata, l.y amd l2.y are lag 1 and lag2 of variables y1 and y2. and u is a vector of errors
clear all
local t = 1200
set obs 1200
gen t = _n
tsset t
forvalues i=1/2 {
gen y`i'=rnormal() in 1/2
}
matrix M = 2, 0
matrix V = (1, 0.5\ 0.5, 1 ) // covariance matrix of errors
drawnorm u1 u2 , n(1200) cov(V) means(M) // drawing bivariate normal errors
mata:
t=1200
cons = (0.1\0.3)
A1 = (0.6,-0.2\0.4,0.2)
A2 = (0.2,0.4\-0.1,0.1)
Sigma = (1,0.5\0.5,1)
y=st_data(.,"y1 y2")
u=st_data(.,"u1 u2")
end
forvalues i=3/`t' {
qui {
replace y1 = 0.1 + 0.6*l.y1 - 0.2*l.y2 + 0.2*l2.y1 + 0.4*l2.y2 + u1 in `i'
replace y2 = 0.3 + 0.4*l.y1 + 0.2*l.y2 - 0.1*l2.y1 + 0.1*l2.y2 + u2 in `i'
}
* equations under forvalues loop in matrix form are y= cons+A1*l.y+ A2*l.2y + u
}
drop in 1/200
THANKS
0 Response to for loop in mata
Post a Comment