Firas Swidan, PhD
2008-Mar-07 08:16 UTC
[R] Puzzling coefficients for linear fitting to polynom
Hi, I can not comprehend the linear fitting results of polynoms. For example, given the following data (representing y = x^2):> x <- 1:3 > y <- c(1, 4, 9)performing a linear fit> f <- lm(y ~ poly(x, 2))gives weird coefficients:> coefficients(f)(Intercept) poly(x, 2)1 poly(x, 2)2 4.6666667 5.6568542 0.8164966 However the fitted() result makes sense:> fitted(f)1 2 3 1 4 9 This is very confusing. How should one understand the result of coefficients()? Thanks for any tips, Firas. -- Firas Swidan, PhD Founder and CEO Olymons: Blessing Machines with Vision (TM) http://www.olymons.com P.O.Box 8125 Nazareth 16480 Israel Cell: +.972.(0)54.733.1788
Firas Swidan, PhD
2008-Mar-07 08:31 UTC
[R] Puzzling coefficients for linear fitting to polynom
Hi, I can not comprehend the linear fitting results of polynoms. For example, given the following data (representing y = x^2):> x <- 1:3 > y <- c(1, 4, 9)performing a linear fit> f <- lm(y ~ poly(x, 2))gives weird coefficients:> coefficients(f)(Intercept) poly(x, 2)1 poly(x, 2)2 4.6666667 5.6568542 0.8164966 However the fitted() result makes sense:> fitted(f)1 2 3 1 4 9 This is very confusing. How should one understand the result of coefficients()? Thanks for any tips, Firas. -- Firas Swidan, PhD Founder and CEO Olymons: Blessing Machines with Vision (TM) http://www.olymons.com P.O.Box 8125 Nazareth 16480 Israel Cell: +.972.(0)54.733.1788
Dimitris Rizopoulos
2008-Mar-07 08:32 UTC
[R] Puzzling coefficients for linear fitting to polynom
poly() computes by default orthogonal polynomials; check the online help file for poly() for more info. Probably you want to use the 'raw' argument in this example, i.e., x <- 1:3 y <- c(1, 4, 9) lm(y ~ poly(x, 2, raw = TRUE)) I hope this helps. Best, Dimitris ---- Dimitris Rizopoulos 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: "Firas Swidan, PhD" <frsswdn at gmail.com> To: <r-help at r-project.org> Sent: Friday, March 07, 2008 9:16 AM Subject: [R] Puzzling coefficients for linear fitting to polynom> Hi, > > I can not comprehend the linear fitting results of polynoms. For > example, given the following data (representing y = x^2): > >> x <- 1:3 >> y <- c(1, 4, 9) > > performing a linear fit > >> f <- lm(y ~ poly(x, 2)) > > gives weird coefficients: > >> coefficients(f) > (Intercept) poly(x, 2)1 poly(x, 2)2 > 4.6666667 5.6568542 0.8164966 > > However the fitted() result makes sense: > >> fitted(f) > 1 2 3 > 1 4 9 > > This is very confusing. How should one understand the result of > coefficients()? > > Thanks for any tips, > Firas. > > -- > Firas Swidan, PhD > Founder and CEO > Olymons: Blessing Machines with Vision (TM) > http://www.olymons.com > P.O.Box 8125 > Nazareth 16480 > Israel > Cell: +.972.(0)54.733.1788 > > ______________________________________________ > 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. >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Bill.Venables at csiro.au
2008-Mar-07 08:33 UTC
[R] Puzzling coefficients for linear fitting to polynom
It does help if you read the help information for poly.> ?poly > x <- 1:3 > y <- c(1, 4, 9) > f <- lm(y ~ poly(x, 2, raw = TRUE)) ## note raw = TRUE > coef(f)(Intercept) poly(x, 2, raw = TRUE)1 poly(x, 2, raw = TRUE)2 0 0 1>You were assuming a power basis for the polynomial, 1, x, x^2. If you want to use that you must declare that using raw = TRUE. The default is to use an orthogonal polynomial basis, and you can expect the coefficients relative to that to be, well, puzzling. Bill Venables CSIRO Laboratories PO Box 120, Cleveland, 4163 AUSTRALIA Office Phone (email preferred): +61 7 3826 7251 Fax (if absolutely necessary): +61 7 3826 7304 Mobile: +61 4 8819 4402 Home Phone: +61 7 3286 7700 mailto:Bill.Venables at csiro.au http://www.cmis.csiro.au/bill.venables/ -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Firas Swidan, PhD Sent: Friday, 7 March 2008 6:16 PM To: r-help at r-project.org Subject: [R] Puzzling coefficients for linear fitting to polynom Hi, I can not comprehend the linear fitting results of polynoms. For example, given the following data (representing y = x^2):> x <- 1:3 > y <- c(1, 4, 9)performing a linear fit> f <- lm(y ~ poly(x, 2))gives weird coefficients:> coefficients(f)(Intercept) poly(x, 2)1 poly(x, 2)2 4.6666667 5.6568542 0.8164966 However the fitted() result makes sense:> fitted(f)1 2 3 1 4 9 This is very confusing. How should one understand the result of coefficients()? Thanks for any tips, Firas. -- Firas Swidan, PhD Founder and CEO Olymons: Blessing Machines with Vision (TM) http://www.olymons.com P.O.Box 8125 Nazareth 16480 Israel Cell: +.972.(0)54.733.1788 ______________________________________________ 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.
(Ted Harding)
2008-Mar-07 08:40 UTC
[R] Puzzling coefficients for linear fitting to polynom
On 07-Mar-08 08:16:06, Firas Swidan, PhD wrote:> Hi, > I can not comprehend the linear fitting results of polynoms. > For example, given the following data (representing y = x^2): > >> x <- 1:3 >> y <- c(1, 4, 9) > > performing a linear fit > >> f <- lm(y ~ poly(x, 2)) > > gives weird coefficients: > >> coefficients(f) > (Intercept) poly(x, 2)1 poly(x, 2)2 > 4.6666667 5.6568542 0.8164966 > > However the fitted() result makes sense: > >> fitted(f) > 1 2 3 > 1 4 9 > > This is very confusing. How should one understand the result of > coefficients()? > > Thanks for any tips, > Firas.Have a look at the values returned by poly(x,2). The coefficients you are getting are the results of fitting y = a + b1*poly(x,2)[,1] + b2*poly(x,2)[,2] where poly(x, 2)[,1] # [1] -7.071068e-01 -9.073264e-17 7.071068e-01 poly(x, 2)[,2] # [1] 0.4082483 -0.8164966 0.4082483 which is probably not what you may have thought you were doing! It is certainly not the same as fitting y = a + b1*x + b2*(x^2) though of course the fitted values will be the same. Best wishes, Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 07-Mar-08 Time: 08:40:46 ------------------------------ XFMail ------------------------------
Firas Swidan, PhD
2008-Mar-07 08:44 UTC
[R] Puzzling coefficients for linear fitting to polynom
Thanks for the clarifications. It seems the confusion resulted from making one assumption more than necessary regarding the behavior of poly(). Best wishes, Firas. On Fri, 2008-03-07 at 18:33 +1000, Bill.Venables at csiro.au wrote:> It does help if you read the help information for poly. > > > ?poly > > x <- 1:3 > > y <- c(1, 4, 9) > > f <- lm(y ~ poly(x, 2, raw = TRUE)) ## note raw = TRUE > > coef(f) > (Intercept) poly(x, 2, raw = TRUE)1 poly(x, 2, raw = TRUE)2 > 0 0 1 > > > > You were assuming a power basis for the polynomial, 1, x, x^2. If you > want to use that you must declare that using raw = TRUE. The default is > to use an orthogonal polynomial basis, and you can expect the > coefficients relative to that to be, well, puzzling. > > Bill Venables > CSIRO Laboratories > PO Box 120, Cleveland, 4163 > AUSTRALIA > Office Phone (email preferred): +61 7 3826 7251 > Fax (if absolutely necessary): +61 7 3826 7304 > Mobile: +61 4 8819 4402 > Home Phone: +61 7 3286 7700 > mailto:Bill.Venables at csiro.au > http://www.cmis.csiro.au/bill.venables/ > > -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] > On Behalf Of Firas Swidan, PhD > Sent: Friday, 7 March 2008 6:16 PM > To: r-help at r-project.org > Subject: [R] Puzzling coefficients for linear fitting to polynom > > Hi, > > I can not comprehend the linear fitting results of polynoms. For > example, given the following data (representing y = x^2): > > > x <- 1:3 > > y <- c(1, 4, 9) > > performing a linear fit > > > f <- lm(y ~ poly(x, 2)) > > gives weird coefficients: > > > coefficients(f) > (Intercept) poly(x, 2)1 poly(x, 2)2 > 4.6666667 5.6568542 0.8164966 > > However the fitted() result makes sense: > > > fitted(f) > 1 2 3 > 1 4 9 > > This is very confusing. How should one understand the result of > coefficients()? > > Thanks for any tips, > Firas. >-- Firas Swidan, PhD Founder and CEO Olymons: Blessing Machines with Vision (TM) http://www.olymons.com P.O.Box 8125 Nazareth 16480 Israel Cell: +.972.(0)54.733.1788