[Rd] nls: constraints (lower/upper) (PR#8401)

bolker@zoo.ufl.edu bolker at zoo.ufl.edu
Thu Dec 15 20:40:37 CET 2005


    I found what seems to be a glaring bug in nls when using
constraints, but it is so glaring that I'm a bit nervous
about having been stupid.  I have (1) tried to make sure
I'm up to date:

platform i486-pc-linux-gnu
arch     i486
os       linux-gnu
system   i486, linux-gnu
status
major    2
minor    2.0
year     2005
month    10
day      06
svn rev  35749

  (2) skimmed through the SVN logs; (3) skimmed the bug
reporting system for references to "lower" or "constraint"

    The problem is that nls() doesn't seem to work with
constraints.  Specifically, it seems to fail to set
the "lower" and "upper" components of match.call() to NULL
before trying to evaluate the formula.
There is a simple fix that makes it work
fine (for me at least -- I haven't tested extensively).

   I would submit a bug report but I'm gun-shy ...

   Sample problem and fix:
--------------
x = runif(200)
a =1
b = 1
c = -0.1
y = a+b*x+c*x^2+rnorm(200,sd=0.05)
plot(x,y)
curve(a+b*x+c*x^2,add=TRUE)
nls(y~a+b*x+c*I(x^2),start=c(a=1,b=1,c=0.1),algorithm="port")
nls(y~a+b*x+c*I(x^2),start=c(a=1,b=1,c=0.1),algorithm="port",lower=c(0,0,0))
## Error in model.frame(formula, rownames, variables, varnames, extras, 
## extranames,  :
##      variable lengths differ

## hack nls()
sink("mynls.R")
nls
sink()

## replace line 34:
## ORIGINAL:
##  mf$start <- mf$control <- mf$algorithm <-
##  mf$trace <- mf$model <- NULL
##    REVISED:
##  mf$start <- mf$control <- mf$algorithm <- mf$trace <- mf$model <-
##      mf$lower <- mf$upper <- NULL
## add "mynls <- " at the beginning, delete namespace code at the end
source("mynls.R")
mynls(y~a+b*x+c*I(x^2),start=c(a=1,b=1,c=0.1),algorithm="port",lower=c(0,0,0))

## works beautifully


-- 
620B Bartram Hall                            bolker at zoo.ufl.edu
Zoology Department, University of Florida    http://www.zoo.ufl.edu/bolker
Box 118525                                   (ph)  352-392-5697
Gainesville, FL 32611-8525                   (fax) 352-392-3704



More information about the R-devel mailing list