Re: [R] nls with some coefficients fixed

2010-07-21 Thread Keith Jewell

Gabor Grothendieck ggrothendi...@gmail.com wrote in message 
news:aanlktilszaicycu3lz2f5d_bxq1g8m8f7jsjsbj2l...@mail.gmail.com...
 On Tue, Jul 20, 2010 at 9:58 AM,  nas...@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

__
R-help@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.


Re: [R] nls with some coefficients fixed

2010-07-21 Thread Keith Jewell

Gabor Grothendieck ggrothendi...@gmail.com wrote in message 
news:aanlktilszaicycu3lz2f5d_bxq1g8m8f7jsjsbj2l...@mail.gmail.com...
 On Tue, Jul 20, 2010 at 9:58 AM,  nas...@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

__
R-help@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.


[R] nls with some coefficients fixed

2010-07-20 Thread nashjc
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().

JN

__
R-help@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.


Re: [R] nls with some coefficients fixed

2010-07-20 Thread Keith Jewell
nas...@uottawa.ca wrote in message 
news:33466.129.6.253.2.1279634282.squir...@webmail02.uottawa.ca...
 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().

 JN


Thanks for that. It would be burdensome to move away from nls, a lot of my 
functions depend on nls-like properties of fitted objects.

I'm considering 'fixing' nls by creating a 'wrapper' with the same name to:
a) if start is unspecified, call getInitial
b) call 'vanilla' nls with start restricted to 'non-fixed' terms,
something like...
-
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)], ...]
}
--
The getInitial values will be calculated ignoring the fact that some 
parameters are 'fixed' (unless the particular 'initial' attribute is very 
clever!), but they shouldn't be too bad, and I don't really see a practical 
alternative way of getting the required starting values.

I think this will work, but I'm very doubtful if it will work reliably. 
Issues that concern me include:

a) How will 'data=parent.frame()' behave?
   I've had troubles before with scope and nls!

b) Will other callers of nls (e.g. nlme:::nlsList) call mine or the 
original?
   Search list is something like
   [1].GlobalEnv[n]mine   [n+l]package:nlme [n+l+m]package:stats

Any comments?

Thanks in advance,

Keith J

__
R-help@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.


Re: [R] nls with some coefficients fixed

2010-07-20 Thread Gabor Grothendieck
On Tue, Jul 20, 2010 at 9:58 AM,  nas...@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))

__
R-help@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.


[R] nls with some coefficients fixed

2010-07-19 Thread Keith Jewell
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 = 
DNase.1)
a b   0.8
4.608 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

__
R-help@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.