[R] nls with some coefficients fixed

Keith Jewell k.jewell at campden.co.uk
Wed Jul 21 10:44:02 CEST 2010


"Gabor Grothendieck" <ggrothendieck at gmail.com> wrote in message 
news:AANLkTilSZAiCyCu3Lz2F5d_bxQ1G8M8F7JsjSbJ2lWOU at mail.gmail.com...
> On Tue, Jul 20, 2010 at 9:58 AM,  <nashjc at uottawa.ca> wrote:
>> For nls, the fixing (or masking) of parameters is not, to my knowledge,
>> possible.
>>
>> This is something I've been trying to get in such routines for over 2
>> decades. Masks are
>> available, but not yet well documented, in Rcgmin and Rvmmin packages.
>> However, these use
>> an optim() style approach, which is quite different from nls(). If 
>> there's
>> sufficient
>> interest and some collaboration, I'd be willing to have a go at providing
>> such
>> functionality, but it would take quite a bit of work to provide the full
>> capability of nls().
>>
>
> You can optimize over b while fixing m like this:
>
> m <- 1
> nls(demand ~ m + b * Time, BOD, start = c(b = 1))
>
Thanks Gabor,

I'd recognised that approach in my original post starting this thread (19 
July) where I'd used the example:
#--------------------------------
# fix parameter and use explicit start values works fine
nls(density ~ SSgompertz(log(conc), Asym, b2, b3=0.8), data = DNase.1, 
start=list(Asym=3, b2=2))
#---------------------------

I'm fitting many different models so I need a more generic approach.
I'd appreciate any comments on the suggestion in my 20 July post? (repeated 
here with some typo's corrected)
-----------------
nls <- function(formula, data=parent.frame(), start, ...){
  if (missing(start)) start <- getInitial(formula, data)
  stats:::nls(formula, data, start=start[names(start) %in% 
all.vars(formula)], ...)
}
----------------------

I see it breaks nls for non-selfStart functions; e.g. omitting the explicit 
start from your example
  nls(demand ~ m + b * Time, BOD)
works fine with vanilla nls (albeit with a warning)
#   No starting values specified for some parameters.
# Intializing 'b' to '1.'.
# Consider specifying 'start' or using a selfStart model
but my wrapper breaks it altogether.
 # Error in getInitial.default(func, data, mCall = as.list(match.call(func, 
:
 #  no 'getInitial' method found for "function" objects

I think that's OK in my application - I either use selfStart functions or 
explicit start lists - but I don't really want to remove existing 
functionality.

I guess I could (!?!) dive into nls and implement the same kind of approach 
internally, but that's very deep water for me :-{

Best regards,

Keith J



More information about the R-help mailing list