Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: linear regression

Subject: linear regression

From: fas

Date: 16 May, 2008 13:19:53

Message: 1 of 11

Hi
I want to do linear regression
X_vec=a*Y_vec + b
.
.
.

where X_vec(1....100) and Y_vec (1..100) are vectors (say of size
100). So I have an overdetermined system where each vector value gives
me one equation and I want to determine 'a' and 'b' after regression.
I am bit novice any help?

Subject: Re: linear regression

From: Steven Lord

Date: 16 May, 2008 13:28:29

Message: 2 of 11


"fas" <faisalmufti@gmail.com> wrote in message
news:13a0ad36-75d8-4ca0-b585-727d3a8ce1a9@w8g2000prd.googlegroups.com...
> Hi
> I want to do linear regression
> X_vec=a*Y_vec + b
> .
> .
> .
>
> where X_vec(1....100) and Y_vec (1..100) are vectors (say of size
> 100). So I have an overdetermined system where each vector value gives
> me one equation and I want to determine 'a' and 'b' after regression.
> I am bit novice any help?

Look at the Interactive Fitting and Programmatic Fitting sections of the
Data Analysis documentation:

http://www.mathworks.com/access/helpdesk/help/techdoc/data_analysis/f1-6010.html

The first example on the Programmatic Fitting page does basically what you
want.

--
Steve Lord
slord@mathworks.com


Subject: Re: linear regression

From: John D'Errico

Date: 16 May, 2008 13:31:01

Message: 3 of 11

fas <faisalmufti@gmail.com> wrote in message <13a0ad36-75d8-4ca0-b585-
727d3a8ce1a9@w8g2000prd.googlegroups.com>...
> Hi
> I want to do linear regression
> X_vec=a*Y_vec + b
> .
> .
> .
>
> where X_vec(1....100) and Y_vec (1..100) are vectors (say of size
> 100). So I have an overdetermined system where each vector value gives
> me one equation and I want to determine 'a' and 'b' after regression.
> I am bit novice any help?

polyfit

John

Subject: Re: linear regression

From: fas

Date: 17 May, 2008 11:12:46

Message: 4 of 11

On May 16, 11:31 pm, "John D'Errico" <woodch...@rochester.rr.com>
wrote:
> fas <faisalmu...@gmail.com> wrote in message <13a0ad36-75d8-4ca0-b585-
>
> 727d3a8ce...@w8g2000prd.googlegroups.com>...
>
> > Hi
> > I want to do linear regression
> > X_vec=a*Y_vec + b
> > .
> > .
> > .
>
> > where X_vec(1....100) and Y_vec (1..100) are vectors (say of size
> > 100). So I have an overdetermined system where each vector value gives
> > me one equation and I want to determine 'a' and 'b' after regression.
> > I am bit novice any help?
>
> polyfit
>
> John

My data is 100x100, 2D which I have converted to X(:) as same for Y,
but it seems its not the right approach to run 1D polyfit on it. Any
idea what to do now

Subject: Re: linear regression

From: John D'Errico

Date: 17 May, 2008 11:51:02

Message: 5 of 11

fas <faisalmufti@gmail.com> wrote in message <4c336df8-f903-4db2-
bb4a-7a8cf785ec45@j33g2000pri.googlegroups.com>...
> On May 16, 11:31 pm, "John D'Errico" <woodch...@rochester.rr.com>
> wrote:
> > fas <faisalmu...@gmail.com> wrote in message <13a0ad36-75d8-
4ca0-b585-
> >
> > 727d3a8ce...@w8g2000prd.googlegroups.com>...
> >
> > > Hi
> > > I want to do linear regression
> > > X_vec=a*Y_vec + b
> > > .
> > > .
> > > .
> >
> > > where X_vec(1....100) and Y_vec (1..100) are vectors (say of size
> > > 100). So I have an overdetermined system where each vector value
gives
> > > me one equation and I want to determine 'a' and 'b' after regression.
> > > I am bit novice any help?
> >
> > polyfit
> >
> > John
>
> My data is 100x100, 2D which I have converted to X(:) as same for Y,
> but it seems its not the right approach to run 1D polyfit on it. Any
> idea what to do now

