[R] nls with some coefficients fixed

Keith Jewell k.jewell at campden.co.uk
Mon Jul 19 16:17:26 CEST 2010

I'm using nls to fit a variety of different models. Here I use SSgompertz as 
an example.

I want the ability to fix one (or more) of the coefficients that would 
normally be optimised (e.g. fix b3=0.8).

Examples; based on and using data from example(SSgompertz)
# vanilla call to nls, no coefficients fixed, works fine
nls(density ~ SSgompertz(log(conc), Asym, b2, b3), data = DNase.1)

# naive attempt to fix one parameter. I don't believe the error message
nls(density ~ SSgompertz(log(conc), Asym, b2, b3=0.8), data = DNase.1)
# Error in nlsModel(formula, mf, start, wts) :
#  singular gradient matrix at initial parameter estimates

# 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))

# getInitial returns third coeff, treating value as a name
getInitial(density ~ SSgompertz(log(conc), Asym=a, b2=b, b3=0.8), data = 
        a         b       0.8
4.6033338 2.2713391 0.7164666

I guess the best approach in principle is to change the initial attribute so 
it only estimates and returns values of those coefficients for which 
is.name(mCall[coef]) == TRUE. BUT,that means handling all combinations of 
fixed coefficients so it's a bit of work even for one model. I'm working 
with a large number of models and really don't want to face all that work!

An alternative would be for nls to recognise fixed coefficients and omit 
their values from the start list; the other coefficients might not be 
optimal for the values of the fixed, but it does seem to work and wouldn't 
need all those SSxxx altering. BUT I don't want to get into nls internals.

Any suggestions?

Thanks in advance,

Keith J

More information about the R-help mailing list