R-Squared for the robust linear regression

15 ビュー (過去 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
Yaser
%%
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
figure
scatter(x,y,'filled'); grid on; hold on
plot(x,b_ls(1)+b_ls(2)*x,'r','LineWidth',2);
plot(x,b_rob(1)+b_rob(2)*x,'g','LineWidth',2)
legend('Data','Ordinary Least Squares','Robust Regression')
% compare by bar chart.
Fit_Re = [stats_linreg(1),rsquare_robustfit, possible_rsquare_robustfit];
figure
X = categorical({'Linear Regression','Reboust Regression','possible - R2 - robustfit'});
X = reordercats(X,{'Linear Regression','Reboust Regression','possible - R2 - robustfit'});
bar(X,Fit_Re);
% 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_off.Rsquared;
mdl_on = fitlm(x,y,'robustOpts','on');
mdl_on.Rsquared;
mdl_bis = fitlm(x,y,'RobustOpts','bisquare');
mdl_bis.Rsquared;
%
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
%
figure
X = categorical({'Linear - Robust off','Robust on','bisquare'});
X = reordercats(X,{'Linear - Robust off','Robust on','bisquare'});
bar(X,B(:,1));
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 件)

カテゴリ

Help Center および File ExchangeLinear and Nonlinear Regression についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by