# [R] Polynomial fitting

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Jan 7 19:38:29 CET 2008

```On Mon, 7 Jan 2008, apjaworski at mmm.com 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
>
> 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: apjaworski at mmm.com
> Tel:  (651) 733-6092
> Fax:  (651) 736-3122
>
>
>
>             "Jonas Malmros"
>             <jonas.malmros at gm
>             ail.com>                                                   To
>             Sent by:                  r-help at r-project.org
>             r-help-bounces at r-                                          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
>
>
>
>
> --
> Jonas Malmros
> Stockholm University
> Stockholm, Sweden
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> and provide commented, minimal, self-contained, reproducible code.
>

--
Brian D. Ripley,                  ripley at stats.ox.ac.uk
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, UK                Fax:  +44 1865 272595

```