[R] "nested" getInitial calls; variable scoping problems
Keith Jewell
k.jewell at campden.co.uk
Mon Aug 18 13:50:07 CEST 2008
Hi All,
Another nls related problem (for background, I'm migrating a complicated
modelling package from S-plus to R).
Below I've reduced this to the minimum necessary to demonstrate my problem
(I think); the real situation is more complicated.
Two similar selfStart functions, ssA and ssB.
The 'initial' function for ssB modifies its arguments a little and then
calls getInital for ssA.
In addition to the "x" and the fitted coefficients ("Coeff"), ssA and ssB
have arguments ("A") which are not the same length as the dataframe, so
cannot be passed as columns of that dataframe.
The initial function for ssA uses eval(... parent.frame()) to find "A",
which is fine when called from .GlobalEnv. But when called from the initial
function of ssB it can't find (the modified version of) "A" .
If A is assigned as a parameter of the dataframe then getInitial.formula
returns "A" as initial estimates of "Coeff", so that doesn't work.
I've explored the evaluation frame parent-child structure created by the
nested getInitial calls, but it doesn't seem helpful, and I certainly
couldn't trust my understanding of it.
I'm considering making up the matched call and calling the 'initial'
function of ssA directly, in the same manner as getInitial.selfStart
attr(ssA, "initial"))(mCall = mCall, data = data, LHS = LHS)
or perhaps if I call getInitial thus...
getInitial(ssA, data, mCall, LHS = NULL, ...)
it will call getInitial.selfStart without calling getInitial.formula.
In that case perhaps eval(... parent.frame()) will find the argument?
Or, at least I could attach the argument to the dataframe as a parameter.
But this all seems a but clumsy, and I feel there must be a better way. Any
comments and/or advice will be welcome.
Thanks in advance;
Keith Jewell
-------------------------------------------
code showing the problem:
ssA <- selfStart(
model = function(x, Coeff, A)
{
paste(x, Coeff, A)
},
initial = function(mCall, data, LHS)
{
x <- eval(mCall[["x"]], data, parent.frame())
A <- eval(mCall[["A"]], data, parent.frame())
paste("CoeffA", x, A)
},
parameters = c("Coeff")
)
ssB <- selfStart(
model = function(x, Coeff, A)
{
paste(x, Coeff, A)
},
initial = function(mCall, data, LHS)
{
x <- eval(mCall[["x"]], data, parent.frame())
A <- eval(mCall[["A"]], data, parent.frame())
Amod <- paste(A, "mod in B")
getInitial(y ~ ssA(x, Coeff, Amod), data)
},
parameters = c("Coeff")
)
getInitial(y ~ ssA("this", "that", "other"), data= data.frame(x=c("test")))
getInitial(y ~ ssB("this", "that", "other"), data= data.frame(x=c("test")))
---------------------------
> sessionInfo()
R version 2.7.1 Patched (2008-08-15 r46352)
i386-pc-mingw32
locale:
LC_COLLATE=English_United Kingdom.1252;LC_CTYPE=English_United
Kingdom.1252;LC_MONETARY=English_United
Kingdom.1252;LC_NUMERIC=C;LC_TIME=English_United Kingdom.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
More information about the R-help
mailing list