[R] Error using nls()

Gabor Grothendieck ggrothendieck at gmail.com
Fri Sep 21 15:28:05 CEST 2007


You need better starting values.  Try setting b3=1 and solving using
lm using that result as your starting values in nls:

> cc <- coef(lm(log(q1)~d1,data=x))
> cc <- c(cc, 1)
> names(cc) <- c("b1", "b2", "b3")
> nls(log(q1)~b1+(b2/b3)*(d1^b3-1),data=x,start=cc,trace=TRUE)
225.0784 :  1.515604 4.329543 1.000000
0.1243237 :    5.807118   8.966687 -13.977948
0.08707836 :    5.807118  10.295483 -13.329564
0.08704864 :    5.807118  10.298223 -13.430181
0.08704864 :    5.807118  10.298319 -13.430735
Nonlinear regression model
  model:  log(q1) ~ b1 + (b2/b3) * (d1^b3 - 1)
   data:  x
     b1      b2      b3
  5.807  10.298 -13.431
 residual sum-of-squares: 0.08705

Number of iterations to convergence: 4
Achieved convergence tolerance: 1.27e-09


On 9/21/07, Dietrich Trenkler <Dietrich.Trenkler at uni-osnabrueck.de> wrote:
> Hallo HelpeRs,
>
> I try to reconstruct some results from an econometric text book
> (Heij et al. (2004), pp. 218-20).
>
> For the data
>
>  > x <- structure(list(q1 = c(345, 331, 320, 314, 299, 395, 415,
>    490, 547, 656, 628, 627), d1 = c(1, 1, 1, 1, 1, 1, 1.05,
>    1.05, 1.05, 1.15, 1.15, 1.15)), .Names = c("q1", "d1"), row.names =
> as.integer(c(NA,
>    12)), class = "data.frame")
>
> I tried to estimate a nonlinear regression model using nls(). I get
>
>  >
> nls(log(q1)~b1+(b2/b3)*(d1^b3-1),data=x,start=list(b1=0,b2=1,b3=1),trace=TRUE)
> 246.132 :  0.0000  1.0000  1.0000 51.0907
> Error in qr.solve(QR.B, cc) : singular matrix 'a' in solve
>
> However, using:
>
>  > jjf <- function(x){z <- log(q1)-x[1]+(x[2]/x[3])*(d1^x[3]-1);sum(z*z)}
>  > optim(c(0,1,1),jjf)
>
> rendered (some of) the results desired in a jiffy. What am I doing
> wrong?
>
> Dietrich
>
> --
> Dietrich Trenkler c/o Universitaet Osnabrueck
> Rolandstr. 8; D-49069 Osnabrueck, Germany
> email: Dietrich.Trenkler at Uni-Osnabrueck.de
>
> ______________________________________________
> 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