[R] R quadreg

Petr Savicky savicky at praha1.ff.cuni.cz
Mon Feb 28 21:36:48 CET 2011


On Mon, Feb 28, 2011 at 08:41:02AM -0800, knut-o wrote:
> Hi
> I have a x-list and a y-list and I'm trying to make a line that goes ca.
> through the dots. I want to find a, b and c in ax^2 + bx + c.
> How to do?

Hi.

Try the following

  x <- rnorm(100)
  y <- 4*x^2 + 3*x + 2 + 0.2*rnorm(100)
  z <- poly(x, degree=2, raw=TRUE)
  lm(y ~ z)$coefficients

  (Intercept)          z1          z2 
     2.000131    3.013591    4.000884 

or also

  x2 <- x^2
  lm(y ~ x + x2)$coefficients

with the same result.

Hope this helps.

Petr Savicky.



More information about the R-help mailing list