[R] Polynomial fitting

2008-01-07 Thread Jonas Malmros
I wonder how one in R can fit a 3rd degree polynomial to some data?

Say the data is:

y <- c(15.51, 12.44, 31.5, 21.5, 17.89, 27.09, 15.02, 13.43, 18.18, 11.32)
x <- seq(3.75, 6, 0.25)

And resulting degrees of polynomial are:

5.8007  -91.6339  472.1726 -774.2584

THanks in advance!



-- 
Jonas Malmros
Stockholm University
Stockholm, Sweden

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Polynomial fitting

2008-01-07 Thread Dimitris Rizopoulos
try this:

y <- c(15.51, 12.44, 31.5, 21.5, 17.89, 27.09, 15.02, 13.43, 18.18, 
11.32)
x <- seq(3.75, 6, 0.25)
coef(lm(y ~ x + I(x^2) + I(x^3)))


I hope it helps.

Best,
Dimitris


Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
 http://www.student.kuleuven.be/~m0390867/dimitris.htm


- Original Message - 
From: "Jonas Malmros" <[EMAIL PROTECTED]>
To: 
Sent: Monday, January 07, 2008 4:15 PM
Subject: [R] Polynomial fitting


>I wonder how one in R can fit a 3rd degree polynomial to some data?
>
> Say the data is:
>
> y <- c(15.51, 12.44, 31.5, 21.5, 17.89, 27.09, 15.02, 13.43, 18.18, 
> 11.32)
> x <- seq(3.75, 6, 0.25)
>
> And resulting degrees of polynomial are:
>
> 5.8007  -91.6339  472.1726 -774.2584
>
> THanks in advance!
>
>
>
> -- 
> Jonas Malmros
> Stockholm University
> Stockholm, Sweden
>
> __
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 


Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Polynomial fitting

2008-01-07 Thread Barry Rowlingson
Dimitris Rizopoulos wrote:
> try this:
> 
> y <- c(15.51, 12.44, 31.5, 21.5, 17.89, 27.09, 15.02, 13.43, 18.18, 
> 11.32)
> x <- seq(3.75, 6, 0.25)
> coef(lm(y ~ x + I(x^2) + I(x^3)))

Or use the 'poly' function:

  > coef(lm(x~poly(y,3)))
  (Intercept) poly(y, 3)1 poly(y, 3)2 poly(y, 3)3
   4.875  -0.6233293   0.0312415  -0.6464533

  But hmmm those aren't the same coefficients?

  Because you need to use 'raw=TRUE' if you really want to fit the raw 
powers rather than orthogonal polynomials (which are better behaved than 
fitting the raw powers):

  > coef(lm(y~poly(x,3,raw=TRUE)))
 (Intercept) poly(x, 3, raw = TRUE)1 poly(x, 3, raw = TRUE)2
 -774.258364  472.172611  -91.633939
poly(x, 3, raw = TRUE)3
5.800653

And there's your coefficients. See help(poly) for more.

Barry

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Polynomial fitting

2008-01-07 Thread apjaworski
Jonas,

In statistical sense polynomial is a linear regression fit.  The function
that handles linear fitting is called lm.  Here is how you can reproduce
your results:

lm(y ~ x + I(x^2) + I(x^3))

Unless you are really after the polynomial coefficients it is probably
better to use orthogonal polynomials.  You can get this fit by doing

lm(y ~ poly(x, 3))

Check out help pages for lm and poly.  Hope this helps,

Andy

__
Andy Jaworski
518-1-01
Process Laboratory
3M Corporate Research Laboratory
-
E-mail: [EMAIL PROTECTED]
Tel:  (651) 733-6092
Fax:  (651) 736-3122


   
 "Jonas Malmros"   
 <[EMAIL PROTECTED] 
 ail.com>   To 
 Sent by:  r-help@r-project.org
 [EMAIL PROTECTED]  cc 
 project.org   
   Subject 
       [R] Polynomial fitting  
 01/07/2008 09:16  
 AM
   
   
   
   




I wonder how one in R can fit a 3rd degree polynomial to some data?

