[Rd] stats::getInitial: requires the model to live in the stats namespace or above

Ivan Krylov kry|ov@r00t @end|ng |rom gm@||@com
Wed Jun 22 17:25:02 CEST 2022


Hello R-devel,

Here's a corner case I've stumbled upon recently:

local({
	# Originally this was a package namespace, but a local
	# environment also leads to failure
	stopifnot(!identical(environment(), globalenv()))

	# Make a self-starting model inside this private environment...
	SSlinear <- selfStart(
		~ a * x + b,
		function(mCall, data, LHS, ...) {
			xy <- sortedXyData(mCall[['x']], LHS, data)
			setNames(
				coef(lm(y ~ x, xy)),
				mCall[c('b', 'a')]
			)
		},
		c('a', 'b')
	)

	# ...and try to use it
	x <- 1:100
	y <- 100 + 5 * x + rnorm(length(x), sd = 10)
	nls(y ~ SSlinear(x, a, b))
	# error in get('SSlinear'): object not found
})

As a workaround, I'll just provide the starting values manually,
but should this work?

As implemented [1], getInitial requires the model object to live in the
stats package namespace or any of its parents, which eventually include
the global environment and the attached packages, but not the private
namespaces of the packages or any other local environments. This
results from the fact that getInitial() uses plain get() in order to
resolve the symbol for the self-starting model, and get() defaults to
the current environment, which leads a chain of stats -> imports:stats
-> base -> global environment -> attached packages.

It seems easy to suggest get(., envir = environment(object)) as a fix,
which would be able to access anything available at the time of
creation of the formula. On the other hand, it would break the case
when the stats package is not attached to the global environment or the
formula environment, which currently works.

-- 
Best regards,
Ivan

[1]
https://github.com/r-devel/r-svn/blob/d43497cbc927e632c6f597fa23001c3f31d4cae6/src/library/stats/R/selfStart.R#L81-L87



More information about the R-devel mailing list