# Manually write code for a Vector Autoregression

15 views (last 30 days)
matlabuser on 19 Oct 2020
Commented: alphabetagamma on 19 Jan 2023
I am trying to manually write the code to get VAR(1) coefficients without using the built-in function from the Econometrics toolbox. There are three variables in the "beta" matrix, and I created the first lags of each of these three. Then, I applied OLS to each equation separately using a for loop, and stored the estimates in a matrix A.
I am also trying to compute the variance-covariance matrix of residuals. So, I thought of collecting the residuals first but when I do that inside the for loop, I received an error message: "unable to perform assignment because brace indexing is not supported of variables of this type"
In general, I am not sure if this is an efficient way to code VAR and get coefficient estimates and variance. So, if you have any suggestions on how to improve the code, I'd be very grateful for your help.
% create lagged values of beta
lag1_beta = lagmatrix(beta,1);
% apply OLS to each equation separately
for i = 1:size(beta,2) % counts the # of columns in the beta matrix
for j =['level', 'slope', 'curv']
VAR_{j} = fitlm(lag1_beta, beta(:, i), 'Intercept', false);
a{i} = VAR_{j}.Coefficients.Estimate';
resid{j} = VAR_{j}.Residuals.Raw';
end
end
% stack the coefficients of the individual OLS estimates
A =[a1; a2; a3]

Dana on 19 Oct 2020
First off, the interior (j) part of your loop doesn't make any sense, for several reasons:
(1) Everything inside that loop is based on the first line
VAR_{j} = fitlm(lag1_beta, beta(:, i), 'Intercept', false);
but there is no j on the right-hand side, so nothing would change as you interate through different values of j. I don't even have a guess as to what you want to change as j iterates.
(2) Note that ['level', 'slope', 'curv']='levelslopecurv'. Thus, when you write
for j =['level', 'slope', 'curv']
MATLAB interprets this as "cycle through the letters of 'levelslopecurv' one at a time, so that first j='l', then j='e', etc.". Hopefully you can see that this makes no sense.
(3) When j is a character (as (2) just established), and you use j as an index (e.g., as you did with VAR_{j}), MATLAB first converts the character to its ASCII code (e.g., 'l' becomes 108, since char(108)='l', etc.), and then uses that ASCII code as the index. There is no chance that's actually what you want here.
(4) The line
a{i} = VAR_{j}.Coefficients.Estimate';
overwrites a{i} (the i-th element of the cell array a) for each value of j. Again, this makes no sense.
Leaving aside the j business since I don't understand what you're trying to do with that, you don't need to do this for one equation at a time. If is your regression equation, with the identifying assumption , then substituing into the latter, you can solve for
.
Assuming you have T periods of observed data, letting be the matrix whose t-th row is , and the matrix whose t-th row is , and estimator of A is then
Thus, all you need to do in your case is
Y0 = beta(1:end-1,:);
Y1 = beta(2:end,:);
A = (Y1'*Y0)/(Y0'*Y0);
matlabuser on 11 Nov 2020
thank you so much
alphabetagamma on 19 Jan 2023
This is an old post, but I wonder how we can edit the code to have multiple lags, for example: VAR(2). I was thinking that I might have to write it in companion form first, but not sure. I appreciate your help.