How to calculate the coefficient of determination R^2 of a Neural Network?
349 次查看(过去 30 天)
显示 更早的评论
I want to calculate the coefficient of determination R^2 of a Neural Network by myself.
This is the Regression plot that Neural Network Training Tool:
but I want to calculate it in a way so I can "confirm" what I see on NN Training Tool.
As you can see below I have plot the Target (X) and the Prediction (Y) as Y = A*X
but the Regression Plot is way different, Prediction (Y) = 0.99*Target+0.0044 as Y=A*X+B
I understand that Weights and Biases are A and B respectively, but how can I find it and do it by myself, since they are Weights on Input Layer and Hidden Layer as well.
Also how can I draw the line that represents the middle of my data points?
figure;
plot(Output_Train,pFNN40_Train,'x');
title('Coefficient of Determination R^2');
legend('Train');
xlabel('Target');
ylabel('Prediction');
axis auto;
grid on;
0 个评论
采纳的回答
the cyclist
2022-12-4
The formula is in the documentation here (for fitlm).It is not always the best goodness-of-fit measure for all models, but you should always be able to calculate it like this:
% Some pretend predicted and actual Y values
y_actual = [2.1; 3.2; 5.3; 7.1; 11.9];
y_predicted = [2.9; 2.7; 5.0; 7.2; 11.1];
% Plot them
plot(y_actual,y_predicted,'o')
% Sum of squared residuals
SSR = sum((y_predicted - y_actual).^2);
% Total sum of squares
TSS = sum(((y_actual - mean(y_actual)).^2));
% R squared
Rsquared = 1 - SSR/TSS
0 个评论
更多回答(1 个)
Joan M. Maura
2023-2-14
Does the R in the regression plots for validation, test, training and all mean R^2 or do I have to square those results?
1 个评论
the cyclist
2023-2-14
编辑:the cyclist
2023-2-14
In general, and certainly in any output from MathWorks, I would definitely not expect someone to write R when they mean R^2.
Also, you need to be cautious that the coefficient of determination (even though it is often call R^2) is not always equal to the correlation coefficient (R) squared. You can even bulding models in which R^2 is negative.
If you need R^2, you need to calculate it, not just square R.
Here is a contrived example, to illustrate the point:
x = [1.0; 2.0; 3.0; 4.0; 5.0; 6.0];
y_actual = [2.5; 4.1; 7.0; 7.7; 10.7; 12.2];
y_predicted = [2; 3; 4; 5; 6; 7]; % I've chosen these terrible prediction on purpose, to illustrate my point
figure
hold on
plot(x,y_actual,".","MarkerSize",24)
plot(x,y_predicted,".-","MarkerSize",8)
legend(["data","prediction"],"Location","NorthWest")
correlation = corr(x,y_predicted)
SSR = sum((y_predicted - y_actual).^2);
TSS = sum(((y_actual - mean(y_actual)).^2));
Rsquared = 1 - SSR/TSS
Notice that the correlation is perfect, while the R^2 is terrible. If I had my the prediction smaller (moving them all down by, say 2 units, the R^2 would have gone negative).
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Sequence and Numeric Feature Data Workflows 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!