# [R] nls() and nls2() behavior?

Gabor Grothendieck ggrothendieck at gmail.com
Tue May 11 21:29:02 CEST 2010

```On Tue, May 11, 2010 at 11:24 AM, Walmes Zeviani
<walmeszeviani at hotmail.com> wrote:
>
> I think the problem with nls() is the model specification. Look:
>
>> y <- c(0.4334,0.3200,0.5848,0.6214,0.3890,0.5233,0.4753,
> +        0.2104,0.3240,0.2827,0.3847,0.5571,0.5432,0.1326,0.3481)
>> x <- c(0.3521,0.4334,0.3200,0.5848,0.6214,0.3890,0.5233,
> +        0.1379,0.2104,0.3240,0.3404,0.3847,0.5571,0.5432,0.1326)
>> dummy <- rep(c("m1","m2","m3"), c(7,3,5))
>>
>> d <- data.frame(y=y, x=x, dummy=dummy)
>>
>> n0 <- nls(y~b0[dummy]+b1*x, data=d,
> +           start=list(b0=c(1,1,1), b1=1))
>> summary(n0)
>
> Formula: y ~ b0[dummy] + b1 * x
>
> Parameters:
>    Estimate Std. Error t value Pr(>|t|)
> b01  0.48616    0.13993   3.474  0.00520 **
> b02  0.27626    0.09970   2.771  0.01820 *
> b03  0.39994    0.12597   3.175  0.00884 **
> b1  -0.01735    0.28352  -0.061  0.95230
> ---
> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
>
> Residual standard error: 0.1331 on 11 degrees of freedom
>
> Number of iterations to convergence: 1
> Achieved convergence tolerance: 2.713e-07
>
> I don't know abou nls2().

nls2 does not support vector parameters.  Try this to calculate the
value of the formula at the start value:

> library(nls2)
> n0 <- nls2(y ~ c(b01, b02, b03)[dummy]+b1*x, data=d, alg = "brute",
+            start=list(b01 = 1, b02 = 1, b03 = 1, b1=1))
> fitted(n0)
 1.3521 1.4334 1.3200 1.5848 1.6214 1.3890 1.5233 1.1379 1.2104 1.3240
 1.3404 1.3847 1.5571 1.5432 1.1326
attr(,"label")
 "Fitted values"

```