Polyfit returns the wrong correlation coeffcient when I try do a linear regression
Show older comments
Hello :)
I am trying to do a linear regression for a set of data, but the regression coefficient returned by matlab polyfit don't fit my data.
The data is plot just fine but the linear regression function seems very odd...
Please see the attached figure
I tried to plot with polyval (option 1) and with a linear relation (y=ax+b) (option 2) but it does not make any difference... The regression coefficient returned are just no the good ones...
Can anyone help me?
subwFMA = w10MeanFMA(521:771, 41:161); %sub mean wind speed
subssrdFMA = ssrdMeanFMA(521:771, 41:161); %sub mean solar radiation
%OPTION 1
pFMA = polyfit(subwFMA(:),subssrdFMA(:),1); %find regression coefficient a and b for a linear regression
fFMA = polyval(pFMA,subwFMA(:)); %linear regression function
figure(1)
plot(subwFMA(:),subssrdFMA(:),'o',subwFMA(:),fFMA,'-');
%OPTION 2
polyfitFMA = polyfit(subwFMA(:),subssrdFMA(:),1); %find correletation coefficient a and b for a linear regression
aFMA = polyfitFMA(1,1); %coeff a
bFMA = polyfitFMA(1,2); %coeff b
yssrdFMA = aFMA*subwFMA(:) + bFMA; %linear regression function
figure(2)
plot(subwFMA(:),subssrdFMA(:),'o',subwFMA(:),yssrdFMA,'-');
I also tried to reverse my x and y data in polyfit but it makes things even worse!
Thanks
3 Comments
John D'Errico
on 11 Nov 2020
Edited: John D'Errico
on 11 Nov 2020
Um, I think you misunderstand what is a correlation coefficient. You seem to think it is either the linear coefficient or the constant term in the regression, both of which are wrong. Nothing in what you did has returned the correlation coefficient.
I think when you use the words correlation coefficient, you think you are referring to the coefficients estimated from a linear regression. That is NOT a correlation coefficient.
As far as what you did wrong here, that is impossible to tell, since we lack your data.
A LL
on 12 Nov 2020
John D'Errico
on 12 Nov 2020
Again, we don't have your data. And it seems like you fit only a small portion of the data. So how can we possibly know what you did wrong?
Accepted Answer
More Answers (0)
Categories
Find more on Linear and Nonlinear Regression in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!