Warning: The assignment added rows to the table, but did not assign values to all of the table's existing
23 ビュー (過去 30 日間)
古いコメントを表示
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?
0 件のコメント
採用された回答
Voss
2022 年 4 月 11 日
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
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 件のコメント
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!