Re: [R] passing local parameters to nls?

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Wed, 09 Jun 2010 13:36:13 -0400

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.
>



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. Received on Wed 09 Jun 2010 - 17:38:48 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 09 Jun 2010 - 17:40:28 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive