Polyfit returns the wrong correlation coeffcient when I try do a linear regression

조회 수: 14 (최근 30일)
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
A LL
A LL 2020년 11월 12일
Hi John,
I am sorry for the confucion, what I mean to do is to do a linear regression with my data by computing the regression coefficient (I think this is the correct expression).
When I plot the linear regression obtained from the polyfit function it does not seem to properly fit my data and I do not understand why this is happening because the data are correctly plot on my figure.
Thanks
John D'Errico
John D'Errico 2020년 11월 12일
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?

댓글을 달려면 로그인하십시오.

채택된 답변

Jon
Jon 2020년 11월 11일
편집: Jon 2020년 11월 11일
I don't think you are plotting your fitted y values
For example where you have
fFMA = polyval(pFMA,subwFMA(:)); %linear regression function
figure(1)
plot(subwFMA(:),subssrdFMA(:),'o',subwFMA(:),yssrdFMA,'-');
I think you want
yssrdfFMA = polyval(pFMA,subwFMA(:)); %linear regression function
figure(1)
plot(subwFMA(:),subssrdFMA(:),'o',subwFMA(:),yssrdFMA,'-');
and where you have
figure(2)
plot(subwFMA(:),subssrdFMA(:),'o',subwFMA(:),fFMA,'-');
I think you want
figure(2)
plot(subwFMA(:),subssrdFMA(:),'o',subwFMA(:),yssrdFMA,'-');
  댓글 수: 5
Jon
Jon 2020년 11월 13일
편집: Jon 2020년 11월 13일
I agree, I would expect your fit line to go more through the cloud even if there were very little correlation. So I think you also have a programming error.
You said you made some edits to your code since the original one you posted. Can you please provide your current code (please use the code button on the MATLAB answers toolbar to get it nicely formatted). Also if you can provide your input file, as John D'Errico suggested that would be even better.
A LL
A LL 2020년 11월 13일
편집: A LL 2020년 11월 14일
Ok, thanks again for your help!
I will go over my code one more time and if I can not find the error I will post it here. :)
update: I was able to solve my problem. I had a mistake in my code. Thanks again for your help!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by