Notice: On March 31, it was **announced** that Statalist is moving from an email list to a **forum**. The old list will shut down at the end of May, and its replacement, **statalist.org** is already up and running.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

From |
Matthew Baker <matthew.baker@hunter.cuny.edu> |

To |
statalist@hsphsun2.harvard.edu |

Subject |
Re: st: passing a function name as an argument to another function in Mata |

Date |
Wed, 24 Aug 2011 10:08:18 -0400 |

I hope it isn't too late to put in a suggestion for this thread, but in any case, another possibility that might do what you want is to use some of the routines in moremata, in particular mm_callf_setup and mm_callf, which allow passing optional arguments along to functions. The only difficulty is I think that you have to use optional arguments, not regular ones. The following is a little example, loosely modeled after the ftof entry in the mata manual. The toy problem confronted by the code is the taking of a numerical derivative of any function with two arguments with respect to just the first argument, so the function, and its second argument, have to be passed as arguments to the numerical-derivative-making function. That is, the mata function fderiv2 created in the code takes a function f, a variable x, and an additional variable A and returns a numerical derivative for some function f(x,A), holding A constant. clear all mata /* Function definition */ real scalar fderiv(f,x) { real scalar ans ans=((*f)(x+1e-6)-(*f)(x))/1e-6 return(ans) } real scalar fderiv2(f,x,|A) { real scalar ans,f1,f2 transmorphic p p=mm_callf_setup(f,args()-2,A) f1=mm_callf(p,x+1e-6) f2=mm_callf(p,x) ans=(f1-f2)/1e-6 return(ans) } real scalar fun(real scalar x,| real scalar A) { real scalar y if (args()==1) y=exp(x)/(1+exp(x)) if (args()==2) y=exp(A*x)/(1+exp(A*x)) return(y) } /* examples of use */ /* manual calculation of derivative and old-fashioned ftof */ x=2 (fun(x+1e-6)-fun(x))/1e-6 fderiv(&fun(),x) /* Derivative now using dummy example where A is also used */ x=2;A=4 (fun(x+1e-6,A)-fun(x,A))/1e-6 /* Derivative, where A is passed along to the function */ fderiv2(&fun(),x,A) end Hope that helps! Matt Baker Dr. Matthew J. Baker Department of Economics Hunter College and the Graduate Center, CUNY On Wed, Aug 24, 2011 at 9:13 AM, Zurab Sajaia <zsajaia@hotmail.com> wrote: > Thanks Kit, > > This will definitely work but it means that I'm calling stata routine from mata (my caller procedure is in mata) that does nothing other that passing arguments back to a mata function...not the most beautiful solution I guess but whatever works :) > > Thanks again, > zurab > > > ---------------------------------------- >> From: kit.baum@bc.edu >> To: statalist@hsphsun2.harvard.edu >> Date: Tue, 23 Aug 2011 20:26:40 -0400 >> Subject: re: st: passing a function name as an argument to another function in Mata >> >> <> >> >> I'm trying to write a function that receives name of another function as an argument and executes it. >> >> >> Section 14.4 of ITSP, Passing a function to a Mata function, discusses that. In the call, >> >> mata: aggreg2("‘varlist’", "‘newvar’", ‘per’, "‘op’", &‘trfn’(), "‘touse’") >> >> where local trfn is the function name (in this case it can handle several, as the user specifies) and then in the header for aggreg2, >> the argument shows up as >> >> pointer(real scalar function) scalar f, >> >> and is invoked within the Mata function as >> >> v1t = (*f)(v1) >> >> in order to transform colvector v1 into v1t. >> >> Note that you cannot pass the name of a builtin function, but you probably don't want to do that anyway. >> >> Kit >> >> Kit Baum | Boston College Economics & DIW Berlin | http://ideas.repec.org/e/pba1.html >> An Introduction to Stata Programming | http://www.stata-press.com/books/isp.html >> An Introduction to Modern Econometrics Using Stata | http://www.stata-press.com/books/imeus.html >> >> >> >> >> * >> * For searches and help try: >> * http://www.stata.com/help.cgi?search >> * http://www.stata.com/support/statalist/faq >> * http://www.ats.ucla.edu/stat/stata/ > * > * For searches and help try: > * http://www.stata.com/help.cgi?search > * http://www.stata.com/support/statalist/faq > * http://www.ats.ucla.edu/stat/stata/ > -- * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/

**References**:**re: st: passing a function name as an argument to another function in Mata***From:*Christopher Baum <kit.baum@bc.edu>

**RE: st: passing a function name as an argument to another function in Mata***From:*Zurab Sajaia <zsajaia@hotmail.com>

- Prev by Date:
**Re: st: basic question** - Next by Date:
**Re: st: multiple imputation and propensity score** - Previous by thread:
**RE: st: passing a function name as an argument to another function in Mata** - Next by thread:
**st: Outreg2 Summary Statistics** - Index(es):