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

Date: Thu 03 Mar 2005 - 08:02:23 EST

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()))

## 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))
 "data.frame"
> data.class(with(cars,model.frame(~.+dist+speed)))
> data.class(with(cars,model.frame(~dist+speed)))
 "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))
 "data.frame"
> data.class(model.frame(~.+dist+speed,data=env))
```R. Woodrow Setzer, Jr.                        Phone: (919) 541-0128