Re: [R] R object as a function

From: Sebastian Mueller <echsberde_at_gmx.de>
Date: Tue, 22 Jan 2008 17:27:07 +0100

On Tuesday 22 January 2008 13:52:29 Thomas Steiner wrote:
> Thank you very much Duncan for your quick answers.
>
> > You're not passing a function as myfunk1, you're passing mf, which is
> > the result of evaluating myfun1, so it's a numeric vector.
>
> Yes, this is exacty my problem.
> If I leave it away, the problem will not be resolved (it needs pa or not)
>
> myfun1<-function(x,pa) {
> return(pa[1]*x^2+pa[2]*x+pa[3])
> }
> myfun2<-function(x,param,myfunk1) {
> return(param[1]*myfunk1(x)+param[2]*myfunk1(x))
> }
> test<-function(pars1,pars2,lo,up){
>
> integ=integrate(f=myfun2,lower=lo,upper=up,param=pars2,myfunk1=myfun1)#pa=p
>ars1 return( 2*integ$value )
> }
> test(pars1=c(1,2,3),pars2=c(-1,1),lo=2,up=7)
>
> Which gives an error:
> Once the "argument pa" is missing and if you add the "pa=pars1" in
> the comment, it says that the argument pa is redundant.
>
If I understood your problem right (i don't know haw integrate() works) the solution could be as follows

myfun1<-function(x=5,pa) {
  return(function(x){pa[1]*x^2+pa[2]*x+pa[3]}) }

It's a substitution for your myfun1 and it gives back a closure (function with bindings) instead of a numeric.
Hope it helps

Sebastian



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 Tue 22 Jan 2008 - 16:28:56 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 Tue 22 Jan 2008 - 16:30:10 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