Dear John,

After further thought, it's probably a better idea to evaluate the weights argument in the same environment as the formula rather than to bypass nonstandard evaluation. You could use lm() as a guide. I don't entirely understand what you're trying to do so maybe this suggestion is off-base.

Best,
 John

On 2022-08-13 9:41 a.m., J C Nash wrote:

Thanks to John Fox and Noah Greifer. Both their approaches resolved my immediate
problem.

That is, to provide a summary of the fix of my example code,

tw <- function(formula, data, start, control, trace, weights) {
   firstcoef <- c(b1=199, b2=50, b3=0.3)
   cat("firstcoef:\n")
   print(firstcoef)
   cat("weights:"); print(weights)
# Following fails -- closure error
#  secondw<-nls(formula, data, firstcoef, control, algorithm=NULL, TRUE, weights=weights)
# from noah.grei...@gmail.com # this works OK
  secondw <- do.call("nls", list(formula, data, firstcoef, control, algorithm=NULL, TRUE, weights = weights))
#  As does putting weights in the data dataframe (here not active)
#  data$weights <- weights # from John Fox
#  secondw<-nls(formula, data, firstcoef, control, algorithm=NULL, TRUE, weights=weights)
   secondw
}

Afraid I avoid the wonders of non-standard evaluation, and this time it jumped up and bit me. But then I remember what machine instruction 260000800009 did on an IBM 1620.

The swiftness of reply from John and Noah was much appreciated.

Best, JN
--
John Fox, Professor Emeritus
McMaster University
Hamilton, Ontario, Canada
web: https://socialsciences.mcmaster.ca/jfox/

______________________________________________
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel

Reply via email to