You need to think about what you want to
do, or at least explain yourself more clearly.
You will find that learning to explain yourself
clearly will often bring you directly to the
answer that you need.

You have an array X and an array Y. Originally
you said that X and Y were vectors of length
100. Now you are telling us that they are
arrays of size 100x100. It cannot be both.

Try a little harder. Explain your problem.

John

Subject: Re: linear regression

From: fas

Date: 18 May, 2008 00:35:42

Message: 6 of 11

On May 17, 9:51 pm, "John D'Errico" <woodch...@rochester.rr.com>
wrote:
> fas <faisalmu...@gmail.com> wrote in message <4c336df8-f903-4db2-
>
> bb4a-7a8cf785e...@j33g2000pri.googlegroups.com>...
>
>
>
> > On May 16, 11:31 pm, "John D'Errico" <woodch...@rochester.rr.com>
> > wrote:
> > > fas <faisalmu...@gmail.com> wrote in message <13a0ad36-75d8-
> 4ca0-b585-
>
> > > 727d3a8ce...@w8g2000prd.googlegroups.com>...
>
> > > > Hi
> > > > I want to do linear regression
> > > > X_vec=a*Y_vec + b
> > > > .
> > > > .
> > > > .
>
> > > > where X_vec(1....100) and Y_vec (1..100) are vectors (say of size
> > > > 100). So I have an overdetermined system where each vector value
> gives
> > > > me one equation and I want to determine 'a' and 'b' after regression.
> > > > I am bit novice any help?
>
> > > polyfit
>
> > > John
>
> > My data is 100x100, 2D which I have converted to X(:) as same for Y,
> > but it seems its not the right approach to run 1D polyfit on it. Any
> > idea what to do now
>
> You need to think about what you want to
> do, or at least explain yourself more clearly.
> You will find that learning to explain yourself
> clearly will often bring you directly to the
> answer that you need.
>
> You have an array X and an array Y. Originally
> you said that X and Y were vectors of length
> 100. Now you are telling us that they are
> arrays of size 100x100. It cannot be both.
>
> Try a little harder. Explain your problem.
>
> John

Ok, X and Y were for an example case are of size 100. In actual X and
Y are both of 2D of size (100x100 again example case). I converted
them to vectorized form as 10000x1. So I have 10000 equations each for
one point and now I want to fit polyfit. But the problem is that as
they are converted from 2D matrix. If I plot in 1D they do not appear
right and hence I believe polyfit may not work. The other option I can
think of is to sort it after converting it into vectorized form, so
that then can come in some order (for of some curve). Will it be ok to
apply polyfit on it. Will the coefficient that I find will have same
meaning or if the regression is done on the same 2D function (some
how)
Thanks,

Subject: Re: linear regression

From: Greg Heath

Date: 18 May, 2008 09:39:30

Message: 7 of 11

