How to show r square correlation and RMSE on a scatterplot

I have 2 colmuns in my excel file and I need to make the scatterplot which I wrote:
dataset = xlsread ('data.xlxs');
x = dataset (:,1);
y = dataset (:,2);
plot (x, y, '*')
title('scatterplot')
xlable('estimated')
ylable('measured')
Now I need to fit a linear regression line on the plot and display the Y=ax+b equation along with R square and RMSE values on the plot.
Can anyone help me? Thanks

2 件のコメント

Rik
Rik 2019 年 9 月 5 日
What have you tried yourself?
PARIVASH PARIDAD
PARIVASH PARIDAD 2019 年 9 月 5 日
I tried the basic fit tool which exists after you plot the scatter to show R2 but I want to write a code for it and for RMSE, I do not how to do it

サインインしてコメントする。

 採用された回答

Petter Stefansson
Petter Stefansson 2019 年 9 月 5 日

4 投票

Given your x and y vectors, perhaps this is what you are looking for?
plot(x, y, '*','displayname','Scatterplot')
title('scatterplot')
xlabel('estimated')
ylabel('measured')
% Fit linear regression line with OLS.
b = [ones(size(x,1),1) x]\y;
% Use estimated slope and intercept to create regression line.
RegressionLine = [ones(size(x,1),1) x]*b;
% Plot it in the scatter plot and show equation.
hold on,
plot(x,RegressionLine,'displayname',sprintf('Regression line (y = %0.2f*x + %0.2f)',b(2),b(1)))
legend('location','nw')
% RMSE between regression line and y
RMSE = sqrt(mean((y-RegressionLine).^2));
% R2 between regression line and y
SS_X = sum((RegressionLine-mean(RegressionLine)).^2);
SS_Y = sum((y-mean(y)).^2);
SS_XY = sum((RegressionLine-mean(RegressionLine)).*(y-mean(y)));
R_squared = SS_XY/sqrt(SS_X*SS_Y);
fprintf('RMSE: %0.2f | R2: %0.2f\n',RMSE,R_squared)

6 件のコメント

PARIVASH PARIDAD
PARIVASH PARIDAD 2019 年 9 月 6 日
Thanks so much to both of you.
Can you please tell me how to show the calculated RMSE and R2 also on the plot please?
Petter Stefansson
Petter Stefansson 2019 年 9 月 6 日
If you want it shown in the title of the figure you could for example write:
title(sprintf('RMSE: %0.2f | R2: %0.2f', RMSE, R_squared))
If you want it shown in the legend along with the regression equation:
plot(x,RegressionLine,'displayname',sprintf('Regression line (y = %0.2f*x + %0.2f | RMSE %0.2f | R2: %0.2f)',b(2),b(1),RMSE,R_squared))
Or if you want it shown floating in a position of your choice in the figure:
text(xloc,yloc,sprintf('RMSE: %0.2f | R2: %0.2f', RMSE, R_squared))
% xloc and yloc should be the x and y coordinates where you want the text
PARIVASH PARIDAD
PARIVASH PARIDAD 2019 年 9 月 6 日
Thanks you very much Petter Stefansson.
PARIVASH PARIDAD
PARIVASH PARIDAD 2019 年 9 月 20 日
Dear Petter Stefansson, can I ask if i need to plot the line of 45 degree instead of regression line, how may I do it?
Petter Stefansson
Petter Stefansson 2019 年 9 月 20 日
編集済み: Petter Stefansson 2019 年 9 月 20 日
If you mean you want a “1/1 line", i.e. a line that increases by the same amount in both the x and y direction and just cuts the figure in a 45° angle, then you can just give the plot command the same input for both the x and y values. For example, to plot a 1/1 line between -100 and 100:
plot([-100 100],[-100 100],'displayname','1/1 line')
However, this line may not visually appear as if it has a 45° slope unless the x and y axis are displayed the same way. So you will probably have to use something like this in order for it to look right:
plot([-100 100],[-100 100],'displayname','1/1 line')
axis equal
xlim([-0.05 0.6])
ylim([-0.05 0.6])
PARIVASH PARIDAD
PARIVASH PARIDAD 2019 年 9 月 20 日
Thanks so much

サインインしてコメントする。

その他の回答 (2 件)

Rik
Rik 2019 年 9 月 5 日

1 投票

With the code below you can determine a fitted value for y. Now it should be easy to calculate the Rsquare and RMSE. Let me know if you're having any issues.
x=sort(20*rand(30,1));
y=4*x+14+rand(size(x));
plot(x,y,'.')
f=@(b,x) b(1)*x+b(2);%linear function
guess_slope=(max(y)-min(y))/(max(x)-min(x));
guess_intercept=0;
b_init=[guess_slope;guess_intercept];
OLS=@(b,x,y,f) sum((f(b,x) - y).^2);%objective least squares
opts = optimset('MaxFunEvals',50000, 'MaxIter',10000);
% Use 'fminsearch' to minimise the 'OLS' function
b_fit=fminsearch(OLS,b_init,opts,x,y,f);
x_fit=x;
y_fit=f(b_fit,x_fit);

3 件のコメント

PARIVASH PARIDAD
PARIVASH PARIDAD 2019 年 9 月 5 日
Thanks a lot Rik. I am new to Matlab. I have to study more to even fully get your code. I do not actually how to calculate R2 and RMSE.
Rik
Rik 2019 年 9 月 5 日
Do you know how to calculate the Rsquare and RMSE with pen and paper? Start there and then implement it. Wikipedia can be a great starting point for situations like this.
As for my code, there isn't really a need to fully understand how an OLS function itself works, it is just one example of a cost function. Every fitting method has some function that describes how well a function fits that data. The fitting process then consists of trying to find parameters that will minimize the cost function. (this is not specific to Matlab)
The fminsearch function tries to minimize a function. This function can have multiple inputs, but the first input must be a vector or matrix with your parameters.
Rik
Rik 2019 年 9 月 5 日
Since Petter Stefansson wrote a complete answer, I'll attach a wrapper for fminsearch I sometimes use, which will also return goodness of fit parameters. I still encourage you to try to find out how it works with pen and paper, attempt to implement it yourself, and see if you get to the same code as me or Petter.

サインインしてコメントする。

カテゴリ

Community Treasure Hunt

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

Start Hunting!

Translated by