I have a question about passing arguments to the function f that nlm
minimizes.
I have no problems if I do this:
x<-seq(0,1,.1)
y<-1.1*x + (1-1.1) + rnorm(length(x),0,.1)
fn<-function(p)
{
yhat<-p*x+(1-p)
sum((y-yhat)^2)
}
out<-nlm(fn,p=1.5,hessian=TRUE)
But I would like to define
fn<-function(x,y,p)
{
yhat<-p*x+(1-p)
sum((y-yhat)^2)
}
so that I can pass the x and y variables along, doing something like
out<-nlm(fn,x=this, y=that,p=1.5,hessian=TRUE)
This version doesn't work of course. I have looked at the examples and
can't figure out how to do what I want. Is it possible?
Thanks very much for any help.
(BTW I know nls() can be used for nonlinear least squares fitting. It
choked on this problem.)
Bill Simpson
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at
stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Sorry for the typo--I meant nlm in subject line not nls Bill Simpson -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
I think you need to make your parameter/parameter vector the *first*
argument to your function. That way the "..." gets interpreted
correctly. e.g.:
fn2<-function(p,x,y)
{
yhat<-p*x+(1-p)
sum((y-yhat)^2)
}
out <- nlm(fn2,p=1.5,x=x, y=y,hessian=TRUE)
On Fri, 27 Apr 2001, Bill Simpson wrote:
> I have a question about passing arguments to the function f that nlm
> minimizes.
>
> I have no problems if I do this:
> x<-seq(0,1,.1)
> y<-1.1*x + (1-1.1) + rnorm(length(x),0,.1)
> fn<-function(p)
> {
> yhat<-p*x+(1-p)
> sum((y-yhat)^2)
> }
> out<-nlm(fn,p=1.5,hessian=TRUE)
>
> But I would like to define
> fn<-function(x,y,p)
> {
> yhat<-p*x+(1-p)
> sum((y-yhat)^2)
> }
> so that I can pass the x and y variables along, doing something like
> out<-nlm(fn,x=this, y=that,p=1.5,hessian=TRUE)
> This version doesn't work of course. I have looked at the examples and
> can't figure out how to do what I want. Is it possible?
>
> Thanks very much for any help.
>
> (BTW I know nls() can be used for nonlinear least squares fitting. It
> choked on this problem.)
>
> Bill Simpson
>
>
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !) To: r-help-request at
stat.math.ethz.ch
>
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>
--
318 Carr Hall bolker at zoo.ufl.edu
Zoology Department, University of Florida http://www.zoo.ufl.edu/bolker
Box 118525 (ph) 352-392-5697
Gainesville, FL 32611-8525 (fax) 352-392-3704
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at
stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Fri, 27 Apr 2001, Bill Simpson wrote:
> I have a question about passing arguments to the function f that nlm
> minimizes.
>
> I have no problems if I do this:
> x<-seq(0,1,.1)
> y<-1.1*x + (1-1.1) + rnorm(length(x),0,.1)
> fn<-function(p)
> {
> yhat<-p*x+(1-p)
> sum((y-yhat)^2)
> }
> out<-nlm(fn,p=1.5,hessian=TRUE)
>
> But I would like to define
> fn<-function(x,y,p)
> {
> yhat<-p*x+(1-p)
> sum((y-yhat)^2)
> }
> so that I can pass the x and y variables along, doing something like
> out<-nlm(fn,x=this, y=that,p=1.5,hessian=TRUE)
> This version doesn't work of course. I have looked at the examples
and
> can't figure out how to do what I want. Is it possible?
>
> Thanks very much for any help.
>
> (BTW I know nls() can be used for nonlinear least squares fitting. It
> choked on this problem.)
>
> Bill Simpson
>
>
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read
http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !) To: r-help-request at
stat.math.ethz.ch
>
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>
I had to do similar things within a function and this is how I do it:
task <- function(...)
{
...
y <- ...
p <- ...
fn <- function(x) {
yhat<-p*x+(1-p)
assign("y",y+1,inherit=TRUE)
sum((y-yhat)^2)
}
nlm(fn,x)
...
}
When R needs y and p within fn, it'll trace through the nested
environments to find them, and since they are not within nlm, the y and p
in task gets used; they can also be provided through the arguments of
task.
The assign(...) line allows me to update y from within fn. My
particular application involves a certain Newton iteration within fn,
and I have to update the starting value for that Newton iteration.
Hope this helps.
Chong
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at
stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._