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

From |
Thomas Jacobs <thomasjacobs@gmail.com> |

To |
statalist@hsphsun2.harvard.edu |

Subject |
Re: st: Re: controlling output in the results window |

Date |
Mon, 23 Feb 2009 23:44:05 -0600 |

Kit, Thanks for the reply. I appear to have taken us full circle as originally I was trying to suppress the code displayed on the screen when I call a do file containing mata routines from another do. Basically, I have a dozen event studies for 50 firms and have a master do that defines the periods and firms and a called do that performs one event study on the 50 firms and accumulates the results in mata matrices and then formats the results for output to the screen. As I am looking for some consistency in the target models I spend a lot of time scrolling up and down to look around the repeated mata code snippets that appear between each do call even while I can cover most of the program with a quietly command I was looking for something cleaner that would suppress all the mata code and just give me the results. In any case, colFirm, FirmModelVar and FirmModelResults are mata variables: mata: eventlength = strtoreal(st_local("EventEndDay"))- strtoreal(st_local("EventBeginDay")) + 1 rws = strtoreal(st_local("Count")) colFirm = cols(st_matrix("e(b)")) + 1 //gather model variable names FirmModelVar = strofreal(J(colFirm-1,1,.)) FirmModelVar = st_matrixcolstripe("e(b)")[.,2] colEvent = 2*eventlength + 4 //set to null so will see indicator of missing records during event periods FirmModel = J(rws,colFirm,.) FirmModelResults = J(4,colFirm,.) end I used the definitions in question within my mata function call from a read of page 13 of [M-1] ado under passing arguments to Mata functions which states, [When you think about writing your Mata subroutine, the arguments your ado-file will find convenient to pass and Mata will make convenient use are 1. numbers which Mata calls real scalars such as 2 and 3 (`x' and `y') and 2. names of variables, macros, scalars, matrices, etc., which Mata calls string scalars, such as "mpg" and "__0001dc" ("`varlist'" and "`touse'").] I must just not be getting something big picture here so why don't we leave it at that for now as I cannot find any examples of what I am trying to do - pass mata variables to a mata function. Thanks again for all of your help. Tom On Sat, Feb 21, 2009 at 7:20 AM, Kit Baum <baum@bc.edu> 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/

**References**:**st: Re: controlling output in the results window***From:*Kit Baum <baum@bc.edu>

- Prev by Date:
**Re: st: mata problem "too many string literals"** - Next by Date:
**Re: st: Re: Basic ado question** - Previous by thread:
**st: Re: controlling output in the results window** - Next by thread:
**Re: st: Re: controlling output in the results window** - Index(es):

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