On May 17, 8:35=A0pm, fas <faisalmu...@gmail.com> wrote:
> On May 17, 9:51 pm, "John D'Errico" <woodch...@rochester.rr.com>
> wrote:
>
>
>
>
>
> > fas <faisalmu...@gmail.com> wrote in message <4c336df8-f903-4db2-
>
> > bb4a-7a8cf785e...@j33g2000pri.googlegroups.com>...
>
> > > On May 16, 11:31 pm, "John D'Errico" <woodch...@rochester.rr.com>
> > > wrote:
> > > > fas <faisalmu...@gmail.com> wrote in message <13a0ad36-75d8-
> > 4ca0-b585-
>
> > > > 727d3a8ce...@w8g2000prd.googlegroups.com>...
>
> > > > > Hi
> > > > > I want to do linear regression
> > > > > X_vec=3Da*Y_vec + b
> > > > > .
> > > > > .
> > > > > .
>
> > > > > where X_vec(1....100) and Y_vec (1..100) are vectors (say of size
> > > > > 100). So I have an overdetermined system where each vector value
> > gives
> > > > > me one equation and I want to determine 'a' and 'b' after regressi=
on.
> > > > > I am bit novice any help?
>
> > > > polyfit
>
> > > > John
>
> > > My data is 100x100, 2D which I have converted to X(:) as same for Y,
> > > but it seems its not the right approach to run 1D polyfit on it. Any
> > > idea what to do now
>
> > You need to think about what you want to
> > do, or at least explain yourself more clearly.
> > You will find that learning to explain yourself
> > clearly will often bring you directly to the
> > answer that you need.
>
> > You have an array X and an array Y. Originally
> > you said that X and Y were vectors of length
> > 100. Now you are telling us that they are
> > arrays of size 100x100. It cannot be both.
>
> > Try a little harder. Explain your problem.
>
> > John
>
> Ok, X and Y were for an example case are of size 100. In actual X and
> Y are both of 2D of size (100x100 =A0again example case). I converted
> them to vectorized form as 10000x1. So I have 10000 equations each for
> one point and now I want to fit polyfit. But the problem is that as
> they are converted from 2D matrix. If I plot in 1D they do not appear
> right and hence I believe polyfit may not work. The other option I can
> think of is to sort it after converting it into vectorized form, so
> that then can come in some order (for of some curve). Will it be ok to
> apply polyfit on it. Will the coefficient that I find will have same
> meaning or if the regression is done on the same 2D function (some
> how)
> Thanks,-

Why is it more difficult than solving

Y =3D W*[ones(100);X]

for W?

Hope this helps.

Greg

Subject: Re: linear regression

From: fas

Date: 18 May, 2008 10:18:46

Message: 8 of 11

On May 18, 7:39 pm, Greg Heath <he...@alumni.brown.edu> wrote:
> On May 17, 8:35 pm, fas <faisalmu...@gmail.com> wrote:
>
>
>
> > On May 17, 9:51 pm, "John D'Errico" <woodch...@rochester.rr.com>
> > wrote:
>
> > > fas <faisalmu...@gmail.com> wrote in message <4c336df8-f903-4db2-
>
> > > bb4a-7a8cf785e...@j33g2000pri.googlegroups.com>...
>
> > > > On May 16, 11:31 pm, "John D'Errico" <woodch...@rochester.rr.com>
> > > > wrote:
> > > > > fas <faisalmu...@gmail.com> wrote in message <13a0ad36-75d8-
> > > 4ca0-b585-
>
> > > > > 727d3a8ce...@w8g2000prd.googlegroups.com>...
>
> > > > > > Hi
> > > > > > I want to do linear regression
> > > > > > X_vec=a*Y_vec + b
> > > > > > .
> > > > > > .
> > > > > > .
>
> > > > > > where X_vec(1....100) and Y_vec (1..100) are vectors (say of size
> > > > > > 100). So I have an overdetermined system where each vector value
> > > gives
> > > > > > me one equation and I want to determine 'a' and 'b' after regression.
> > > > > > I am bit novice any help?
>
> > > > > polyfit
>
> > > > > John
>
> > > > My data is 100x100, 2D which I have converted to X(:) as same for Y,
> > > > but it seems its not the right approach to run 1D polyfit on it. Any
> > > > idea what to do now
>
> > > You need to think about what you want to
> > > do, or at least explain yourself more clearly.
> > > You will find that learning to explain yourself
> > > clearly will often bring you directly to the
> > > answer that you need.
>
> > > You have an array X and an array Y. Originally
> > > you said that X and Y were vectors of length
> > > 100. Now you are telling us that they are
> > > arrays of size 100x100. It cannot be both.
>
> > > Try a little harder. Explain your problem.
>
> > > John
>
> > Ok, X and Y were for an example case are of size 100. In actual X and
> > Y are both of 2D of size (100x100 again example case). I converted
> > them to vectorized form as 10000x1. So I have 10000 equations each for
> > one point and now I want to fit polyfit. But the problem is that as
> > they are converted from 2D matrix. If I plot in 1D they do not appear
> > right and hence I believe polyfit may not work. The other option I can
> > think of is to sort it after converting it into vectorized form, so
> > that then can come in some order (for of some curve). Will it be ok to
> > apply polyfit on it. Will the coefficient that I find will have same
> > meaning or if the regression is done on the same 2D function (some
> > how)
> > Thanks,-
>
> Why is it more difficult than solving
>
> Y = W*[ones(100);X]
>
> for W?
>
> Hope this helps.
>
> Greg

