Error using horzcat Dimensions of arrays being concatenated are not consistent.

5 views (last 30 days)
Any sort of help with this one here would be greatly appreciated. For whatever reason,I keep on getting this annoying error whenever I attempt to estimate a VAR model:
Error using horzcat
Dimensions of arrays being concatenated are not consistent.
Error in Question1 (line 27)
Z = [G Y C T P R];
The code or main body is as follows:
close all;
clear; clc;
%% STEP 1 - DOWNLOAD THE DATA AND TRANSFORM APPROPRIATELY
% 1 - LOG GDP
Ydata = xlsread('Exam_data_2018.xlsx','Sheet1','b6:b235');
Y = 100*log(Ydata);
% 2 - LOG GOVERNMENT SPENDING
Gdata = xlsread('Exam_data_2018.xlsx','Sheet1','c6:c157');
G = 100*log(Gdata);
% 3 - LOG PRIVATE CONSUMPTION
Cdata = xlsread('Exam_data_2018.xlsx','Sheet1','c6:c157');
C = 100*log(Cdata);
% 4 - TAXES MINUS TRANSFERS
Tdata = xlsread('Exam_data_2018.xlsx','Sheet1','e6:e157');
T = 100*log(Tdata+1000); %Add a constant to avoid taking logs of a negative number
% 5 - CPI (Pt)
Pdata = xlsread('Exam_data_2018.xlsx','Sheet1','f6:f157');
P = 100*log(Pdata); %Add a constant to avoid taking logs of a negative number
% 6 - 3-MONTH T-BILL (R)
Rdata = xlsread('Exam_data_2018.xlsx','Sheet1','g6:g157');
R = 100*log(Rdata); %Add a constant to avoid taking logs of a negative number
%% STEP 2 - ARRANGE THE DATASET
Z = [G Y C T P R];
Shock = zeros(size(Z,2),1); Shock(1,1) = 1; % Give shock to 1st variable (G)
pp = 4; % Lag length
hh = 40; % IRF length
tt = 3; % Choice of trend: 1 for constant but no trend, 2 for linear trend, 3 for linear and quadratic trend
[T, N] = size(Z);
%% STEP 3 - FIT THE VAR
[AR_3d,Chol_Var,epsi] = VAR_OLS(Z,pp,tt,[]);
%% STEP 4 - COMPUTE IRFs
Ai_mat = dyn_multipliers(N,pp,AR_3d,hh);
SIRF = Sirf(N,hh,Ai_mat,Chol_Var,Shock)';
%% STEP 5 - BOOTSTRAP CONFIDENCE BANDS
no_boot=1000;
conf_err=1; % No. of std. errors in confidence bands - 1 std. error equals 68 percent conf. band, 2 std. errors imply 95 percent
do_bootstrap_new; %Call external function
%% STEP 6 - PLOT THE IRFs
%First we normalize the IRFs with respect to the initial increase in G (in column 1 of SIRF)
factor = 1/SIRF(1,1);
SIRF = SIRF.*factor; SIRF_low = SIRF_low*factor; SIRF_up = SIRF_up*factor;
% Normalize the responses of C and Y by their size relative to G:
Gratio = [ Gdata./Ydata Gdata./Cdata ]; ratios=mean(Gratio);
SIRF(:,2)=SIRF(:,2)./ratios(1); SIRF(:,3)=SIRF(:,3)./ratios(2);
SIRF_low(:,2)=SIRF_low(:,2)./ratios(1); SIRF_up(:,2)=SIRF_up(:,2)./ratios(1);
SIRF_low(:,3)=SIRF_low(:,3)./ratios(2); SIRF_up(:,3)=SIRF_up(:,3)./ratios(2);
figure(1);
xnum=1:1:hh; x2num=[xnum,fliplr(xnum)]; %Trick to help plot conf. bands
graychoice=[0.83,0.83,0.83]; %Choose color of conf. bands
subplot(2,2,1);
conf_area=[SIRF_low(:,1)',fliplr(SIRF_up(:,1)')];
h=fill(x2num,conf_area,graychoice);
set(h,'EdgeColor','none')
hold on
plot(SIRF(:,1),'linewidth',2,'color','k')
hold on
plot([zeros(hh,1)],'linewidth',1,'LineStyle','--','color','k');
xlim([1 hh])
title('Government Spending')
ylabel('G_{t}')
xlabel('Quarters')
subplot(2,2,2);
conf_area=[SIRF_low(:,2)',fliplr(SIRF_up(:,2)')];
h=fill(x2num,conf_area,graychoice);
set(h,'EdgeColor','none')
hold on
plot(SIRF(:,2),'linewidth',2,'color','k')
hold on
plot([zeros(hh,1)],'linewidth',1,'LineStyle','--','color','k');
xlim([1 hh])
title('Output')
ylabel('Y_{t}')
xlabel('Quarters')
subplot(2,2,3);
conf_area=[SIRF_low(:,3)',fliplr(SIRF_up(:,3)')];
h=fill(x2num,conf_area,graychoice);
set(h,'EdgeColor','none')
hold on
plot(SIRF(:,3),'linewidth',2,'color','k')
hold on
plot([zeros(hh,1)],'linewidth',1,'LineStyle','--','color','k');
xlim([1 hh])
title('Private Consumption')
ylabel('C_{t}')
xlabel('Quarters')
subplot(2,2,4);
conf_area=[SIRF_low(:,4)',fliplr(SIRF_up(:,4)')];
h=fill(x2num,conf_area,graychoice);
set(h,'EdgeColor','none')
hold on
plot(SIRF(:,4),'linewidth',2,'color','k')
hold on
plot([zeros(hh,1)],'linewidth',1,'LineStyle','--','color','k');
xlim([1 hh])
title('Taxes')
ylabel('T_{t}')
xlabel('Quarters')
  2 Comments
Walter Roberson
Walter Roberson on 16 Jan 2019
Edited: Walter Roberson on 16 Jan 2019
Ydata = xlsread('Exam_data_2018.xlsx','Sheet1','b6:b235');
Y = 100*log(Ydata);
% 2 - LOG GOVERNMENT SPENDING
Gdata = xlsread('Exam_data_2018.xlsx','Sheet1','c6:c157');
G = 100*log(Gdata);
6:235 and 6:157 are difference sizes. You will not be able to horzcat those two together.

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 16 Jan 2019
For whatever reason,I keep on getting this annoying error
There is nothing obscure about the error, and it's puzzling why you can't see the problem which is stated clearly. You certainly could have seen the problem straight away by looking at the size of the arrays G, Y, C, T, etc.
It's also obvious just looking at the code:
Ydata = xlsread('Exam_data_2018.xlsx','Sheet1','b6:b235');
Y = 100*log(Ydata);
So, Ydata (and Y) is a column vector of 230 elements (235-6+1).
Gdata = xlsread('Exam_data_2018.xlsx','Sheet1','c6:c157');
G = 100*log(Gdata);
So, Gdata (and G, and all the other vectors actually) is a column vector of 152 elements.
You can concatenate column vectors with different number of rows, as the error message tells you. Either use vectors that are the same size or explain what should be done when they're not the same size.
And proofread your code. I would suspect that you made an error with Cdata. It probably should come from column D, not C. As it is it's the same as Gdata.

More Answers (0)

Categories

Find more on Variables in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!