Re: [R] Polynomial fitting

2011-07-09 Thread Berry Boessenkool


Hey Matti,

lm() _is_ a least square approximation.
Did you notivce in the poly-help you'll probably need to set raw=TRUE ?
Helped me a lot once I figured that one out...
check this:

a - c(-5:10)  ;  b - 5*a^3 + 2*a^2 - 7*a + 3  + rnorm(15, 0, 100) 
data.frame(a,b) 
modell - lm(b ~  poly(a,3, raw=T)) ;    modell
plot(a,b)  ; lines(a, predict(modell))

now try this with raw=FALSE. The graph will look the same, but the coefficients 
are normalized and centered.
Notice that the value estimated for the intercept is quite different form the 
real intercept (3), as the random numbers added have great variation (sd=100).
The other coefficents are closer to the real ones. Note also, that they are 
printed in reverse order than specified in b - ...

A way to avoid the whole polyfunction altogether is:

lm(b ~ I(a^3) + I(a^2) + a)

You decide what's more elegant...

Hope this helps,
Berry

-
Berry Boessenkool
Potsdam
-


 Date: Fri, 8 Jul 2011 21:12:46 +0300
 From: matti.joki...@gmail.com
 To: gerrit.eich...@math.uni-giessen.de
 CC: r-help@r-project.org
 Subject: Re: [R] Polynomial fitting
 
 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?
 
[[elided Hotmail spam]]
 
 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
  tasks.
 
  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?
 
[[elided Hotmail spam]]
 
  --
  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@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.eich...@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
  -
 
 
 __
 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

2011-07-09 Thread Matti Jokipii
Oh yes! raw=T did the trick. I even tried raw=T while i was still trying 
to dig up the coeffs directly from poly. It didn't cross my mind after i 
learned about fit- lm( y ~ poly( x, k)) from Gerrit, and i jumped 
into my own false conclusions. Good of you to guess that was my 
problem. Now the approximation looks absolutely beautiful.


Thank you very much Berry and Gerrit for helping me out on this! I 
really really like using R, and i'm so happy for not needing any other 
tool for this.


Matti

09.07.2011 16:25, Berry Boessenkool kirjoitti:



Hey Matti,

lm() _is_ a least square approximation.
Did you notivce in the poly-help you'll probably need to set raw=TRUE ?
Helped me a lot once I figured that one out...
check this:

a- c(-5:10)  ;  b- 5*a^3 + 2*a^2 - 7*a + 3  + rnorm(15, 0, 100)
data.frame(a,b)
modell- lm(b ~  poly(a,3, raw=T)) ;modell
plot(a,b)  ; lines(a, predict(modell))

now try this with raw=FALSE. The graph will look the same, but the coefficients 
are normalized and centered.
Notice that the value estimated for the intercept is quite different form the 
real intercept (3), as the random numbers added have great variation (sd=100).
The other coefficents are closer to the real ones. Note also, that they are 
printed in reverse order than specified in b- ...

A way to avoid the whole polyfunction altogether is:

lm(b ~ I(a^3) + I(a^2) + a)

You decide what's more elegant...

Hope this helps,
Berry

-
Berry Boessenkool
Potsdam
-



Date: Fri, 8 Jul 2011 21:12:46 +0300
From: matti.joki...@gmail.com
To: gerrit.eich...@math.uni-giessen.de
CC: r-help@r-project.org
Subject: Re: [R] Polynomial fitting

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
tasks.

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@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.eich...@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
-



__
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

2011-07-08 Thread Matti Jokipii

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
tasks.

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@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.eich...@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
-



__
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] Polynomial fitting

2011-07-07 Thread mfa
Hello,

i'm fairly familiar with R and use it every now and then for math related
tasks.

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@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

2011-07-07 Thread Gerrit Eichner

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
tasks.

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@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.eich...@math.uni-giessen.de   Justus-Liebig-University Giessen
Tel: +49-(0)641-99-32104  Arndtstr. 2, 35392 Giessen, Germany
Fax: +49-(0)641-99-32109http://www.uni-giessen.de/cms/eichner

__
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] Polynomial fitting

2009-11-11 Thread Julia Cains
Dear R helpers
 
 
Suppose I have a following data
 
y  - c(9.21, 9.51, 9.73, 9.88, 10.12. 10.21)
 
t  - c(0, 0.25, 1, 3, 6, 12)
 
I want to find out the polynomial which fits y in terms of t i.e. y = f(t) some 
function of t.
 
e.g.   y = bo + b1*t + (b2 * t^2) + (b3 * t^3) + .. and so on.
 
In Excel I have defined y as independent variable, then defined t, t^2 and t^3 
and using regression I could arrive at the equation 
 
y = 9.505799 + (0.191092 * t) - (0.0225 * t^2) + (0.001245 * t^3)
 
However I feel this is wrong as I am trying to use linear regression but here I 
am having polynomial in t.
 
I am not that good in stats as well as in mathmatics.
 
I request you to kindly help me as to how to express the 'y' in polynomial in 
terms of t.
 
