Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# RE: st: Need help on using external global in st_view to avoid recompiling in Mata

 From <[email protected]> To <[email protected]> Subject RE: st: Need help on using external global in st_view to avoid recompiling in Mata Date Fri, 30 Mar 2012 13:25:03 -0400

```Hello Nick,

Many thanks for taking the time to look at my problem.

I have to admit, however, that I am not sure to fully understand what you suggest (sorry).

I am slowly changing the way I do things to address problem in Mata, but it's
not always easy as old (and sometime bad) programming habits are deeply rooted
in me ;o)

For example, I use to write:

st_view(h_0=.,.,("h0"))
st_view(h_20=.,.,("h20"))
st_view(h_40=.,.,("h40"))

st_view(y_0=.,.,("y0"))
st_view(y_20=.,.,("y20"))
st_view(y_40=.,.,("y40"))

U_0  = beta_y:*y_0 :+ beta_h:*h_0
U_20  = beta_y:*y_20 :+ beta_h:*h_20
U_40  = beta_y:*y_40 :+ beta_h:*h_40

sum_eU = exp(U_0) :+ exp(U_20) :+ exp(U_40)

but working with matrices such as

st_view(y_i=.,.,("y0","y20","y40"))
st_view(h_i=.,.,("h0","h20","h40"))

reduces the number of arguments and intermediate step and allow me to work in a
more generic way.  It makes it easier to change the number of labour supply choice and
it also gives me the same results (hopefully not because of luck).

So far, I have compiled a few versions of my function (with different names) where
the only things that vary is the number (and name) of variables that are used in
st_view (e.g. different choices of hours).  Of course, I use different function
on different data set that have been constructed appropriately.  If I understand
correctly, this is the way to do it.

Many thanks,
Jean-François Bertrand

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Nick Cox
Sent: March 30, 2012 11:56 AM
To: [email protected]
Subject: Re: st: Need help on using external global in st_view to avoid recompiling in Mata

I would not think this way. Write your code to take what varies as an input argument.

Nick

On Fri, Mar 30, 2012 at 4:32 PM,  <[email protected]> wrote:

> I have written a Mata function to use with -moptimize- or -ml- to
> estimate a discrete labour supply model.  The simplified example below
> works for data where I would have 3 distinct hours choice (e.g. 0
> hours, 20 hours and 40 hours) with income for each choice.
>
> ----
>
> version 11.2
>
> mata:
>
> function LSmodel_d0(transmorphic scalar M, real scalar todo, real
> rowvector b, real scalar lnf, real rowvector g, real matrix H) {
>        real colvector beta_y, beta_h
>
>        real colvector U
>        real colvector sum_eU
>        real colvector y_chosen
>        real colvector h_chosen
>
>        real colvector L_1
>
>        real matrix y_i
>        real matrix h_i
>
>        beta_y          = moptimize_util_xb(M,b,1)
>        beta_h          = moptimize_util_xb(M,b,2)
>
>        st_view(y=.,.,"y_chosen")
>        st_view(h=.,.,"h_chosen")
>
>        st_view(y_i=.,.,("y0","y20","y40"))
>        st_view(h_i=.,.,("h0","h20","h40"))
>
>        U  = beta_y:*y_chosen :+ beta_h:*h_chosen
>
>        U_i  = beta_y:*y_i :+ beta_h:*h_i
>
>
>        L_1 = (U :- ln(sum_eU))
>
>        lnf = moptimize_util_sum(M,L_1) }
>
> mata mosave mata_LS_singles_quad_d2(), dir(PERSONAL) replace
>
> end
>
> ----
>
> Since I might use different hours choices for different group of the
> population (as done in the economic literature), I would like to avoid
> having to recompile the code if I want to run my estimator on, for
> example, 5 distinct hours choice (e.g. 0 hours, 10 hours, 20 hours, 30 hours and 40 hours) so that:
>
> st_view(y_i=.,.,("y0","y10","y20","y30","y40"))
> st_view(h_i=.,.,("h0","h10","h20","h30","h40"))
>
> I was thinking of defining a global macro that could vary depending on the data I use.
> For example, I could use:
>
> global h_i = `""h0","h20","h40""'
>
> or
>
> global h_i = `""h0","h10","h20","h30","h40""'
>
> depending on the data used.
>
> Is it possible?
>
> My attempts to use pointers and global macro (-findexternal()- or
> -valofexternal()-) have not worked so far.

*
*   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: