# [R] Non-linear Regression : Error in eval(expr, envir, enclos)

Sundar Dorai-Raj sundar.dorai-raj at pdf.com
Sat Mar 11 14:20:20 CET 2006

```
Cal Stats wrote:
> Hi..
>
>       i have an expression of the form:
>
>   model1<-nls(y~beta1*(x1+(k1*x2)+(k1*k1*x3)+(k2*x4)+(k2*k1*x5)+(k2*k2*x6)+(k3*x7)+(k3*k4*x8)+(k3*k2*x9)+(k3*k3*x10)+          (k4*x11)+(k4*k1*x12)+(k4*k2*x13)+(k4*k3*x14)+(k4*k4*x15)+(k5*x16)+(k5*k1*x17)+(k5*k2*x18)+(k5*k3*x19)+             (k5*k4*x20)+(k5*k5*x21)+(k6*x22)+(k6*k1*x23)+(k6*k2*x24)+(k6*k3*x25)+(k6*k4*x26)+(k6*k5*x27)+(k6*k6*x28)+             (k7*x29)+(k7*k1*x30)+(k7*k2*x31)+(k7*k3*x32)+(k7*k4*x33)+(k7*k5*x34)+(k7*k6*x35)+(k7*k7*x36)),x,             start=c(beta1=-0.001480981,k1=0.001,k2=0.001,k3=0.001,k4=0.001,k5=0.001,k6=0.001,k7=0.001),control=c(maxiter=100))
>
>   where beta1, k1 -- k7 are the parameters to be estimated and
>   x1---x36 are the columns of the dataframe x
>
>   when i run this expression I get the error
>
>   Error in eval(expr, envir, enclos) : numeric 'envir' arg not of length one
>
>   I searched  a couple of previous posting on this error but none of them addressed it in detail
>
>   Thanks
>
>   Harsh
>
>
>
>
> ---------------------------------
>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help

Hi, Harsh,

Please provide a working example with real or random data as the posting
guide tells you. That should not be difficult for this problem. I just
did it in under 10 minutes:

set.seed(42)
b <- c(beta1 = -0.001480981, k1 = 0.001, k2 = 0.001, k3 = 0.001,
k4 = 0.001, k5 = 0.001, k6 = 0.001, k7 = 0.001)
z <- as.data.frame(lapply(1:36, function(x) runif(100)))
names(z) <- sprintf("x%d", 1:36)
z\$y <- with(z, b["beta1"] *
(x1 + b["k1"] * x2 + b["k1"] * b["k1"] * x3 + b["k2"] * x4 +
b["k2"] * b["k1"] * x5 + b["k2"] * b["k2"] * x6 + b["k3"]
* x7 +
b["k3"] * b["k4"] * x8 + b["k3"] * b["k2"] * x9 + b["k3"]
* b["k3"] * x10 +
b["k4"] * x11 + b["k4"] * b["k1"] * x12 + b["k4"] *
b["k2"] * x13 +
b["k4"] * b["k3"] * x14 + b["k4"] * b["k4"] * x15 +
b["k5"] * x16 +
b["k5"] * b["k1"] * x17 + b["k5"] * b["k2"] * x18 +
b["k5"] * b["k3"] * x19 +
b["k5"] * b["k4"] * x20 + b["k5"] * b["k5"] * x21 +
b["k6"] * x22 +
b["k6"] * b["k1"] * x23 + b["k6"] * b["k2"] * x24 +
b["k6"] * b["k3"] * x25 +
b["k6"] * b["k4"] * x26 + b["k6"] * b["k5"] * x27 +
b["k6"] * b["k6"] * x28 +
b["k7"] * x29 + b["k7"] * b["k1"] * x30 + b["k7"] *
b["k2"] * x31 +
b["k7"] * b["k3"] * x32 + b["k7"] * b["k4"] * x33 +
b["k7"] * b["k5"] * x34 +
b["k7"] * b["k6"] * x35 + b["k7"] * b["k7"] * x36) +
rnorm(100, 0, .1))
model1 <- nls(y ~ beta1 *
(x1 + k1 * x2 + k1 * k2 * x3 + k2 * x4 +
k2 * k1 * x5 + k2 * k2 * x6 + k3 * x7 +
k3 * k4 * x8 + k3 * k2 * x9 + k3 * k3 * x10 +
k4 * x11 + k4 * k1 * x12 + k4 * k2 * x13 +
k4 * k3 * x14 + k4 * k4 * x15 + k5 * x16 +
k5 * k1 * x17 + k5 * k2 * x18 + k5 * k3 * x19 +
k5 * k4 * x20 + k5 * k5 * x21 + k6 * x22 +
k6 * k1 * x23 + k6 * k2 * x24 + k6 * k3 * x25 +
k6 * k4 * x26 + k6 * k5 * x27 + k6 * k6 * x28 +
k7 * x29 + k7 * k1 * x30 + k7 * k2 * x31 +
k7 * k3 * x32 + k7 * k4 * x33 + k7 * k5 * x34 +
k7 * k6 * x35 + k7 * k7 * x36),
data = z, start = b, control = c(maxiter = 100))

This call works without a hitch so I cannot replicate your error. In