[R] passing local parameters to nls?

Gabor Grothendieck ggrothendieck at gmail.com
Wed Jun 9 19:36:13 CEST 2010


It looks in the environment of your formula (which in this case is the
global environment) and then in data.  Thus here are two solutions:

f2 <- function(c) {
	environment(fo) <- environment()
	nls(fo, data=d, start=list(a=1, b=1))
}

f3 <- function(c) nls(fo, data=c(d, c = c), start=list(a=1, b=1))

fo <- y ~ a + x^2 + b * x + c
f2(1)
f3(1)

On Wed, Jun 9, 2010 at 9:39 AM, ivo welch <ivowel at gmail.com> wrote:
> dear R wizards:  environment-related programming question:
>
> myformula = y ~ a*x^2+b*x+c
> d= data.frame( x=rnorm(20), y=rnorm(20) )
>
> cat("\nunconstrained works: \n");
> nls( myformula, data=d, start=list(a=1, b=1, c=1), trace= TRUE)
>
> cat("\nconstrained works: \n");
> b=1; nls( myformula, data=d, start=list(a=1, c=1), trace= TRUE); rm(b)
>
> cat("\nbut how do I pass a local parameter into my formula? \n");
> f = function(c) nls( myformula, data=d, start=list(a=1, b=1), trace= TRUE)
>
> f(c=1);
>
> advice, as always, appreciated.  /iaw
>
> ----
> Ivo Welch (ivo.welch at brown.edu, ivo.welch at gmail.com)
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list