Thanking you in advance
 
Julia
 
 
 
 




Only a man of Worth sees Worth in other men




  
[[alternative HTML version deleted]]

__
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

2009-09-29 Thread chris carleton

Thanks for the response. I'm sorry I didn't provide the code or data example 
earlier. I was using the polynomial fitting technique of this form;

test - lm(x[,34] ~ I(x[,1]) + I(x[,1]^2) + I(x[,1]^3))

for the original fitting operation. I also tried to use;

lm(y ~ poly(x,3,raw=TRUE))

with the same results for the polynomial coefficients in both cases. If my 
understanding is correct, both of the methods above produce the coefficients of 
a polynomial based on the data in 'y' as that data varies over 'x'. Therefore, 
I would assume that the function of the polynomial should always produce the 
same results as the predict() function in R produces. However, here are the raw 
data for anyone that has the time to help me out.

y:
[1] 9097 9074 9043 8978 8912 8847 8814 8786 8752 8722 8686 8657 8610 8604 8554
[16] 8546 8496 8482 8479 8462 8460 8438 8428 8418 8384

x:
[1] 17.50NA 20.59 21.43 17.78 21.89NA 22.86NA  6.10NA  5.37
[13]  3.80NA  6.80NANANA  5.80NANANANANA
[25]NA

I think that R lm() just ignores the NA values, but I've also tried this by 
first eliminating NAs and the corresponding x values from the data before 
fitting the poly and the result was the same coefficients. Thanks very much to 
anyone who is willing to provide information.

Chris Carleton

 CC: r-help@r-project.org
 From: r.tur...@auckland.ac.nz
 Subject: Re: [R] Polynomial Fitting
 Date: Tue, 29 Sep 2009 13:30:07 +1300
 To: w_chris_carle...@hotmail.com
 
 
 On 29/09/2009, at 10:52 AM, chris carleton wrote:
 
 
  Hello All,
 
   This might seem elementary to everyone, but please bear with me. I've
   just spent some time fitting poly functions to time series data in R
   using lm() and predict(). I want to analyze the functions once I've
   fit them to the various data I'm studying. However, after pulling the
   first function into Octave (just by plotting the polynomial function
   using fplot() over the same x interval as my original data) I was
   surprised to see that the scale and y values were vastly different
   than the ones I have in R. The basic shape of the polynomial over the
   same interval looks similar in both Octave and R, but the y values  
  are
   all different. When I compute the y values using the polynomial
   function by hand, the y values from the Octave plot are returned and
   not the y values predicted by predict() in R. Can someone explain to
   me why the values for a function would be different in R? Thanks,
   Chris Carleton
 
 Presumably because you were using poly() with the argument raw left
 equal to its default, i.e. FALSE.
 
   cheers,
 
   Rolf Turner
 
 P. S.  The posting guide asks for reproducible examples .
 
   R. T.
 
 ##
 Attention: 
 This e-mail message is privileged and confidential. If you are not the 
 intended recipient please delete the message and notify the sender. 
 Any views or opinions presented are solely those of the author.
 
 This e-mail has been scanned and cleared by MailMarshal 
 www.marshalsoftware.com
 ##
   
_


[[alternative HTML version deleted]]

__
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

2009-09-29 Thread Rolf Turner


On 30/09/2009, at 5:34 AM, chris carleton wrote:



Thanks for the response. I'm sorry I didn't provide the code or  
data example earlier. I was using the polynomial fitting technique  
of this form;


test - lm(x[,34] ~ I(x[,1]) + I(x[,1]^2) + I(x[,1]^3))

for the original fitting operation. I also tried to use;

lm(y ~ poly(x,3,raw=TRUE))

with the same results for the polynomial coefficients in both  
cases. If my understanding is correct, both of the methods above  
produce the coefficients of a polynomial based on the data in 'y'  
as that data varies over 'x'. Therefore, I would assume that the  
function of the polynomial should always produce the same results  
as the predict() function in R produces. However, here are the raw  
data for anyone that has the time to help me out.


y:
[1] 9097 9074 9043 8978 8912 8847 8814 8786 8752 8722 8686 8657  
8610 8604 8554

[16] 8546 8496 8482 8479 8462 8460 8438 8428 8418 8384

x:
[1] 17.50NA 20.59 21.43 17.78 21.89NA 22.86NA  6.10 
NA  5.37
[13]  3.80NA  6.80NANANA  5.80NANANA 
NANA

[25]NA

I think that R lm() just ignores the NA values, but I've also tried  
this by first eliminating NAs and the corresponding x values from  
the data before fitting the poly and the result was the same  
coefficients. Thanks very much to anyone who is willing to provide  
information.


What's the problem?

fit - lm(y~x+I(x^2)+I(x^3))
ccc - coef(fit)
X - cbind(1,x,x^2,x^3)
print(fitted(fit))
chk - X%*%ccc
print(chk[!is.na(chk)])
print(range(fitted(fit)-chk[!is.na(chk)]))
[1] 0.00e+00 5.456968e-12

