[R] nls: 'data' error? (was formula error?)

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue May 23 09:09:23 CEST 2006


>From ?nls

     data: an optional data frame in which to evaluate the variables in
           'formula'.

>From the printout of 'Temp' it appears 'Temp' is a matrix.  Assuming 
'temp' is the same as 'Temp', it is not a data frame as required, and the 
message is consistent with feeding eval() a numeric matrix where a data 
frame is expected.

On Mon, 22 May 2006, H. Paul Benton wrote:

> So thanks for the help,
>
> I have a matrix (AB) which in the first column has my bin numbers so -4 - +4
> in 0.1 bin units. Then I have in the second column the frequency from some
> data. I have plotted them and they look roughly Gaussian. So I want to fit
> them/ find/optimize "mu", "sigma", and "A".
>
> So I call the nls function :
>
> nls_AB <- nls(x ~ (A/sig*sqrt(2*pi))* exp(-1*((x-mu)^2/(2*
> sig^2))),data=temp, start= list(A=0.1, mu=0.01, sig=0.5), trace=TRUE)
> Error in eval(expr, envir, enclos) : numeric 'envir' arg not of length one
>
> Temp looks like this:
>       bin   x
> [1,] -4.0   0
> [2,] -3.9   0
> [3,] -3.8   0
> .........etc
> [41,]  0.0 241
> [42,]  0.1 229
> [43,]  0.2 258
> [44,]  0.3 305
> [45,]  0.4 370
> [46,]  0.5 388
>
> So I don't get my error message. I looked at doing
> class(fo <- (x ~ (A/sig*sqrt(2*pi))* exp(-1*((x-mu)^2/(2* sig^2)))))
> terms(fo)
>
> and that seems to work. So if anyone has any ideas I would welcome them.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list