[R] non linear models

Prof Brian Ripley ripley at stats.ox.ac.uk
Sun Sep 30 22:26:12 CEST 2001


On Sun, 30 Sep 2001, 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.

Which in that sense are not non-linear models...

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

Fitting to noise-free data is not a good test, so I've added some more
observations (or the cubic is not determined) and some noise.

set.seed(1)
x <- c(-1:1, runif(10, -1, 1))
y <- 1+ x^2+ rnorm(13, 0, 0.1)

quadratic:

lm(y ~ offset(x^2))
Coefficients:
(Intercept)
      0.977

via nls:

nls(y ~ p + I(x^2), start=list(p=0))
Nonlinear regression model
  model:  y ~ p + I(x^2)
   data:  parent.frame
        p
0.9769938
 residual sum-of-squares:  0.1429682

General cubic:

lm(y ~ x + I(x^2) + I(x^3))
Coefficients:
(Intercept)            x       I(x^2)       I(x^3)
     1.0224      -0.1833       0.9425       0.2188

or (better) use orthogonal polynomials

lm(y ~ poly(x, 3))
Coefficients:
(Intercept)  poly(x, 3)1  poly(x, 3)2  poly(x, 3)3
     1.4543      -0.8127       1.2372       0.1289

or via nls

nls(y ~ a + b*x +  c*x^2 + d*x^3, start=list(a=0, b=0, c=0, d=0))
Nonlinear regression model
  model:  y ~ a + b * x + c * x^2 + d * x^3
   data:  parent.frame
         a          b          c          d
 1.0224028 -0.1833263  0.9425109  0.2187559
 residual sum-of-squares:  0.1213026


-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
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 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

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