How to calculate R^2 using 1 - (SSR/SST)? For normal fit distribution.

22 views (last 30 days)
Hello, I have used the fitlm function to find R^2 (see below), to see how good of a fit the normal distribution is to the actual data. The answer is 0.9172.
How can I manually calculate R^2?
R^2 = 1 - (SSR/SST) or in other words 1 - ((sum(predicted - actual)^2) / ((sum(actual - mean of actual)^2)). I am having a hard time getting the correct answer.
Table = readtable("practice3.xlsx");
actual_values = Table.values;
actual_values = sort(actual_values);
normalfit = fitdist(actual_values,'Normal'); % fit the normal distribution to the data
cdfplot(actual_values); % Plot the empirical CDF
x = 0:2310;
hold on
plot(x, cdf(normalfit, x), 'Color', 'r') % plot the normal distribution
hold off
grid on
nonExceedanceProb = sum(actual_values'<=actual_values,2)/numel(actual_values);
Table.nonExceedanceProb=nonExceedanceProb;
mdl=fitlm(cdf(normalfit, actual_values),Table.nonExceedanceProb);
mdl.Rsquared.Ordinary % R^2
ans = 0.9172
mdl.SSR
ans = 0.7567
mdl.SST
ans = 0.8250
% How can I manually calculate R^2 (or SSR and SST)?
% SSR = sum(((predicted data - actual data).^2))
% TSS = sum((actual data - mean(actual data)).^2)
% Rsquared = 1 - SSR/TSS

Accepted Answer

Torsten
Torsten on 15 Feb 2023
Edited: Torsten on 15 Feb 2023
In my opinion, it does not make sense to fit a linear function to the value pairs (cdf(normalfit, actual_values),Table.nonExceedanceProb) as you do above.
In principle, the blue points below should lie on the red line. This would mean that the empirical cdf is perfectly reproduced by the normal distribution.
So if you really want to compare the two distributions, you should consider the distance of the blue points (achieved quality of fit) to the red line (perfect fit).
Table = readtable("practice3.xlsx");
actual_values = Table.values;
actual_values = sort(actual_values);
normalfit = fitdist(actual_values,'Normal'); % fit the normal distribution to the data
nonExceedanceProb = sum(actual_values'<=actual_values,2)/numel(actual_values);
hold on
plot(nonExceedanceProb,cdf(normalfit, actual_values),'o')
plot([0 1],[0 1])
xlabel('P(empirical)')
ylabel('P(normal)')
hold off
grid on
  12 Comments
Macy
Macy on 21 Feb 2023
Yes, Rsquared1 he said is the "pearson correlation coefficient" and Rsquared2 is the "coefficient of determination".
Torsten
Torsten on 21 Feb 2023
corr(yi,fi) is the pearson correlation coeffcient - I don't know why he wanted to square it.
Anyway: congratulations that you finished your assignment successfully.

Sign in to comment.

More Answers (0)

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!