loop for OLS using fitlm

3 views (last 30 days)
Alexis Villacis
Alexis Villacis on 18 Dec 2018
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

Answers (1)

Felipe Valenzuela Soto
Felipe Valenzuela Soto on 11 Apr 2019
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.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!