The answers are the same.

cheers,

Rolf Turner

##
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}

__
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

2009-09-29 Thread chris carleton

Thanks a ton Rolf,

I was using the coefficients as given from summary(fit), which have been 
rounded. When I used coef(fit) as you've done, the poly function is the same 
now in Octave and returned the R predicted values as expected. Your time is 
appreciated,

C

 CC: r-help@r-project.org
 From: r.tur...@auckland.ac.nz
 Subject: Re: [R] Polynomial Fitting
 Date: Wed, 30 Sep 2009 08:30:15 +1300
 To: w_chris_carle...@hotmail.com
 
 
 On 30/09/2009, at 5:34 AM, chris carleton wrote:
 
 
  Thanks for the response. I'm sorry I didn't provide the code or  
  data example earlier. I was using the polynomial fitting technique  
  of this form;
 
  test - lm(x[,34] ~ I(x[,1]) + I(x[,1]^2) + I(x[,1]^3))
 
  for the original fitting operation. I also tried to use;
 
  lm(y ~ poly(x,3,raw=TRUE))
 
  with the same results for the polynomial coefficients in both  
  cases. If my understanding is correct, both of the methods above  
  produce the coefficients of a polynomial based on the data in 'y'  
  as that data varies over 'x'. Therefore, I would assume that the  
  function of the polynomial should always produce the same results  
  as the predict() function in R produces. However, here are the raw  
  data for anyone that has the time to help me out.
 
  y:
  [1] 9097 9074 9043 8978 8912 8847 8814 8786 8752 8722 8686 8657  
  8610 8604 8554
  [16] 8546 8496 8482 8479 8462 8460 8438 8428 8418 8384
 
  x:
  [1] 17.50NA 20.59 21.43 17.78 21.89NA 22.86NA  6.10 
  NA  5.37
  [13]  3.80NA  6.80NANANA  5.80NANANA 
  NANA
  [25]NA
 
  I think that R lm() just ignores the NA values, but I've also tried  
  this by first eliminating NAs and the corresponding x values from  
  the data before fitting the poly and the result was the same  
  coefficients. Thanks very much to anyone who is willing to provide  
  information.
 
 What's the problem?
 
 fit - lm(y~x+I(x^2)+I(x^3))
 ccc - coef(fit)
 X - cbind(1,x,x^2,x^3)
 print(fitted(fit))
 chk - X%*%ccc
 print(chk[!is.na(chk)])
 print(range(fitted(fit)-chk[!is.na(chk)]))
 [1] 0.00e+00 5.456968e-12
 
 The answers are the same.
 
   cheers,
 
   Rolf Turner
 
 ##
 Attention: 
 This e-mail message is privileged and confidential. If you are not the 
 intended recipient please delete the message and notify the sender. 
 Any views or opinions presented are solely those of the author.
 
 This e-mail has been scanned and cleared by MailMarshal 
 www.marshalsoftware.com
 ##
  
_


[[alternative HTML version deleted]]

__
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] Polynomial Fitting

2009-09-28 Thread chris carleton

Hello All,

 This might seem elementary to everyone, but please bear with me. I've 
 just spent some time fitting poly functions to time series data in R 
 using lm() and predict(). I want to analyze the functions once I've 
 fit them to the various data I'm studying. However, after pulling the 
 first function into Octave (just by plotting the polynomial function 
 using fplot() over the same x interval as my original data) I was 
 surprised to see that the scale and y values were vastly different 
 than the ones I have in R. The basic shape of the polynomial over the 
 same interval looks similar in both Octave and R, but the y values are 
 all different. When I compute the y values using the polynomial 
 function by hand, the y values from the Octave plot are returned and 
 not the y values predicted by predict() in R. Can someone explain to 
 me why the values for a function would be different in R? Thanks,
 Chris Carleton 
 
  

  
_


[[alternative HTML version deleted]]

__
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

2009-09-28 Thread Rolf Turner


On 29/09/2009, at 10:52 AM, chris carleton wrote:



Hello All,

 This might seem elementary to everyone, but please bear with me. I've
 just spent some time fitting poly functions to time series data in R
 using lm() and predict(). I want to analyze the functions once I've
 fit them to the various data I'm studying. However, after pulling the
 first function into Octave (just by plotting the polynomial function
 using fplot() over the same x interval as my original data) I was
 surprised to see that the scale and y values were vastly different
 than the ones I have in R. The basic shape of the polynomial over the
 same interval looks similar in both Octave and R, but the y values  
are

 all different. When I compute the y values using the polynomial
 function by hand, the y values from the Octave plot are returned and
 not the y values predicted by predict() in R. Can someone explain to
 me why the values for a function would be different in R? Thanks,
 Chris Carleton


Presumably because you were using poly() with the argument raw left
equal to its default, i.e. FALSE.

cheers,

Rolf Turner

P. S.  The posting guide asks for reproducible examples .

R. T.

##
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}

__
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.


[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: r-help@r-project.org
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.