Warning: The assignment added rows to the table, but did not assign values to all of the table's existing
조회 수: 39 (최근 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개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Tables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!