Filter löschen
Filter löschen

loop for OLS using fitlm

2 Ansichten (letzte 30 Tage)
Alexis Villacis
Alexis Villacis am 18 Dez. 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

Antworten (1)

Felipe Valenzuela Soto
Felipe Valenzuela Soto am 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.

Produkte


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by