Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to plot 2 fitting lines in 1 dataset?

Subject: How to plot 2 fitting lines in 1 dataset?

From: Kuo-Hsien

Date: 5 Jun, 2009 17:21:02

Message: 1 of 8

Hi all,

My dataset can be categorized into 2 groups by eyes, so can I plot 2 fitting lines for this dataset? Right now, I just use polyfit function, but it fits for all data points. Thanks.

Michael

Subject: How to plot 2 fitting lines in 1 dataset?

From: Lorenzo Guerrasio

Date: 5 Jun, 2009 20:18:02

Message: 2 of 8


you can use logical indexing to divide your data set and make a fit for each group.

regards
"Kuo-Hsien" <mchangks@hotmail.com> wrote in message <h0bk5u$cha$1@fred.mathworks.com>...
> Hi all,
>
> My dataset can be categorized into 2 groups by eyes, so can I plot 2 fitting lines for this dataset? Right now, I just use polyfit function, but it fits for all data points. Thanks.
>
> Michael

Subject: How to plot 2 fitting lines in 1 dataset?

From: Kuo-Hsien

Date: 6 Jun, 2009 22:13:01

Message: 3 of 8

Hi Lorenzo,
What do you just mean logical indexing? My data point is spread out from 0-50 with temperature, but two group of data points increase with me in different rate with temperature.

Subject: How to plot 2 fitting lines in 1 dataset?

From: John D'Errico

Date: 7 Jun, 2009 12:31:01

Message: 4 of 8

"Kuo-Hsien" <mchangks@hotmail.com> wrote in message <h0epld$lkf$1@fred.mathworks.com>...
> Hi Lorenzo,
> What do you just mean logical indexing? My data point is spread out from 0-50 with temperature, but two group of data points increase with me in different rate with temperature.

Do you know where the break occurs?

If you do, then split this into two problems. In
fact they can be solved in one estimation step.

If not then you need to use a tool that can find the
location of the break. This is sometimes known as a
free knot spline, here a piecewise linear spline. Either
problem can be solved easily enough, but the solution
depends on your answer.

So, do you know the location of the break or must
it be estimated?

John

Subject: How to plot 2 fitting lines in 1 dataset?

From: John D'Errico

Date: 7 Jun, 2009 13:01:02

Message: 5 of 8

"John D'Errico" <woodchips@rochester.rr.com> wrote in message <h0gbu5$ssj$1@fred.mathworks.com>...
> "Kuo-Hsien" <mchangks@hotmail.com> wrote in message <h0epld$lkf$1@fred.mathworks.com>...
> > Hi Lorenzo,
> > What do you just mean logical indexing? My data point is spread out from 0-50 with temperature, but two group of data points increase with me in different rate with temperature.
>
> Do you know where the break occurs?
>
> If you do, then split this into two problems. In
> fact they can be solved in one estimation step.
>
> If not then you need to use a tool that can find the
> location of the break. This is sometimes known as a
> free knot spline, here a piecewise linear spline. Either
> problem can be solved easily enough, but the solution
> depends on your answer.
>
> So, do you know the location of the break or must
> it be estimated?
>
> John

Let me be more explicit, since I know you will ask
for more information.

x = rand(100,1);
y = (1 + x).*(x<0.4) + (-2*x + 2.2).*(x>=0.4) + randn(size(x))/20;
plot(x,y,'o')

Here, you and I know the break occurs at 0.4.
Can we estimate the coefficients of the lines?

IFF we knew the break location, then we would do this:

k = (x <= 0.4); % A logical varable
coef = [k,x.*k,~k,x.*~k]\y

coef = [k,x.*k,~k,x.*~k]\y
coef =
         0.981257860141531
          1.07669216114181
          2.20227048054644
         -2.01520597681586

With no error, the coefficients should have been
[1; 1; 2.2; -2], so we did rather well here.

Can I do as well if I do not assume the location of
the break is known? In fact, I cannot do quite as
well in general, since we must estimate an extra
parameter. We can do well enough though.

coefest = @(B) [(x<B),x.*(x<B),(x>=B),x.*(x>=B)]\y;
preds = @(B) [(x<B),x.*(x<B),(x>=B),x.*(x>=B)]*coefest(B);

BreakPoint = fminbnd(@(B) sum((y - preds(B)).^2),0,1)
BreakPoint =
         0.397098484667164

coefest(BreakPoint)
ans =
          0.98340320488404
          1.06085977689174
          2.20816686087013
          -2.0226000312523

See that I've used the limits of the data itself to
define the range over which fminbnd will be allowed
to search for the break point.

