[R] "may be used in an incorrect context"

Rolf Turner rolf.turner at xtra.co.nz
Tue Jul 19 03:55:00 CEST 2011


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.

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