[R] R object as a function

Sebastian Mueller echsberde at gmx.de
Tue Jan 22 17:27:07 CET 2008


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



More information about the R-help mailing list