# [R] nls: example code throws error

Duncan Mackay mackay at northnet.com.au
Fri Apr 26 09:36:21 CEST 2013

```Hi

Try

x <- -(1:100)/10
set.seed(1)
y <- 100 + 10 * exp(x / 2) + rnorm(x)/10

short cut to starting values
lm(log(y) ~-log(x+10))
Call:
lm(formula = log(y) ~ -log(x + 10))

Coefficients:
(Intercept)
4.624

nlmod <- nls(y ~  A + B * exp(C * x), start=list(A=90, B=5,C=0.1))

Formula: y ~ A + B * exp(C * x)

Parameters:
Estimate Std. Error t value Pr(>|t|)
A 100.009079   0.017797  5619.4   <2e-16
B   9.999993   0.042718   234.1   <2e-16
C   0.499529   0.004495   111.1   <2e-16

Residual standard error: 0.09073 on 97 degrees of freedom

Number of iterations to convergence: 5
Achieved convergence tolerance: 0.00000002475

I will leave you to plot the results as a check

Duncan

Duncan Mackay
Department of Agronomy and Soil Science
University of New England
Armidale NSW 2351
Email: home: mackay at northnet.com.au

At 09:16 26/04/2013, you wrote:
>Content-Type: text/plain
>Content-Disposition: inline
>Content-length: 899
>
>Greets,
>
>I'm trying to learn to use nls and was running the example code for
>an exponential model:
>
> > x <- -(1:100)/10
> > y <- 100 + 10 * exp(x / 2) + rnorm(x)/10
> > nlmod <- nls(y ~  Const + A * exp(B * x))
>Error in B * x : non-numeric argument to binary operator
>In addition: Warning message:
>In nls(y ~ Const + A * exp(B * x)) :
>   No starting values specified for some parameters.
>Initializing 'Const' to '1.'.
>Consider specifying 'start' or using a selfStart model
>
>Presumably, the code should work if it is part of an example on the
>help page. In perusing various help forums for similar problems, it
>also appears that others believe this syntax should work in the model formula.
>
>Any ideas?
>
>Perhaps also, a pointer to a comprehensive and correct document that
>details model formulae syntax if someone has one?
>
>Thanks & Best Regards,
>Steven
>
>
>
>         [[alternative HTML version deleted]]
>
>
>______________________________________________
>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.

```

More information about the R-help mailing list