[R] Using varPower in gnls, an answer of sorts.

Setzer.Woodrow@epamail.epa.gov Setzer.Woodrow at epamail.epa.gov
Wed Mar 2 22:02:23 CET 2005


Back on January 16, a message on R-help from Ravi Varadhan described a
problem with gnls using weights=varPower().  The problem was that the
fit failed with error

Error in eval(expr, envir, enclos) : Object "." not found

I can reliably get this error in version 2.0.1-patched 2004-12-09 on
Windows XP and 2.0.1-Patched 2005-01-26 on Linux.

The key feature of that example is that the data are being passed in the
environment.  Consider a modification of the example in the man page for
gnls:

First, something that should work:

> gnls(weight ~ Asym/(1 + exp((xmid - Time)/scal)),data=Soybean,
+ start=c(Asym=16,xmid=50,scal=7),weights=varPower())
Generalized nonlinear least squares fit
  Model: weight ~ Asym/(1 + exp((xmid - Time)/scal))
  Data: Soybean
  Log-likelihood: -486.8973

Coefficients:
    Asym     xmid     scal
17.35681 51.87230  7.62052

Variance function:
 Structure: Power of variance covariate
 Formula: ~fitted(.)
 Parameter estimates:
    power
0.8815438
Degrees of freedom: 412 total; 409 residual
Residual standard error: 0.3662752

## Now, use with() to pass Soybean in the environment of gnls:

> with(Soybean,gnls(weight ~ Asym/(1 + exp((xmid - Time)/scal)),
+ start=c(Asym=16,xmid=50,scal=7),weights=varPower()))

Error in eval(expr, envir, enclos) : Object "." not found

## drop the weights argument from gnls, and the error message goes away.

The problem is in a call to model.frame.  When varPower() (and
presumably other weight functions using '.' to represent a fitted model)
is used, gnls() constructs a formula argument for model.matrix that
looks like:
~.+weight+Time
This works when the data are passed in a data frame, but not when they
are in the environment.  Look at the example in the man page for
model.frame

> data.class(model.frame(~.+dist+speed, data=cars))
[1] "data.frame"
> data.class(with(cars,model.frame(~.+dist+speed)))
Error in eval(expr, envir, enclos) : Object "." not found
> data.class(with(cars,model.frame(~dist+speed)))
[1] "data.frame"
> env <- new.env(TRUE,NULL)
> assign("dist",cars$dist,envir=env)
> assign("speed",cars$speed,envir=env)
> data.class(model.frame(~dist+speed,data=env))
[1] "data.frame"
> data.class(model.frame(~.+dist+speed,data=env))
Error in eval(expr, envir, enclos) : Object "." not found

I'm sending this to r-help rather to the authors of nlme because I am
not sure where the bug is: is model.frame misbehaving, or should gnls
not include '.' in its call to model.frame?

R. Woodrow Setzer, Jr.                        Phone: (919) 541-0128
Experimental Toxicology Division             Fax:  (919) 541-4284
Pharmacokinetics Branch
NHEERL B143-01; US EPA; RTP, NC 27711




More information about the R-help mailing list