R-Squared for the robust linear regression

조회 수: 43(최근 30일)
Yaser Khojah
Yaser Khojah 2021년 1월 25일
답변: Gaurav Garg 2021년 1월 28일
Hello there, I am trying to calculate the R-Squared by using the linear regression function (regress) and robust linear regression. For the linear regression function (regress), it can be estimated directly from the function. However, for the robust case, it is not done directly. I saw some people recommended using different approach as below. I do not see the final conclusion as which method is the right one and should be compared against the linear regression.
Any help will be very welcome
x = (1:10)';
y = 10 - 2*x + randn(10,1);
y(10) = 0;
% R-Squared for Regression
[b_ls,~,~,~,stats_linreg] = regress(y,[ones(size(x)) x]);
rquare_linreg = stats_linreg(1);
% R-Squared for Robusfit by using the corrlation^2 (First Approch)
[b_rob, stats_rob] = robustfit(x,y);
rsquare_robustfit = corr(y,b_rob(1)+b_rob(2)*x)^2;
% R-Squared for Robusfit by calculating the error (Second Approch)
sse = stats_rob.dfe * stats_rob.robust_s^2;
phat = b_rob(1) + b_rob(2)*x;
ssr = norm(phat-mean(phat))^2;
possible_rsquare_robustfit = 1 - sse / (sse + ssr);
% plot the data and fitting curves
scatter(x,y,'filled'); grid on; hold on
legend('Data','Ordinary Least Squares','Robust Regression')
% compare by bar chart.
Fit_Re = [stats_linreg(1),rsquare_robustfit, possible_rsquare_robustfit];
X = categorical({'Linear Regression','Reboust Regression','possible - R2 - robustfit'});
X = reordercats(X,{'Linear Regression','Reboust Regression','possible - R2 - robustfit'});
% Using the curve fitting App
[mdl] = fitlm(x,y,'robustOpts','on');
w = mdl.Robust.Weights;
y_estimate = mdl.Coefficients.Estimate(2)*x + mdl.Coefficients.Estimate(1);
sse = sum( w .* (y - y_estimate).^2 ); % Sum of Squares due to Error //// Sum of Squares of residuals
sst = sum( w .* (y - mean(y)).^2 ); % total sum of squares
ssr = sum( w .* (y_estimate-mean(y)).^2); % sum of squares of regression
r2 = 1-sse/sst
mdl_off = fitlm(x,y,'robustOpts','off');
mdl_on = fitlm(x,y,'robustOpts','on');
mdl_bis = fitlm(x,y,'RobustOpts','bisquare');
mdl_R = [mdl_off.Rsquared; mdl_on.Rsquared; mdl_bis.Rsquared];
C = cell2mat(struct2cell(mdl_R));
B = reshape(C,2,[])';
% Ordinary / Adjusted
% 0.6784 / 0.6382
% 0.9099 / 0.8986
% 0.9099 / 0.8986
X = categorical({'Linear - Robust off','Robust on','bisquare'});
X = reordercats(X,{'Linear - Robust off','Robust on','bisquare'});
Now which out of these is the right way to estimate R-Squared for the robust linear regression?

채택된 답변

Gaurav Garg
Gaurav Garg 2021년 1월 28일
Hi Yaser,
You can go through another similar case here to solve your query.

추가 답변(0개)

Community Treasure Hunt

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

Start Hunting!

Translated by