[R] Polynomial fitting

Matti Jokipii matti.jokipii at gmail.com
Fri Jul 8 20:12:46 CEST 2011

```Thank you Gerrit for the quick reply! And yes, i'm Matti.

I can get the coeffs now, though i'm not sure whether i'm doing
something wrong or whether poly is just not the right method for what
i'm trying to find. I will look into this more closely and give it
another try.

Is poly best for fitting on noisy data that's been generated by a
polynomial and not that good for approximating an arbitrary function? I
tried a least squares fitting with a web applet and got all exited
because the approximation looked quite promising. I understand that R is
designed mainly for statistical computing and may not be the best tool
for my purposes. Before i look elsewhere i would like to ask if there is
some other R method i should try, perhaps a least squares approximation?

Thank you for your help!

Matti Jokipii

08.07.2011 08:25, Gerrit Eichner kirjoitti:
> Hello, mfa (Matti?),
>
> if x and y contain the coordinates of your data points and k is the
> wanted polynomial degree, then
>
> fit <- lm( y ~ poly( x, k))
>
> fits orthonormal polynomials up to degree k to your data. Using
>
> dummy.coef( fit)
>
> should give the coefficients you are interested in.
>
> Hth -- Gerrit
>
> On Thu, 7 Jul 2011, mfa wrote:
>
>> Hello,
>>
>> i'm fairly familiar with R and use it every now and then for math related
>>
>> I have a simple non polynomial function that i would like to approximate
>> with a polynomial. I already looked into poly, but was unable to
>> understand
>> what to do with it. So my problem is this. I can generate virtually any
>> number of datapoints and would like to find the coeffs a1, a2, ... up
>> to a
>> given degree for a polynomial a1x^1 + a2x^2 + ... that approximates my
>> simple function. How can i do this with R?
>>
>> Your help will be highly appreciated!
>>
>> --
>> View this message in context:
>> http://r.789695.n4.nabble.com/Polynomial-fitting-tp3652816p3652816.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> R-help at 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.
>>
> ---------------------------------------------------------------------
> Dr. Gerrit Eichner Mathematical Institute, Room 212
> gerrit.eichner at math.uni-giessen.de Justus-Liebig-University Giessen
> Tel: +49-(0)641-99-32104 Arndtstr. 2, 35392 Giessen, Germany
> Fax: +49-(0)641-99-32109 http://www.uni-giessen.de/cms/eichner
> ---------------------------------------------------------------------
>

```