Calculating linear fit with respect from origin (0,0)

조회 수: 8 (최근 30일)
Sharah
Sharah 2016년 9월 28일
편집: Massimo Zanetti 2016년 9월 28일
I have a data of DataX and Data Y, which is plotted in the blue line as in picture.I am trying to analyse how much the blue line is deviated from the black dotted line x and y = [0:1:7]. The thing is, if i want to compare the linear fit, the linear fit of my data does not start from 0 (yellow line).
Can anyone suggest what kind of metric that I can use here? If possible, I would like a way to measure say for example if the fit of my data falls below the dotted line, the value will be <0, and if it is above the >0.
  댓글 수: 2
Massimo Zanetti
Massimo Zanetti 2016년 9월 28일
There is no data above the dotted line. Can you explain better? Do you want to measure the distance of the Data with respect to the Linear Fit Data?
Sharah
Sharah 2016년 9월 28일
편집: Sharah 2016년 9월 28일
Say, if I have data line the blue line, but it falls above the dotted line. What I want to do is to calculate how much the blue data is different from the dotted line.
The following is one of the method that I used, but not sure if this is accurate
function [rsq] = linearity(xData, yData)
xlin = 0:max(xData)/(length(xData)-1):max(xData);
ylin = 0:max(xData)/(length(xData)-1):max(xData);
p = polyfit(xlin, ylin,1);
yfit = p(1) * xlin +p(2); %similar to polyval(p, x)
yresid = yData - yfit';
SSresid = sum(yresid.^2);
SStotal = (length(yData)-1)*var(yData);
rsq= 1-SSresid/SStotal;
In which I calculated the Rsquare of my data relative to the linear fit of blue line? Will that be correct?

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

채택된 답변

Massimo Zanetti
Massimo Zanetti 2016년 9월 28일
편집: Massimo Zanetti 2016년 9월 28일
Ok, you just have to use the right statistics. Look here:
%random points
x=1:10;
y=rand(1,10);
%fit data
p=polyfit(x,y,1);
yfit=p(2)+x*p(1);
%residuals
yres = y-yfit;
%deviation from mean
ymean = mean(y(:));
ydev = y-ymean;
%compute R2
SSR = sum(yres.^2);
SST = sum(ydev.^2);
R2 = 1-SSR/SST;
%plot fitting
plot(x,y,'b',x,yfit,'r');
The R2 number is Rsquared statistics explaining how the fit line well represents the points.
  댓글 수: 2
Sharah
Sharah 2016년 9월 28일
The naswer that you are suggesting over here would calculate how well the data explain to the polyfit of your data. what I am trying to do is to see how well the data is explaining to the linear line of for example xlin=ylin =1:7 (which will be the same size with the data)
function [rsq] = linearity(xData, yData)
xlin = 0:max(xData)/(length(xData)-1):max(xData);
ylin = 0:max(xData)/(length(xData)-1):max(xData);
p = polyfit(xlin, ylin,1);
yfit = p(1) * xlin +p(2); %similar to polyval(p, x)
yresid = yData - yfit';
SSresid = sum(yresid.^2);
SStotal = (length(yData)-1)*var(yData);
rsq= 1-SSresid/SStotal;
will this one works? instead of taking the Rsquared to the polyfit of orginial data, I take 'rsquared' to the linear line that I want (the reference line)
Massimo Zanetti
Massimo Zanetti 2016년 9월 28일
편집: Massimo Zanetti 2016년 9월 28일
Ok, so it is much simple. Just replace the polyfit line with reference one.
%random points
x=1:10;
y=rand(1,10);
yref=1:10;
%residuals
yres_ref = y-yref;
%deviation from mean
ymean = mean(y(:));
ydev = y-ymean;
%compute R2
SSR = sum(yres_ref.^2);
SST = sum(ydev.^2);
R2 = 1-SSR/SST;
%plot fitting
plot(x,y,'b',x,yref,'r');

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by