[R] nls problem
John C Nash
nashjc at uottawa.ca
Wed Jul 4 15:55:07 CEST 2012
By interpreting the code line by line and looking at the output of the lines, I got the
following result. It looks like it needs the fifu converted to an expression, then
evaluated. This suggests a workaround, but doesn't answer the underlying question about
whether this is supposed to work this way.
JN
> str(fifu)
language exp(-k * x)
> fifu2<-as.expression(fifu)
> fit2b <- nls(y ~ fifu2, data = data, start = c(k = 1))
Error in lhs - rhs : non-numeric argument to binary operator
> fifu2
expression(exp(-k * x))
> fit2be <- nls(y ~ eval(fifu2), data = data, start = c(k = 1))
> fit2be
Nonlinear regression model
model: y ~ eval(fifu2)
data: data
k
1
residual sum-of-squares: 1.604e-06
Number of iterations to convergence: 1
Achieved convergence tolerance: 1.603e-06
--------------------------------------
>
> Message: 13
> Date: Tue, 03 Jul 2012 13:54:11 +0200
> From: "joerg van den hoff" <j.van_den_hoff at hzdr.de>
> To: r-help at r-project.org
> Subject: [R] nls problem
> Message-ID: <op.wgvcolzs24ol6l at marco.fz-rossendorf.de>
> Content-Type: text/plain; charset=iso-8859-15; format=flowed;
> delsp=yes
>
> hi list,
>
> used versions: 2.12.1 and 2.14.0 under ubuntu and macosx.
>
> I recently stumbled over a problem with `nls', which occurs if the model
> is not specified explicitly but via an evaluation of a 'call' object.
> simple example:
>
> 8<--------------------------------------------------------------------------------------
>
> nlsProblem <- function (len = 5) {
> #=======================================================================
> # purpose: to demonstrate an apparent problem with `nls' which occurs,
> # if the model is specified by passing th lhs as an evaled 'call'
> # object. The problem is related to the way `nls' tries to compute
> # its internal variable `varIndex' which rests on the assumption that
> # the dependent ("y") and, possibly, the independent ("x") variable
> # are identified by having a length equal to the `nls' variable
> # `respLength'. the problem arises when there are `varNames'
> # components accidentally having this length, too.
>
> # in the present example, setting the number of data points to
> # len=2 triggers the error since the `call' object `fifu' has this
> # length, too and `nls' constructs an erroneous `mf$formula' internally.
> #=======================================================================
> #generate some data
> x <- seq(0, 4, len = len)
> y <- exp(-x)
> y <- rnorm(y, y, .001*y)
> data <- list(x = x, y = y)
>
> #define suitable model
> model <- y ~ exp(-k*x)
> fifu <- model[[3]]
>
This is where my output above should be placed. JN
> #this fit is fine:
> fit1 <- nls(model, data = data, start = c(k = 1))
> print(summary(fit1))
>
> #this fit crashes `nls' if len = 2:
> fit2 <- nls(y ~ eval(fifu), data = data, start = c(k = 1))
> print(summary(fit2))
> }
>
> 8<--------------------------------------------------------------------------------------
>
> to see the problem call `nlsProblem(2)'.
>
> as explained in the above comments in the example function, I tracked it
> down to the way
> `nls' identifies x and y in the model expression. the problem surfaces in
> the line
>
> varIndex <- n%%respLength == 0
>
> (line 70 in the function listing from within R) which, in the case of
> `fit2' in the above
> example always returns a single TRUE index as long as `len != 2' (which
> seems fine for the
> further processing) but returns a TRUE value for the index of `fifu' as
> well if `len == 2'.
>
> question1: I'm rather sure it worked about 6 months ago with (ca.) 2.11.x
> under ubuntu. have there been changes in this area?
> question2: is something like the `fit2' line in the example expected to
> work or not?
> qeustion3: if it is not expected to work, should not the manpage include a
> corresponding caveat?
> question4: is there a a substitute/workaround for the `fit2' line which
> still allows to specify the rhs of the model via a variable instead of a
> constant (explicit) expression or function call?
>
> the above example is of course construed but in my use case I actually
> need this sort of thing. is there any chance that
> the way `nls' analyzes its `model' argument can be changed to parse the
> `eval(fifu)' construct correctly in all cases?
>
> since I'm currently not subscribed to the list I'd appreciate if responses
> could be Cc'ed to me directly.
>
> thanks in advance,
>
> joerg
>
>
More information about the R-help
mailing list