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: Zebbik

Date: 12 Mar, 2008 19:52:24

Message: 1 of 12

Hello.

My problem is that I am supposed to find trend line /regression of from
the results made in experiment. I know

p = polyfit(x,y,1)

gives me coefficients of line (the correlation is strong almost 1 so
it's linear tendency), where in terms of y = mx + c

p(1) - slope (m)
p(2) - 'c' coefficient

I know also that

yfit = polyval(p, y)

gives me the fit of line such that it's going to be linear reggresion.

Now. The value im looking for is the slope. I would like to find the
slope itself with the error or let's say Delta that the slope could
differ arround like
(Slope - Delta_Slope, Slope + Delta_Slope)

Does any1 know how to do that?

Thanks.

Subject: Re: Linear regression.

From: Paul

Date: 13 Mar, 2008 00:21:02

Message: 2 of 12

Zebbik <zebik@op.pl> wrote in message
<fr9c9t$o1p$1@news.onet.pl>...
> Hello.
>
> My problem is that I am supposed to find trend line
/regression of from
> the results made in experiment. I know
>
> p = polyfit(x,y,1)
>
> gives me coefficients of line (the correlation is strong
almost 1 so
> it's linear tendency), where in terms of y = mx + c
>
> p(1) - slope (m)
> p(2) - 'c' coefficient
>
> I know also that
>
> yfit = polyval(p, y)
>
> gives me the fit of line such that it's going to be linear
reggresion.
>
> Now. The value im looking for is the slope. I would like
to find the
> slope itself with the error or let's say Delta that the
slope could
> differ arround like
> (Slope - Delta_Slope, Slope + Delta_Slope)
>
> Does any1 know how to do that?
>
> Thanks.

I suggest you search the file exchange.

Subject: Re: Linear regression.

From: Arthur G

Date: 13 Mar, 2008 02:38:50

Message: 3 of 12

On 2008-03-12 20:21:02 -0400, "Paul " <par@ceri.memphis.edu> said:

> Zebbik <zebik@op.pl> wrote in message
> <fr9c9t$o1p$1@news.onet.pl>...
>> Hello.
>>
>> My problem is that I am supposed to find trend line
> /regression of from
>> the results made in experiment. I know
>>
>> p = polyfit(x,y,1)
>>
>> gives me coefficients of line (the correlation is strong
> almost 1 so
>> it's linear tendency), where in terms of y = mx + c
>>
>> p(1) - slope (m)
>> p(2) - 'c' coefficient
>>
>> I know also that
>>
>> yfit = polyval(p, y)
>>
>> gives me the fit of line such that it's going to be linear
> reggresion.
>>
>> Now. The value im looking for is the slope. I would like
> to find the
>> slope itself with the error or let's say Delta that the
> slope could
>> differ arround like
>> (Slope - Delta_Slope, Slope + Delta_Slope)
>>
>> Does any1 know how to do that?
>>
>> Thanks.
>
> I suggest you search the file exchange.

If you have the statistics toolbox, the regress function will give 95%
confidence intervals.

Subject: Re: Linear regression.

From: Peter Perkins

Date: 13 Mar, 2008 12:56:07

Message: 4 of 12

Zebbik wrote:

> Now. The value im looking for is the slope. I would like to find the
> slope itself with the error or let's say Delta that the slope could
> differ arround like
> (Slope - Delta_Slope, Slope + Delta_Slope)

POLYCONF in the Statistics Toolbox, or LSCOV in core MATLAB.

Hope this helps.

- Peter Perkins
   The MathWorks, Inc.

Subject: Re: Linear regression.

From: Zebbik

Date: 13 Mar, 2008 16:38:20

Message: 5 of 12

Paul wrote:
> Zebbik <zebik@op.pl> wrote in message
> <fr9c9t$o1p$1@news.onet.pl>...
>> Hello.
>>
>> My problem is that I am supposed to find trend line
> /regression of from
>> the results made in experiment. I know
>>
>> p = polyfit(x,y,1)
>>
>> gives me coefficients of line (the correlation is strong
> almost 1 so
>> it's linear tendency), where in terms of y = mx + c
>>
>> p(1) - slope (m)
>> p(2) - 'c' coefficient
>>
>> I know also that
>>
>> yfit = polyval(p, y)
>>
>> gives me the fit of line such that it's going to be linear
> reggresion.
>> Now. The value im looking for is the slope. I would like
> to find the
>> slope itself with the error or let's say Delta that the
> slope could
>> differ arround like
>> (Slope - Delta_Slope, Slope + Delta_Slope)
>>
>> Does any1 know how to do that?
>>
>> Thanks.
>
> I suggest you search the file exchange.
I am not quite sure what you mean.

Subject: Re: Linear regression.

From: Zebbik

Date: 13 Mar, 2008 18:20:58

Message: 6 of 12

Peter Perkins wrote:
> Zebbik wrote:
>
>> Now. The value im looking for is the slope. I would like to find the
>> slope itself with the error or let's say Delta that the slope could
>> differ arround like
>> (Slope - Delta_Slope, Slope + Delta_Slope)
>
> POLYCONF in the Statistics Toolbox, or LSCOV in core MATLAB.
>
> Hope this helps.
>
> - Peter Perkins
> The MathWorks, Inc.
The thing i am looking for is in Excel, however, i would like to find it
in MATLAB to be honest. I will try to explain that on Excel example

In EXCEL there is a formula called LINEST and has the form below

LINEST(knowm_y value cells, known_x values cells, CONST, STATS)

This formula produce more or less such results as:
SLope Coeff Point Of intercept
Slope Coeff Error Point Of Intercep Error
...
...
...

I dont know that yet but i believe in produces results with 95%
confidence intervals. The other values are not important here at the
moment. The Slope Error is the thing i am looking for in MATLAB.
Can someone please be so kind and help me to find/understand that.

Regards

Subject: Re: Linear regression.

From: Yuri Geshelin

Date: 13 Mar, 2008 18:50:19

Message: 7 of 12

Hello,

I had the same problem and did not find anything in matlab.
Below is the code, which I wrote up for this purpose. It
contains the reference to the source, which I used. Perhaps
I will consider uploading it to file exchange, but for now
I will just copy-paste it. Hope you will cope with text
wrapping and figure it out.

Yuri
------------------------
%*******************************
function [sesl, cisl, sein, ciin] = secisi(X,Y,pr);

% Returns Standard Errors and Confidence Intervals
% for the Slope and Intercept of a regression.

% Author: Yuri Geshelin, 2002.

% Source: N. R. Draper and H. Smith, 1966:
% Applied Regression Analysis, 407 p. Publ. by
% John Wiley and Sons, Inc. New York - London - Sydney.

% Input:
% 1-D arrays X and Y, for which the regression
% Y = B0 + B1 * X is computed
% pr - probability (e.g. if we want 95%
% confidence level, then pr = 95.

% Output:
% sesl - standard error for slope B1
% clsl - pr%-confindence interval for slope B1
% sein - standard error for intercept B0
% clin - pr%-confindence interval for intercept B0

if pr < 10 | pr > 99.9
   error('pr should be between 10 and 99.9!')
end

n = length(X);
if length(Y) ~= n
   error('X, Y should be the same lengths!')
end

n2 = n - 2; % n - 2 degrees of freedom

if n2 < 121 % if n2 = 120, then we already would
            % have to use the next-to-last line
            % of the table on p. 305: this code is only
            % for the last line.
   error('This code only works with n > 120!')
end

% The following line is the result of
% prob = 100 * ...
% (1 - [0.9:-0.2:0.3 0.2 0.1 0.05 0.02 0.01 0.001]) ...
% (see p. 305):

prob = [10 30 50 70 80 90 95 98 99 99.9]; % percentage

pp = [0.126 0.385 0.674 1.036 1.282 ...
      1.645 1.960 2.326 2.576 3.291]; % last line of the
table,
                                      % p. 305.

% Compute total corrected sum of squares (SS) of the Y's
% (see p.14 and table on p. 16):

SStot = sum((Y - mean(Y)).^2);

% Compute SS due to regression (see p. 16):

SX2 = sum(X .^ 2);
SSrgr = ...
(sum(X .* Y) - sum(X) * sum(Y) / n) ^2 / ...
(SX2 - sum(X) ^2 / n);

% Compute SS due to residual (see p. 16):

SSres = SStot - SSrgr;

% Compute mean square about regression
% (see tables on pp. 15, 16):

s2 = SSres / n2;

% Compute estimate of variance of slope B1 (p. 19)

sesl = sqrt(s2 / sum((X - mean(X)) .^2 ));

% Found the interpolated value from the table
% (page 305):

val_int = interp1(prob,pp,pr);

% Compute confidence limit for slope:

cisl = val_int * sesl;

% Compute estimate of variance of intercept B0 (p. 21)

sein = sesl * sqrt(SX2 / n);

% Compute confidence limit for intercept:

ciin = val_int * sein;
%***********************************

Subject: Re: Linear regression.

From: Peter Perkins

Date: 13 Mar, 2008 19:30:56

Message: 8 of 12

Zebbik wrote:

> The thing i am looking for is in Excel, however, i would like to find it
> in MATLAB to be honest. I will try to explain that on Excel example

Again, POLYCONF in the Statistics Toolbox, or LSCOV in core MATLAB.

Subject: Re: Linear regression.

From: Yuri Geshelin

Date: 13 Mar, 2008 19:43:02

Message: 9 of 12

Peter Perkins <Peter.PerkinsRemoveThis@mathworks.com> wrote
in message <frbvdg$7t7$1@fred.mathworks.com>...
> Zebbik wrote:
>
> > The thing i am looking for is in Excel, however, i
would like to find it
> > in MATLAB to be honest. I will try to explain that on
Excel example
>
> Again, POLYCONF in the Statistics Toolbox, or LSCOV in
core MATLAB.

Hi,

Here is what Zebbik wrote:

"The Slope Error is the thing i am looking for in MATLAB."

Does lscov or polyconf accomplish this task?

Subject: Re: Linear regression.

From: Peter Perkins

Date: 14 Mar, 2008 13:13:13

Message: 10 of 12

Yuri Geshelin wrote:

> Here is what Zebbik wrote:
>
> "The Slope Error is the thing i am looking for in MATLAB."
>
> Does lscov or polyconf accomplish this task?

Yuri, "yes" to LSCOV (depending on what is meant by "error"), but you're
right, my suggestion of polyconf was off track; I I wasn't paying enough
attention.

Zebbik, if by "error", you mean a standard error, then LSCOV will give
you that as its second output.

If you want a confidence interval for the coefficient, then REGRESS in
the Statistics Toolbox (as pointed out by Arthur G) returns that as its
second output.

In both cases, all you have to do is to create the design matrix X as

    X = [ones(length(x),1) x(:)]

where x and y are vectors containing your data.

Subject: Re: Linear regression.

From: Zebbik

Date: 18 Mar, 2008 14:01:56

Message: 11 of 12

Yuri Geshelin wrote:
> Hello,
>
> I had the same problem and did not find anything in matlab.
> Below is the code, which I wrote up for this purpose. It
> contains the reference to the source, which I used. Perhaps
> I will consider uploading it to file exchange, but for now
> I will just copy-paste it. Hope you will cope with text
> wrapping and figure it out.
>
> Yuri
> ------------------------
> %*******************************
> function [sesl, cisl, sein, ciin] = secisi(X,Y,pr);
>
> % Returns Standard Errors and Confidence Intervals
> % for the Slope and Intercept of a regression.
>
> % Author: Yuri Geshelin, 2002.
>
> % Source: N. R. Draper and H. Smith, 1966:
> % Applied Regression Analysis, 407 p. Publ. by
> % John Wiley and Sons, Inc. New York - London - Sydney.
>
> % Input:
> % 1-D arrays X and Y, for which the regression
> % Y = B0 + B1 * X is computed
> % pr - probability (e.g. if we want 95%
> % confidence level, then pr = 95.
>
> % Output:
> % sesl - standard error for slope B1
> % clsl - pr%-confindence interval for slope B1
> % sein - standard error for intercept B0
> % clin - pr%-confindence interval for intercept B0
>
> if pr < 10 | pr > 99.9
> error('pr should be between 10 and 99.9!')
> end
>
> n = length(X);
> if length(Y) ~= n
> error('X, Y should be the same lengths!')
> end
>
> n2 = n - 2; % n - 2 degrees of freedom
>
> if n2 < 121 % if n2 = 120, then we already would
> % have to use the next-to-last line
> % of the table on p. 305: this code is only
> % for the last line.
> error('This code only works with n > 120!')
> end
>
> % The following line is the result of
> % prob = 100 * ...
> % (1 - [0.9:-0.2:0.3 0.2 0.1 0.05 0.02 0.01 0.001]) ...
> % (see p. 305):
>
> prob = [10 30 50 70 80 90 95 98 99 99.9]; % percentage
>
> pp = [0.126 0.385 0.674 1.036 1.282 ...
> 1.645 1.960 2.326 2.576 3.291]; % last line of the
> table,
> % p. 305.
>
> % Compute total corrected sum of squares (SS) of the Y's
> % (see p.14 and table on p. 16):
>
> SStot = sum((Y - mean(Y)).^2);
>
> % Compute SS due to regression (see p. 16):
>
> SX2 = sum(X .^ 2);
> SSrgr = ...
> (sum(X .* Y) - sum(X) * sum(Y) / n) ^2 / ...
> (SX2 - sum(X) ^2 / n);
>
> % Compute SS due to residual (see p. 16):
>
> SSres = SStot - SSrgr;
>
> % Compute mean square about regression
> % (see tables on pp. 15, 16):
>
> s2 = SSres / n2;
>
> % Compute estimate of variance of slope B1 (p. 19)
>
> sesl = sqrt(s2 / sum((X - mean(X)) .^2 ));
>
> % Found the interpolated value from the table
> % (page 305):
>
> val_int = interp1(prob,pp,pr);
>
> % Compute confidence limit for slope:
>
> cisl = val_int * sesl;
>
> % Compute estimate of variance of intercept B0 (p. 21)
>
> sein = sesl * sqrt(SX2 / n);
>
> % Compute confidence limit for intercept:
>
> ciin = val_int * sein;
> %***********************************
>
thanks for that =))

Subject: Re: Linear regression.

From: Zebbik

Date: 18 Mar, 2008 14:04:06

Message: 12 of 12

Peter Perkins wrote:
> Yuri Geshelin wrote:
>
>> Here is what Zebbik wrote:
>>
>> "The Slope Error is the thing i am looking for in MATLAB."
>>
>> Does lscov or polyconf accomplish this task?
>
> Yuri, "yes" to LSCOV (depending on what is meant by "error"), but you're
> right, my suggestion of polyconf was off track; I I wasn't paying enough
> attention.
>
> Zebbik, if by "error", you mean a standard error, then LSCOV will give
> you that as its second output.
>
> If you want a confidence interval for the coefficient, then REGRESS in
> the Statistics Toolbox (as pointed out by Arthur G) returns that as its
> second output.
>
> In both cases, all you have to do is to create the design matrix X as
>
> X = [ones(length(x),1) x(:)]
>
> where x and y are vectors containing your data.
It's np. thanks for ur answer it is always helpful to know something new.

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