How to show r square correlation and RMSE on a scatterplot
Mostrar comentarios más antiguos
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 comentarios
Rik
el 5 de Sept. de 2019
What have you tried yourself?
PARIVASH PARIDAD
el 5 de Sept. de 2019
Respuesta aceptada
Más respuestas (2)
Rik
el 5 de Sept. de 2019
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 comentarios
PARIVASH PARIDAD
el 5 de Sept. de 2019
Rik
el 5 de Sept. de 2019
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
el 5 de Sept. de 2019
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.
ABHILASH SINGH
el 18 de Ag. de 2020
0 votos
For those who is looking for a complete set of code; Just check this

Categorías
Más información sobre Annotations en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!