[R] non linear models

John Fox jfox at mcmaster.ca
Mon Oct 1 00:15:06 CEST 2001


Dear Christian,

Polynomial-regression models are linear in the parameters, so they can be 
fit by lm. There are several ways to do your example:

 > x <- c(-1, 0, 1)
 > y <- c(2, 1, 2)

 > lm(y ~ I(x^2))

Call:
lm(formula = y ~ I(x^2))

Coefficients:
(Intercept)       I(x^2)
           1            1

 > lm(y ~ x + I(x^2))  # this fits a term in x as well as x^2

Call:
lm(formula = y ~ x + I(x^2))

Coefficients:
(Intercept)            x       I(x^2)
    1.00e+00    -7.85e-17     1.00e+00

 > lm(y ~ poly(x, 2)) # this fits order-2 orthogonal polynomials

Call:
lm(formula = y ~ poly(x, 2))

Coefficients:
(Intercept)  poly(x, 2)1  poly(x, 2)2
   1.667e+00   -3.127e-16    8.165e-01

I hope that this helps,
  John

At 11:59 AM 30/09/2001 -0400, Christian Endter wrote:
>Dear Members of the Help List,
>
>Honestly, I feel a little bit stupid - I would like to do something rather
>simple: fit a non linear model to existing data, to be more precise I wanted
>to start with simple higher order polynomials.
>
>Unfortunately, I do not quite understand the examples in the helpfiles for
>the nlm, nls and nlsModel commands.
>
>Could anyone please provide a simple example to get me started (i.e. y = p +
>x^2 fitted to x= -1 0 1 y = 2 1 2; a simple parabola p should turn out to be
>1). How do I do this and how do I do the same for something like y = a + bx
>+ cx^2 + dx^3 ??

-----------------------------------------------------
John Fox
Department of Sociology
McMaster University
Hamilton, Ontario, Canada L8S 4M4
email: jfox at mcmaster.ca
phone: 905-525-9140x23604
web: www.socsci.mcmaster.ca/jfox
-----------------------------------------------------

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list