[R] "may be used in an incorrect context" [solved]
Ross Boylan
ross at biostat.ucsf.edu
Tue Jul 19 04:58:11 CEST 2011
On Tue, 2011-07-19 at 13:55 +1200, Rolf Turner wrote:
> I can't give you a definitive answer, but your use of "..." in your call
> to optim()
> makes no sense at all to me. The "..." argument to optim() is used to
> pass further
> arguments (in addition to the argument over which the objective function is
> being optimized) to the objective function. If these "further
> arguments" are to
> be specified in a "..." argument to the function which is actually
> calling optim()
> then your call to optim() *could* make sense.
Just the hint I needed! Thank you. The problem is actually not further
down (the inner function being optimized over has no ...), but further
up.
The arguments I wanted to capture were control parameters for optim
itself. However, the outer function had no ... argument, and so it was
not a meaningful reference.
I added ... to the outer function, which in turn revealed that, for
various reasons, I wasn't going to be able to pass arguments down that
way (e.g., some of the arguments I wanted to pass were already named
arguments--with different meanings--in some of the functions through
which the ... was passed.
Although I haven't gotten things to run with the added ..., the warnings
(which became runtime errors) went away.
Here's a small example:
foo <- function(a, ...) {
a+10
}
bar <- function(b){
b*foo(3, ...)
}
> bar(4)
Error in bar(4) : '...' used in an incorrect context
The body of bar references ..., but it is not in the formal parameter
list of bar.
bar <- function(b, ...)
would make the reference to ... in the body sensible.
Ross
>
> Except for the fact that your objective function --- agfitfn() --- has
> no further
> arguments at all! It is a function of beta only. (Whence I suspect
> that there
> is no "..." argument in the calling function and that you are putting in
> "..."
> in the call to optim() in a mindless manner with no idea of what the
> inclusion
> of this argument is supposed to accomplish. Feel free to demonstrate that
> I am wrong in my suspicion.)
>
> If your objective function (here agfitfn()) actually had further
> arguments (say "melvin",
> "clyde", and "irving" and if your calling function had usage of the form
>
> foo(this,that,theOther,...)
>
> then you might call foo() in the manner
>
> gorp <-
> foo(17,42,xxx,melvin=someThing,clyde=someThingElse,irving=whatEver)
>
> where xxx, someThing, someThingElse and whatEver are previously defined
> objects. Then
> inside foo() a call to optim() of the form
>
> r <- optim(init,agfitfn,...)
>
> would be sensible. The value of "..." would be the (named) list with
> components
> melvin=someThing, clyde=someThingElse and irving=whatEver. This would
> result
> in agfitfn() being called with arguments beta, melvin=someThing,
> clyde=someThingElse
> and irving=whatEver, for various values of beta (as optim() searched
> over various values
> of beta for the optimum).
>
> Note that in the call to foo() the "..." arguments must be specified in
> the name=value
> manner.
>
> But since agfitfn() is a function of beta only, just lose the "..."
> argument, and your worries
> will vanish.
>
> cheers,
>
> Rolf Turner
>
> On 19/07/11 12:48, Ross Boylan wrote:
> > R CMD check tells me
> > * checking R code for possible problems ... NOTE
> > agexact.fit.rds: ... may be used in an incorrect context: ‘optim(init,
> > agfitfn, ...)’
> > Warning:<anonymous>: ... may be used in an incorrect context: ‘optim(init, agfitfn, ...)’
> >
> > Can anyone tell me what this message means? My searches haven't turned
> > up anything useful. This is with R 2.7 and 2.9.
> >
> > The message appears to be referring to a section of code whose
> > highlights are
> > agfitfn<- function(beta) {
> > r<- .C("agexactrds", iter= as.integer(control$iter.max),
> > as.integer(n),
> > as.integer(nvar), sstart, sstop,
> > sstat,
> > x= x[sorted,],
> > sextra,
> > as.integer(length(response.prob)),
> > as.double(response.prob),
> > as.double(alpha),
> > as.double(offset[sorted] - mean(offset)),
> > newstrat,
> > means = double(nvar),
> > coef= as.double(beta),
> > u = double(nvar),
> > imat= double(nvar*nvar), loglik=double(2),
> > flag=integer(1),
> > double(2*nvar*nvar +nvar*4 + n),
> > integer(2*n),
> > as.double(control$eps),
> > as.double(control$toler.chol),
> > sctest=double(1),
> > as.integer(length(debugfile)),
> > as.character(debugfile),
> > as.integer(1), # dumb mode
> > PACKAGE="survivalrds" )
> > - r$loglik[2]
> > }
> > r<- optim(init, agfitfn, ...)
> > return(r)
> >
> > All of this is in the body of
> > agexact.fit.rds<- function([stuff] {
> > [more stuff]
> > }
> >
> > Thanks.
> > Ross
> >
> > ______________________________________________
> > R-help at 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.
>
More information about the R-help
mailing list