Sorry I could not get you completely. ?

I would like to know that if we convert a nxm (matrix ) function to 1D
and then sort it and then polyfit . Can we use the result. As in a 3D
format( nxm) matrix I cannot use polyfit :(

Subject: Re: linear regression

From: John D'Errico

Date: 18 May, 2008 11:23:01

Message: 9 of 11

fas <faisalmufti@gmail.com> wrote in message <f4a869df-f422-47d9-
bda7-1daafa173bb1@z16g2000prn.googlegroups.com>...

> > Why is it more difficult than solving
> >
> > Y = W*[ones(100);X]
> >
> > for W?
> >
> > Hope this helps.
> >
> > Greg
>
> Sorry I could not get you completely. ?
>
> I would like to know that if we convert a nxm (matrix ) function to 1D
> and then sort it and then polyfit . Can we use the result. As in a 3D
> format( nxm) matrix I cannot use polyfit :(

Why do you state that it does not work?

If your model is

   X =a*Y + b

then polyfit as applied to

  ab = polyfit(Y(:),X(:),1);

will work. Equally as well, if you use backslash,

  ab = [Y(:),ones(numel(Y),1)]\X(:);

this will also work.

John

Subject: Re: linear regression

From: John D'Errico

Date: 18 May, 2008 11:29:02

Message: 10 of 11

fas <faisalmufti@gmail.com> wrote in message <2e7d4e65-1ce4-4bf3-
8ab3-81f0bae4638c@c19g2000prf.googlegroups.com>...

> Ok, X and Y were for an example case are of size 100. In actual X and
> Y are both of 2D of size (100x100 again example case). I converted
> them to vectorized form as 10000x1. So I have 10000 equations each for
> one point and now I want to fit polyfit. But the problem is that as
> they are converted from 2D matrix. If I plot in 1D they do not appear
> right and hence I believe polyfit may not work. The other option I can
> think of is to sort it after converting it into vectorized form, so
> that then can come in some order (for of some curve). Will it be ok to
> apply polyfit on it. Will the coefficient that I find will have same
> meaning or if the regression is done on the same 2D function (some
> how)
> Thanks,

What does not "appear" right?

If your model is as stated, then this is correct.

I think that you are asking to do a 2-d regression,
in both the x and y directinos, but for some reason
do not realize that is what you want to do. Or, you
just don't want to admit it.

John

Subject: Re: linear regression

From: fas

Date: 19 May, 2008 04:51:40

Message: 11 of 11

On May 18, 9:29 pm, "John D'Errico" <woodch...@rochester.rr.com>
wrote:
> fas <faisalmu...@gmail.com> wrote in message <2e7d4e65-1ce4-4bf3-
>
> 8ab3-81f0bae46...@c19g2000prf.googlegroups.com>...
>
> > Ok, X and Y were for an example case are of size 100. In actual X and
> > Y are both of 2D of size (100x100 again example case). I converted
> > them to vectorized form as 10000x1. So I have 10000 equations each for
> > one point and now I want to fit polyfit. But the problem is that as
> > they are converted from 2D matrix. If I plot in 1D they do not appear
> > right and hence I believe polyfit may not work. The other option I can
> > think of is to sort it after converting it into vectorized form, so
> > that then can come in some order (for of some curve). Will it be ok to
> > apply polyfit on it. Will the coefficient that I find will have same
> > meaning or if the regression is done on the same 2D function (some
> > how)
> > Thanks,
>
> What does not "appear" right?
>
> If your model is as stated, then this is correct.
>
> I think that you are asking to do a 2-d regression,
> in both the x and y directinos, but for some reason
> do not realize that is what you want to do. Or, you
> just don't want to admit it.
>
> John

I admit that I wan to to do 2D regression. So if I want to to do that
then doing a vectorization and sorting and then regression may not be
right (Is it?). So in that case how to to 2D regression ?

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics