Warning: The assignment added rows to the table, but did not assign values to all of the table's existing

40 Ansichten (letzte 30 Tage)
I have the following code that generates the Warning
nmonths = 100; % use 100 months worth of data to estimate the betas
for i = 1:nstocks
for j = 0:(nmonths-60) % 5 x 12 = 60 months of estimation window and
% obtain 41 observations of beta for each stock i
mdlbetas = fitlm(monthlyX((1+j):(60+j),:),monthlyY((1+j):(60+j),i));
Betas(i,j+1) = mdlbetas.Coefficients(2,1);
end
end
I get the warning: Warning: The assignment added rows to the table, but did not assign values to all of the table's existing
How do I avoid this warning and should I even worry about this if I get the output I desire?

Akzeptierte Antwort

Voss
Voss am 11 Apr. 2022
The warning happens because the table Betas gets a new row added each time the j loop runs its first iteration (except when i is 1, since in that case there are no variables in the table Betas yet), but only the value of the first column/variable in the new row is set (the rest default to 0).
nmonths = 62;
nstocks = 2;
monthlyX = rand(nmonths,1);
monthlyY = rand(nmonths,nstocks);
Betas = table();
for i = 1:nstocks
for j = 0:(nmonths-60) % 5 x 12 = 60 months of estimation window and
% obtain 41 observations of beta for each stock i
mdlbetas = fitlm(monthlyX((1+j):(60+j),:),monthlyY((1+j):(60+j),i));
fprintf('i = %d, j = %d:',i,j);
Betas(i,j+1) = mdlbetas.Coefficients(2,1);
disp('Betas = ');
disp(Betas);
end
end
i = 1, j = 0:
Betas =
Var1 ________ 0.065973
i = 1, j = 1:
Betas =
Var1 Var2 ________ ________ 0.065973 0.058035
i = 1, j = 2:
Betas =
Var1 Var2 Var3 ________ ________ ________ 0.065973 0.058035 0.040655
i = 2, j = 0:
Warning: The assignment added rows to the table, but did not assign values to all of the table's existing variables. Those variables are extended with rows containing default values.
Betas =
Var1 Var2 Var3 _________ ________ ________ 0.065973 0.058035 0.040655 -0.083345 0 0
i = 2, j = 1:
Betas =
Var1 Var2 Var3 _________ ________ ________ 0.065973 0.058035 0.040655 -0.083345 -0.10717 0
i = 2, j = 2:
Betas =
Var1 Var2 Var3 _________ ________ ________ 0.065973 0.058035 0.040655 -0.083345 -0.10717 -0.13571
You can avoid this warning by initializing the table Betas to have the full number of rows and columns it will need.
However, maybe Betas doesn't need to be a table at all; maybe it can be a numeric matrix:
Betas = NaN(nstocks,nmonths-60+1);
for i = 1:nstocks
for j = 0:(nmonths-60) % 5 x 12 = 60 months of estimation window and
% obtain 41 observations of beta for each stock i
mdlbetas = fitlm(monthlyX((1+j):(60+j),:),monthlyY((1+j):(60+j),i));
% Betas(i,j+1) = mdlbetas.Coefficients(2,1);
Betas(i,j+1) = mdlbetas.Coefficients{2,1};
end
end
disp(Betas);
0.0660 0.0580 0.0407 -0.0833 -0.1072 -0.1357

Weitere Antworten (0)

Kategorien

Mehr zu Tables finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by