Well call me pig-headed but I just could not let go of this until I
figured out how to get it working. In the end it required either
converting mata values(matrices) to stata locals(matrices) so they
could be passed to the mata function. I post it here not just for
posterity but for anyone else trying to pass a stata matrix to a mata
function within an ado file. Hopefully it will save someone the many
hours I spent trying to get it to work. Both eofb and
sFirmModelResults are stata matrices. The latter was created from a
mata matrix FirmModelResults using
st_replacematrix("sFirmModelResults",FirmModelResults)
and the ado follows:
program ProduceResults
version 10.1
local EventBeginDay "`1'"
local EventEndDay "`2'"
local scolFirm "`3'"
mata: OutputFn("eofb","sFirmModelResults")
end
version 10.1
mata:
void OutputFn(eofb,sFirmModelResults)
{
printf("Results for Event Period beginning with trading day %4.0f and
ending at day %4.0f.\n",
strtoreal(st_local("EventBeginDay")), strtoreal(st_local("EventEndDay")))
colFirm = strtoreal(st_local("scolFirm"))
FirmModelVar = st_matrixcolstripe("e(b)")[.,2]
printf("Average Firm Adjusted R Square %12.4f\n",
st_matrix(sFirmModelResults)[1,strtoreal(st_local("scolFirm"))])
printf("\n")
printf("{txt}Average Firm Model{c |} Coef SampSE
t-stat p-val\n")
printf("{hline 18}{c +}{hline 52}\n")
for (mi=1; mi<= colFirm-1; mi++) {
printf("{txt} %13s {c |}", FirmModelVar[mi])
for (mj=1; mj <=4; mj++) {
printf("{res} %9.4f",st_matrix(sFirmModelResults)[mj,mi])
}
printf("\n")
}
}
end
On Sat, Feb 21, 2009 at 7:20 AM, Kit Baum <[email protected]> wrote:
> <>
> First of all do not compile your function into a .mo. (I know, my slides
> show how to do that, but it confuses the issue here). Put all of the Mata
> code in the do-file that you're running. You need not make an ado-file out
> of it.
>
> The error at the execution of your Mata function presumably results from
> "ColFirm" being something other than a numeric scalar. That is what you have
> told Mata to expect. In hprescott.ado I pass a number, stored in local macro
> smooth, to the Mata routine. Note how it is `smooth', not "`smooth'".
>
> Your function is looking for a matrix FirmModelResults, but you are
> declaring it as a string scalar. If you generate a matrix in Stata and want
> to use it in Mata, the easiest way is via st_matrix(). Likewise FirmModelVar
> is a matrix, so should be transferred as such. The reason why hprescott uses
> string scalars is that it has to exchange lists of variable names with
> Stata. The logic would be quite different if it was exchanging matrices with
> Stata.
>
> Kit Baum, Boston College Economics and DIW Berlin
> http://ideas.repec.org/e/pba1.html
> An Introduction to Modern Econometrics Using Stata:
> http://www.stata-press.com/books/imeus.html
>
>
> On Feb 21, 2009, at 02:33 , Tom wrote:
>
>> I am back again, having studied your slides. I have some conceptual
>> questions. I was unable to convert my called do file to an ado nor
>> understand several of the posts people submitted in response to my
>> inquiries on the conversion. So, having read your slides I took the
>> section of my called do file that I was unable to silence with the
>> quietly command and attempted to make it a mata function, but I have
>> been unable to get that to work, either. One thing I have noticed is
>> that the Stata manuals in M-1 ado - using Mata with ado files and your
>> slides always seem to imply that a mata function is called from a
>> stata program passing stata variables. In my case, I have a stata
>> program with significant sections of mata code and mata variables
>> created prior to the called mata function, so I am left wondering how
>> does one pass mata variables to a mata function? If this is not
>> permitted, does this mean I need to broaden my mata function to
>> include all the mata code in the stata program and pass stata
>> variables as in the examples?
>>
>> Here is the code section I am trying to convert to a mata function
>> right below the end of the quietly command where colFirm,
>> FirmModelResults, and FirmModelVar are mata variables created earlier:
>>
>> *end quietly
>> }
>>
>> mata:
>>
>> //Used one pass loop to clean up output
>> for (mh=1; mh<=1;mh++) {
>>
>> printf("Results for Event Period beginning with trading day %4.0f
>> and
>> ending at day %4.0f\n",
>> strtoreal(st_local("EventBeginDay")),
>> strtoreal(st_local("EventEndDay")))
>> printf("Average Firm Adjusted R Square
>> %12.4f\n",FirmModelResults[1,colFirm])
>> printf("\n")
>> printf("{txt}Average Firm Model{c |} Coef SampSE
>> t-stat p-val\n")
>> printf("{hline 18}{c +}{hline 52}\n")
>>
>> for (mi=1; mi<= colFirm-1; mi++) {
>>
>> printf("{txt} %13s {c |}", FirmModelVar[mi])
>>
>> for (mj=1; mj <=4; mj++) {
>>
>> printf("{res} %9.4f",FirmModelResults[mj,mi])
>>
>> }
>>
>> printf("\n")
>>
>> }
>>
>> }
>>
>> end
>>
>> Here is the do file I use to create the function (as a side question,
>> every time I run this the replace option fails giving me an error that
>> the .mo already exists and I have to manually delete it and restart
>> stata. I have posted this to tech support to see if this is a bug but
>> thought I would mention in case you see something I am doing wrong):
>>
>> *PooledResultsMo.do
>> version 10.1
>> mata:
>> void PooledResultsMo(real scalar colFirm, string scalar
>> FirmModelResults, string scalar FirmModelVar)
>> {
>> printf("Results for Event Period beginning with trading day %4.0f
>> and
>> ending at day %4.0f\n",
>> strtoreal(st_local("EventBeginDay")),
>> strtoreal(st_local("EventEndDay")))
>> printf("Average Firm Adjusted R Square
>> %12.4f\n",FirmModelResults[1,colFirm])
>> printf("\n")
>> printf("{txt}Average Firm Model{c |} Coef SampSE
>> t-stat p-val\n")
>> printf("{hline 18}{c +}{hline 52}\n")
>>
>> for (mi=1; mi<= colFirm-1; mi++) {
>>
>> printf("{txt} %13s {c |}", FirmModelVar[mi])
>>
>> for (mj=1; mj <=4; mj++) {
>>
>> printf("{res} %9.4f",FirmModelResults[mj,mi])
>>
>> }
>>
>> printf("\n")
>>
>> }
>> }
>>
>> mata mosave PooledResultsMo(), dir(PERSONAL) replace
>> end
>>
>> And here is the error I get when I try to execute the entire program
>> where a do file calls a do file which calls PooledResultsMo:
>>
>>
>> . *end quietly
>> . }
>> . mata:PooledResultsMo("`colFirm'", "`FirmModel'", "`FirmModelVar'" )
>> PooledResultsMo(): 3253 <tmp>[1,1] found where real required
>> <istmt>: - function returned error
>> r(3253);
>
> *
> * 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/
>
--
Thomas Jacobs
*
* 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/