MatLab Conversion to VBA

normanbox

New Member
Joined
Jul 8, 2015
Messages
46
Hello group,

Ultimately I'm trying to complete a Granger Causality analysis in Excel. Because I'm only slightly familiar with VBA, I need all the help I can get. I found another post on a different forum with a MatLab code and in the comments, the person said that it will easily convert to VBA; however, I have no idea how to do it. Below is the code and if someone, anyone, could convert the code, I would be very appreciative. If the Matlab code doesn't convert, could somebody provide me with code to complete a Granger Causality analysis? As mentioned, that's all I really need to do.

Thank you in advance!
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
I can't believe I forgot to actually post the code!

function [F,c_v] = granger_cause(x,y,alpha,max_lag)
% [F,c_v] = granger_cause(x,y,alpha,max_lag)
% Granger Causality test
% Does Y Granger Cause X?
%
% User-Specified Inputs:
% x -- A column vector of data
% y -- A column vector of data
% alpha -- the significance level specified by the user
% max_lag -- the maximum number of lags to be considered
% User-requested Output:
% F -- The value of the F-statistic
% c_v -- The critical value from the F-distribution
%
% The lag length selection is chosen using the Bayesian information
% Criterion
% Note that if F > c_v we reject the null hypothesis that y does not
% Granger Cause x

% Chandler Lutz, UCR 2009
% Questions/Comments: chandler.lutz@email.ucr.edu
% $Revision: 1.0.0 $ $Date: 09/30/2009 $
% $Revision: 1.0.1 $ $Date: 10/20/2009 $

% References:
% [1] Granger, C.W.J., 1969. "Investigating causal relations by econometric
% models and cross-spectral methods". Econometrica 37 (3), 424?438.

%Make sure x & y are the same length
if (length(x) ~= length(y))
error('x and y must be the same length');
end

%Make sure x is a column vector
[a,b] = size(x);
if (b>a)
%x is a row vector -- fix this
x = x';
end

%Make sure y is a column vector
[a,b] = size(y);
if (b>a)
%y is a row vector -- fix this
y = y';
end



%Make sure max_lag is >= 1
if max_lag < 1
error('max_lag must be greater than or equal to one');
end

%First find the proper model specification using the Bayesian Information
%Criterion for the number of lags of x

T = length(x);

BIC = zeros(max_lag,1);

%Specify a matrix for the restricted RSS
RSS_R = zeros(max_lag,1);

i = 1;
while i <= max_lag
ystar = x(i+1:T,:);
xstar = [ones(T-i,1) zeros(T-i,i)];
%Populate the xstar matrix with the corresponding vectors of lags
j = 1;
while j <= i
xstar(:,j+1) = x(i+1-j:T-j);
j = j+1;
end
%Apply the regress function. b = betahat, bint corresponds to the 95%
%confidence intervals for the regression coefficients and r = residuals
[b,bint,r] = regress(ystar,xstar);

%Find the bayesian information criterion
BIC(i,:) = T*log(r'*r/T) + (i+1)*log(T);

%Put the restricted residual sum of squares in the RSS_R vector
RSS_R(i,:) = r'*r;

i = i+1;

end

x_lag = find(min(BIC));

%First find the proper model specification using the Bayesian Information
%Criterion for the number of lags of y

BIC = zeros(max_lag,1);

%Specify a matrix for the unrestricted RSS
RSS_U = zeros(max_lag,1);

i = 1;
while i <= max_lag

ystar = x(i+x_lag+1:T,:);
xstar = [ones(T-(i+x_lag),1) zeros(T-(i+x_lag),x_lag+i)];
%Populate the xstar matrix with the corresponding vectors of lags of x
j = 1;
while j <= x_lag
xstar(:,j+1) = x(i+x_lag+1-j:T-j,:);
j = j+1;
end
%Populate the xstar matrix with the corresponding vectors of lags of y
j = 1;
while j <= i
xstar(:,x_lag+j+1) = y(i+x_lag+1-j:T-j,:);
j = j+1;
end
%Apply the regress function. b = betahat, bint corresponds to the 95%
%confidence intervals for the regression coefficients and r = residuals
[b,bint,r] = regress(ystar,xstar);

%Find the bayesian information criterion
BIC(i,:) = T*log(r'*r/T) + (i+1)*log(T);

RSS_U(i,:) = r'*r;

i = i+1;

end

y_lag = find(min(BIC));

%The numerator of the F-statistic
F_num = ((RSS_R(x_lag,: ) - RSS_U(y_lag,:))/y_lag);

%The denominator of the F-statistic
F_den = RSS_U(y_lag,:)/(T-(x_lag+y_lag+1));

%The F-Statistic
F = F_num/F_den;

c_v = finv(1-alpha,y_lag,(T-(x_lag+y_lag+1)));
 
Upvote 0
I know the code is a little long, but doesn't anybody have any help they can provide? Maybe to resource so I can convert the code myself?
 
Upvote 0

Forum statistics

Threads
1,223,676
Messages
6,173,758
Members
452,534
Latest member
autodiscreet

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top