필터 지우기
필터 지우기

loop for OLS using fitlm

조회 수: 5 (최근 30일)
Alexis Villacis
Alexis Villacis 2018년 12월 18일
답변: Felipe Valenzuela Soto 2019년 4월 11일
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
Felipe Valenzuela Soto 2019년 4월 11일
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에서 Resampling Techniques에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by