Error using regstats (RESPONSES and DATA must have the same number of rows)

Hello!
I must deliver the code for an Econometrics exam and everything is running smoothly except for the following error:
Error using regstats
RESPONSES and DATA must have the same number of rows.
Error in FinancialEconometrics_Homework (line 515)
out=regstats(PInde(:,i),F);
that refers to this part of the code:
out=regstats(PInde(:,i),F);
APToutBtM(i,1:6)=out.beta';
APToutBtM(i,7:12)=out.tstat.t';
APToutBtM(i,13)=out.adjrsquare;
APTresBtM(:,i)=out.r;
I don't know how to solve the problem.
PInde is a 97x25 double matrix and F is a 403x5 double matrix.
Could you please help me?
Thank you

 Accepted Answer

regstats(y,X,model) performs a multilinear regression of the responses in y on the predictors in X. X is an n-by-p matrix of p predictors at each of n observations. y is an n-by-1 vector of observed responses.
So the number of rows of X and y must be the same.
You are using PIndel(:,i) as the y, which will be 97 x 1
You are using F as the X, which will be 403 x 5.
The number of rows of 97 x 1 does not match the number of rows of 403 x 5, so this will fail.

5 Comments

Thank you for your answer Walter,
actually I undestood the problem.
Do you think there's something wrong in creating the matrices?
I used this for PInde
% compute portfolio excess returns
PBtMe=PbyBtM-rf;
PInde=PbyInd-rf;
% CAPM estimation
for i=1:25
y = PInde(:, i);
intercept = ones(size(Me));
intercept = intercept(:,1);
X = [intercept Me];
beta_ols = inv(X' * X) * X' * y;
fitted_y = X * beta_ols;
e = y - fitted_y;
sigma2 = mean( e.^2 );
se_beta_ols = sigma2 * inv(X' * X);
t_stats(i, :) = beta_ols ./ diag(se_beta_ols);
end
% Factor model
% CAPM estimation + Multifactor model estimation
PInde = PInde(36:end, :);
Me = Me(36:end, :);
T = 97;
and this for F
first this
% load data
PbyBtM=readmatrix('Download Data - INDEX_US_S&P US_SPX.csv');
PbyInd=readmatrix('dataset.csv');
FFF = readmatrix('North_America_5_Factors.csv');
% remove dates
PbyBtM=PbyBtM(121:end,6);
PbyInd=PbyInd(271:end,2:end);
FFF=FFF(:,2:end);
and then
% Select market excess returns and risk-free, and all factors
Me=table2array(Mkt_return);
Me=Me(121:end);
rf=0;
F=FFF(37:end,1:end-1);
I would need to know the size of the various matrices.
PInde is 97x25
PbyInd is 132x25
F is 403x5
FFF is 439x6
dataset.csv to create PbyInd is 403x26 with first column dates and first row tags
Download Data - INDEX_US_S&P US_SPX.csv to create FFF is 253x6 with first column dates and fist row tags
intercept = ones(size(Me));
intercept = intercept(:,1);
What is the point of that? Why not just
intercept = ones(size(Me,1),1);
You're right, thank you for your suggestion! I'm really sorry, I'm still learning to write codes in MatLab and often I just make the same things my professor does during the lectures.

Sign in to comment.

More Answers (1)

Categories

Find more on Financial Toolbox in Help Center and File Exchange

Products

Release

R2023b

Tags

Community Treasure Hunt

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

Start Hunting!