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 ?? Thank you very much, Christian Endter -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._