[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

From |
Partha Deb <[email protected]> |

To |
[email protected] |

Subject |
Re: st: Macro substitution in Mata |

Date |
Mon, 26 Dec 2005 11:50:38 -0500 |

All,

Thanks to Bill and Kit for their suggestions. I (he :) ) have been learning about pointers, but have realized I have a bigger issue. I needed some form of a loop, I thought, because I'm trying to write an ML routine for which the number of equations is a function of the data (e.g., mlogit) and for which the lf and gradients are calculated in Mata. Below is a description of how the code is structured. TIA for your help.

e.g. (with dimension=2)

program mmlogit_lf

version 9

args todo b lnf g negH g1 g2

...

mata: mmlogit_lf("`lnL'","`g1'","`g2'",...)

...

end

mata:

function mmlogit_lf(string scalar lnL, string scalar g1, string scalar g2, ... )

{

...

code for the lf - vector vlnL

code for g's - matrix vg with gradients in columns

...

gnames = (g1, g2)

st_store(., lnL, vlnL)

st_store(., gnames, vg)

}

end

This code works fine for the dimension=2 case (or any other dimension that I specify in advance). I have also written a macro so that the "g1 g2" in the args statement is generalized. I am not sure one can specify the arguments of the mata function and the call to the mata function using macros. If one can, I'm having trouble doing it. If not, how can I return the required objects?

--On Monday, December 26, 2005 8:31 AM -0600 "William Gould, Stata" <[email protected]> wrote:

Partha Deb <[email protected]> writes,I'm trying to create a number of matrices in Mata ordered, e.g., x1, x2, ..., x7. I'd like to set up a loop to create these. If these were Stata objects, I could do them quite easily, e.g., I might write something like forvalues i=1/7 { gen x`i' = `i' } but I can't seem to figure out how to do something similar within Mata.Exactly right. My first recommendation is to use subroutines and simply call seven times: mat_init(x1) mat_init(x2) ... mat_init(x7) If Partha is absolutely determined to work in a loop, then she is going to have to learn about pointers. The setup would be p = J(7, 1, NULL) p[1] = &x1 p[2] = &x2 ... p[7] = &x7 and then Partha could code things like for (i=1; i<=7; i++) { mat_init(*p[i]) } or for (i=1; i<=7; i++) { *p[i] = J(4,4,0) for (j=1; j<=4; j++) (*p[i])[j] = 1 } In terms of initialization of p[], unless it was important to be able to refer to them by the names x1, x2, ..., x7, one could simply the setup to p = J(7, 1, NULL) for (i=1; i<=7; i++) p[i] = &J(0,0,.) There are some problems for which pointers are wonderful solutions. In a problem like this one, however, my guess is that pointers are not worth the effort. -- Bill [email protected] * * For searches and help try: * http://www.stata.com/support/faqs/res/findit.html * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/

************************************************************************ Partha Deb ph: (212) 772-5435 Department of Economics fax: (212) 772-5398 Hunter College http://urban.hunter.cuny.edu/~deb/ Emancipate yourselves from mental slavery None but ourselves can free our minds. - Bob Marley ************************************************************************ * * For searches and help try: * http://www.stata.com/support/faqs/res/findit.html * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/

**References**:**Re: st: Macro substitution in Mata***From:*[email protected] (William Gould, Stata)

- Prev by Date:
**[no subject]** - Next by Date:
**st: R-Squared in Panel data (RE vs. FE)** - Previous by thread:
**Re: st: Macro substitution in Mata** - Next by thread:
**Re: st: Macro substitution in Mata** - Index(es):

© Copyright 1996–2024 StataCorp LLC | Terms of use | Privacy | Contact us | What's new | Site index |