From bolker at zoo.ufl.edu  Thu Jan 19 21:50:40 2006
From: bolker at zoo.ufl.edu (bolker@zoo.ufl.edu)
Date: Thu, 19 Jan 2006 21:50:40 +0100 (CET)
Subject: [Rd] nls profiling with algorithm="port" may violate bounds
	(PR#8508)
Message-ID: <20060119205040.3F5DD206FA@slim.kubism.ku.dk>

  [posted to R-devel, no discussion:
resubmitting it as a bug, just so it gets
logged appropriately]

   Sorry to report further difficulties with
nls and profiling and constraints ... the problem
this time (which I didn't check for in my last
round of testing) is that the nls profiler doesn't
seem to respect constraints that have been
set when using the port algorithm.
    See test code below ...
    If I can I will try to hack the code, but I will
probably start by redefining my function with
some workarounds to make the fit quadratically "bad" (but well-defined)
when the parameters are negative ...
     As always, please don't hesitate to correct me
if I'm being an idiot ...

    cheers
      Ben Bolker

-----------------------
rm(list=ls())

npts=10
set.seed(1001)

a =2
b =0.5
x= runif(npts)
y = a*x/(1+a*b*x)+rnorm(npts,sd=0.2)

gfun <- function(a,b,x) {
    if (a<0 || b<0) stop("bounds violated")
    a*x/(1+a*b*x)
}

m1 = nls(y~gfun(a,b,x),algorithm="port",
    lower=c(0,0),start=c(a=1,b=1))

try(confint(m1))
----------------

for what it's worth, the logic appears to be OK in mle in the stats4
library:
--------------
library(stats4)

mfun <- function(a,b,s) {
    if (a<0 || b<0 || s<0) stop("bounds violated")
    -sum(dnorm(y,a*x/(1+a*b*x),sd=s,log=TRUE))
}

m2 = mle(minuslogl=mfun,
    start=list(a=1,b=1,s=0.1),
    method="L-BFGS-B",lower=c(0.002,0.002,0.002))

confint(m2)

m2b = mle(minuslogl=mfun,
    fixed=list(b=0),start=list(a=1,s=0.1),
    method="L-BFGS-B",lower=c(0.002,0.002,0.002))
## set boundary slightly above zero to avoid
## boundary cases

dev <- 2*(-logLik(m2b)+logLik(m2))
as.numeric(pchisq(dev,lower.tail=FALSE,df=1))



[Rd] nls profiling with algorithm="port" may violate bounds (PR#8508)
bolker@zoo.ufl.edu