Say the data is:

y <- c(15.51, 12.44, 31.5, 21.5, 17.89, 27.09, 15.02, 13.43, 18.18, 11.32)
x <- seq(3.75, 6, 0.25)

And resulting degrees of polynomial are:

5.8007  -91.6339  472.1726 -774.2584

THanks in advance!



--
Jonas Malmros
Stockholm University
Stockholm, Sweden

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Polynomial fitting

2008-01-07 Thread Prof Brian Ripley
On Mon, 7 Jan 2008, [EMAIL PROTECTED] wrote:

> Jonas,
>
> In statistical sense polynomial is a linear regression fit.  The function
> that handles linear fitting is called lm.  Here is how you can reproduce
> your results:
>
> lm(y ~ x + I(x^2) + I(x^3))
>
> Unless you are really after the polynomial coefficients it is probably
> better to use orthogonal polynomials.  You can get this fit by doing
>
> lm(y ~ poly(x, 3))

And if you are, y ~ poly(x, 3, raw=TRUE) is simpler to type and 
comprehend.

>
> Check out help pages for lm and poly.  Hope this helps,
>
> Andy
>
> __
> Andy Jaworski
> 518-1-01
> Process Laboratory
> 3M Corporate Research Laboratory
> -
> E-mail: [EMAIL PROTECTED]
> Tel:  (651) 733-6092
> Fax:  (651) 736-3122
>
>
>
> "Jonas Malmros"
> <[EMAIL PROTECTED]
> ail.com>   To
> Sent by:  r-help@r-project.org
> [EMAIL PROTECTED]  cc
> project.org
>       Subject
>   [R] Polynomial fitting
> 01/07/2008 09:16
> AM
>
>
>
>
>
>
>
>
> I wonder how one in R can fit a 3rd degree polynomial to some data?
>
> Say the data is:
>
> y <- c(15.51, 12.44, 31.5, 21.5, 17.89, 27.09, 15.02, 13.43, 18.18, 11.32)
> x <- seq(3.75, 6, 0.25)
>
> And resulting degrees of polynomial are:
>
> 5.8007  -91.6339  472.1726 -774.2584
>
> THanks in advance!
>
>
>
> --
> Jonas Malmros
> Stockholm University
> Stockholm, Sweden
>
> __
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
> __
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

-- 
Brian D. Ripley,  [EMAIL PROTECTED]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel:  +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UKFax:  +44 1865 272595

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Polynomial fitting

2008-01-09 Thread Jonas Malmros
Dear Mr. Rowlingson, Rizopoulos, Jaworski, and Ripley

Thank you for your help with the polynomial.

Regards,
Jonas

On Jan 7, 2008 5:18 PM, Barry Rowlingson <[EMAIL PROTECTED]> wrote:
> Dimitris Rizopoulos wrote:
> > try this:
> >
> > y <- c(15.51, 12.44, 31.5, 21.5, 17.89, 27.09, 15.02, 13.43, 18.18,
> > 11.32)
> > x <- seq(3.75, 6, 0.25)
> > coef(lm(y ~ x + I(x^2) + I(x^3)))
>
> Or use the 'poly' function:
>
>  > coef(lm(x~poly(y,3)))
>  (Intercept) poly(y, 3)1 poly(y, 3)2 poly(y, 3)3
>   4.875  -0.6233293   0.0312415  -0.6464533
>
>  But hmmm those aren't the same coefficients?
>
>  Because you need to use 'raw=TRUE' if you really want to fit the raw
> powers rather than orthogonal polynomials (which are better behaved than
> fitting the raw powers):
>
>  > coef(lm(y~poly(x,3,raw=TRUE)))
> (Intercept) poly(x, 3, raw = TRUE)1 poly(x, 3, raw = TRUE)2
> -774.258364  472.172611  -91.633939
> poly(x, 3, raw = TRUE)3
>5.800653
>
> And there's your coefficients. See help(poly) for more.
>
> Barry
>



-- 
Jonas Malmros
Stockholm University
Stockholm, Sweden

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.