Fit Plots to A Given Slope

12 views (last 30 days)
Joseph
Joseph on 21 Aug 2013
Hi, I have a set of data that I have plotted using MATLAB. The data I have is supposed to fit to a line with a slope of either 0.5, 1, or 2. Which line the data best fits to tells me what type of system the data is from. So if the data best fits to a line of slope 0.5 I know I am working with system A, if it best fits to a line of slope 1 I am working with system B, and if it best fits to a line of slope 2 I am working with system C. The intercept of the lines does not matter. What I need is a way to fit my data to a line of each of the above mentioned slopes and then to find the regression coefficient to tell which line best fits to the data. So in short I need to know how to fit my data to a line of a slope of 0.5, 1, or 2. Please note that I am working in a log-log plot and that I cannot have an intercept of less than zero. Thank you for your help.
  1 Comment
dpb
dpb on 21 Aug 2013
Is this slope/fit in log-log space I presume?
Why not approach it the other way 'round--just do the fit and find the slope then compare it to the three choices for nearest neighbor --
Simple example...
>> y=rand(10,1); x=1:10; x=x'; % make up some data
>> c=polyfit(x,5*sort(y),1)
c =
0.4067 -0.0011
>> bnear=interp1([0.5 1 2]',[0.5 1 2]',c(1),'nearest','extrap')
bnear =
0.5000
>> c=polyfit(x,10*sort(y),1)
c =
0.8134 -0.0022
>> bnear=interp1([0.5 1 2]',[0.5 1 2]',c(1),'nearest','extrap')
bnear =
1
>> c=polyfit(x,20*sort(y),1)
c =
1.6268 -0.0045
>> bnear=interp1([0.5 1 2]',[0.5 1 2]',c(1),'nearest','extrap')
bnear =
2
>>
Easy enough to vectorize, of course...

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 21 Aug 2013
x = [....]; %the vector of x values that "data" exist at
sd_half = std(data - 0.5 * x);
sd_one = std(data - x);
sd_two = std(data - 2 * x);
I would suggest, though, trying
coeffs = polyfit(x, data, 2);
slope = coeffs(1);
You might to use the three-output version of polyfit to have centering and scaling done.
  4 Comments
Joseph
Joseph on 24 Aug 2013
Edited: Joseph on 24 Aug 2013
Well the data should fit a line with one of the given slopes on the log-log plot. So I have a log-log plot of the data. I then have three line with slopes 0.5, 1, and 2. The y intercept of the lines doesn't matter. I then have to find which of these lines best fits the data. The best way of doing this I have so far is to basically eye the data and the three lines and take a guess as to which is the best fit. This is highly impractical though. I need a way to find which of these lines fits the data best. I could easily fit a line of any slope and intercept to the data but that is not what I need. I know the fit won't be perfect but usually at least part of the data sets will conform to one of the given lines. I hope that clarifies things and sorry if my original question was unclear. Thank you for your help so far.
dpb
dpb on 24 Aug 2013
Edited: dpb on 25 Aug 2013
What's wrong w/ my solution--just apply it in the log-log space if that's where the correlation is.
Walter's works, too, but seems simpler to me to do the match to the nearest computed slope rather than find the minimum variance his does (didn't think about the assumed slope of unity still leading to the smallest variance w/ choice of exponent was what got me initially) as only compute the one slope and a lookup as opposed to three variances and the comparison...overall, probably not a great deal of difference, though.
Pick one (or both and compare) and go with it... :)

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!