[R] nls() error: Object not found
Douglas Bates
bates at stat.wisc.edu
Tue Nov 30 23:53:50 CET 2004
Yang, Richard wrote:
> Dear R-helpers;
>
> Using nls() to fit a function,Rdum, defined below I stumbled on an
> error: "Error in eval(expr, envir, enclos): Object "s0" not found."
>
> The function Rdum is defined as
>
> Rdum <- deriv(~ h1 * (s0 + sl0*sl + sm0*sm + sp01*sp1 + sp02*sp2 +
> sp03*sp3+sp04*sp4) *
> ((1 - exp(-(s1 + sl1*sl + sm1*sm + sp11*sp1 + sp12*sp2 +
> sp14*sp4)*t2)) /
> (1 - exp(-(s1 + sl1*sl + sm1*sm + sp11*sp1 + sp12*sp2 +
> sp14*sp4)*t1)))^
> (s2 + sl2*sl+sm2*sm+ sp21*sp1+sp22*sp2+sp23*sp3+sp24*sp4),
> c("s0", "s10","sm0", "sp01","sp02","sp03","sp04",
> "s1", "sl1","sm1", "sp11", "sp12","sp14",
> "s2","sl2","sm2", "sp21","sp22","sp23","sp24"),
> function(s0,sl0,sm0,sp01,sp02,sp03,sp04, s1, sl1, sm1, sp11,sp12, sp14,
>
> s2, sl2, sm2, sp21,sp22, sp23, sp24, h1, t1,t2, sl, sm,sp1, sp2, sp3,
> sp4){})
>
> and variables t1, t2, h1, h2, sl, sm, sp1, sp2, sp3, and sp4 in the gbht10D
> data frame are fitted to the function by
>
> Dum.nls <- nls(h2 ~ Rdum(s0, sl0, sm0, sp01, sp02, sp03, sp04, sl,
> sl1,sm1,sp11,sp12, sp14,
> s2, sl2, sm2, sp21, sp22, sp23, sp24,h1, t1, t2, sl, sm, sp1, sp2, sp3,
> sp4), data=gbht10D,
> start=c(0.8413,-0.1602,-0.0156,0.0527,0.0513,0.00314,0.0378,
> -0.00598,-0.0125,0.00328,0.00989,0.0113,0.00583,
> 1.9255,0.9427,0.2408,-0.0643,0.0047,0.0016,-0.00519))
>
> The complained object "s0" is one of the 20 parameters in the function
> to be estimated from the dataset gbht10D.
>
> The nls() script is relatively short. Stepping into the source code, I
> located the offending line:
>
> Browse[1]> start
> [1] 0.84130 -0.16020 -0.01560 0.05270 0.05130 0.00314 0.03780 -0.00598
> -0.01250
> [10] 0.00328 0.00989 0.01130 0.00583 1.92550 0.94270 0.24080 -0.06430
> 0.00470
> [19] 0.00160 -0.00519
> Browse[1]> n
> debug: pnames <- names(start)
> Browse[1]> n
> debug: varNames <- varNames[is.na(match(varNames, pnames, nomatch = NA))]
> Browse[1]> n
> debug: varIndex <- sapply(varNames, function(varName, data, respLength) {
> length(eval(as.name(varName), data))%%respLength == 0
> }, data, length(eval(formula[[2]], data)))
> Browse[1]> varNames
> [1] "h2" "s0" "sl0" "sm0" "sp01" "sp02" "sp03" "sp04" "sl" "sl1"
> "sm1" "sp11"
> [13] "sp12" "sp14" "s2" "sl2" "sm2" "sp21" "sp22" "sp23" "sp24" "h1"
> "t1" "t2"
> [25] "sm" "sp1" "sp2" "sp3" "sp4"
> Browse[1]> n
> Error in eval(expr, envir, enclos) : Object "s0" not found.
>
> The "s0" is shown in varNames. Is the error message misleading? Or did I
> miss something?
The part of the manual page that states that start must be a named
vector?
start: a named list or named numeric vector of starting estimates
The piece of code you are examining matches the starting values against
the parameter names. Because you did not name the starting values it
cannot find a match for "s0".
More information about the R-help
mailing list