HTH,
John

Subject: How to plot 2 fitting lines in 1 dataset?

From: Lorenzo Guerrasio

Date: 7 Jun, 2009 16:54:01

Message: 6 of 8

I'm not sure whether the data of the guy had a kink...it seems more that he has two data set, i.e. x1 and x2, with two different relation, i.e. y=x1 and y=3*x2, intermingled in one data set y,x
If that is the case, you can find (in a more or less mathematical way) a line dividing the two data set, i.e. y=2*x2.
Then you define the two data set as
D1=y./x<=2;
D2=y./x>=2;

That is what I got from the problem


"John D'Errico" <woodchips@rochester.rr.com> wrote in message <h0gdme$lhi$1@fred.mathworks.com>...
> "John D'Errico" <woodchips@rochester.rr.com> wrote in message <h0gbu5$ssj$1@fred.mathworks.com>...
> > "Kuo-Hsien" <mchangks@hotmail.com> wrote in message <h0epld$lkf$1@fred.mathworks.com>...
> > > Hi Lorenzo,
> > > What do you just mean logical indexing? My data point is spread out from 0-50 with temperature, but two group of data points increase with me in different rate with temperature.
> >
> > Do you know where the break occurs?
> >
> > If you do, then split this into two problems. In
> > fact they can be solved in one estimation step.
> >
> > If not then you need to use a tool that can find the
> > location of the break. This is sometimes known as a
> > free knot spline, here a piecewise linear spline. Either
> > problem can be solved easily enough, but the solution
> > depends on your answer.
> >
> > So, do you know the location of the break or must
> > it be estimated?
> >
> > John
>
> Let me be more explicit, since I know you will ask
> for more information.
>
> x = rand(100,1);
> y = (1 + x).*(x<0.4) + (-2*x + 2.2).*(x>=0.4) + randn(size(x))/20;
> plot(x,y,'o')
>
> Here, you and I know the break occurs at 0.4.
> Can we estimate the coefficients of the lines?
>
> IFF we knew the break location, then we would do this:
>
> k = (x <= 0.4); % A logical varable
> coef = [k,x.*k,~k,x.*~k]\y
>
> coef = [k,x.*k,~k,x.*~k]\y
> coef =
> 0.981257860141531
> 1.07669216114181
> 2.20227048054644
> -2.01520597681586
>
> With no error, the coefficients should have been
> [1; 1; 2.2; -2], so we did rather well here.
>
> Can I do as well if I do not assume the location of
> the break is known? In fact, I cannot do quite as
> well in general, since we must estimate an extra
> parameter. We can do well enough though.
>
> coefest = @(B) [(x<B),x.*(x<B),(x>=B),x.*(x>=B)]\y;
> preds = @(B) [(x<B),x.*(x<B),(x>=B),x.*(x>=B)]*coefest(B);
>
> BreakPoint = fminbnd(@(B) sum((y - preds(B)).^2),0,1)
> BreakPoint =
> 0.397098484667164
>
> coefest(BreakPoint)
> ans =
> 0.98340320488404
> 1.06085977689174
> 2.20816686087013
> -2.0226000312523
>
> See that I've used the limits of the data itself to
> define the range over which fminbnd will be allowed
> to search for the break point.
>
> HTH,
> John

Subject: How to plot 2 fitting lines in 1 dataset?

From: Kuo-Hsien

Date: 8 Jun, 2009 01:46:02

Message: 7 of 8

To John,
Yes, I think I probably can plot an exponential line to separate this dataset into two groups (an exponential between these two groups). What exactly should I do?

Thx,
Michael

> Do you know where the break occurs?
 
> If you do, then split this into two problems. In
> fact they can be solved in one estimation step.
>
> If not then you need to use a tool that can find the
> location of the break. This is sometimes known as a
> free knot spline, here a piecewise linear spline. Either
> problem can be solved easily enough, but the solution
> depends on your answer.
>
> So, do you know the location of the break or must
> it be estimated?
>
> John

Subject: How to plot 2 fitting lines in 1 dataset?

From: Kuo-Hsien

Date: 8 Jun, 2009 02:31:01

Message: 8 of 8

"Kuo-Hsien" <mchangks@hotmail.com> wrote in message <h0hqgq$7s9$1@fred.mathworks.com>...
> To John,
> Yes, I think I probably can plot an exponential line to separate this dataset into two groups (an exponential between these two groups). What exactly should I do?
>
> Thx,
> Michael
>
I should look down for more information before I reply this post. Nice and clear explanation. Michael

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us