loop for OLS using fitlm

2 次查看(过去 30 天)
Alexis Villacis
Alexis Villacis 2018-12-18
I am trying to estimate standar errors of my betas using bootstraping. I am using fitlm.
My Y and x are two vectors of dimensions 202x1. How can I program the loop to save the betas in a matrix? I should have 2 betas for each of the 1000 models.
My code is as follows:
%Get Initial values
OLS=fitlm(x,Y)
%save residuals
res=OLS.Residuals.Raw;
%generate y hat
ypred=predict(OLS1,x)
%bootstrap 1000 draws of the residuals with replacement
resboot=res(randi(202,202,1000))
%generate 1000 new draws of Y
yboot=ypred+resboot
% Do the fits
for i = 1:1000
mdl= fitlm(x,yboot(:,i));
end

回答(1 个)

Felipe Valenzuela Soto
Hi Alexis,
Maybe you already solve it, but i think you should first create two zeros vector of 202x1 to asign the estimated betas and residuals from the loop using the fitlm function. I have used this method in my codes for yield curve term structure estimation, more related to financial application. I hope it works for you.
beta = zeros(size(X,1),1);
residuals = zeros(size(X,1),numel(X));
%loop
for i = 1:size(X,1)
EstOLS = fitlm(X, Y','Intercept", false); %Or true in you want
beta (i ; :) = EstOLS.Coefficients.Estimate';
residuals(i ; :) = EstOLS.Residuals.Raw';
end
Good luck!
Felipe Valenzuela.

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by