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
figurescatter(x,y,'filled'); grid on; hold onplot(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];figureX = 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
%figureX = 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?
Best Answer