Thomas says
I am trying to decipher the manuals for mata to do something that I
imagine is pretty simple, returning my results from mata to (ideally)
matching observations for my starting data in Stata. I basically use
the st_data command to move my stata based observations into a mata
matrix as follows:
I = st_data(., "var 1", "var2", etc.)
I now have a mata based matrix, I, that has all of my data including
identification data such as company number and trading day.
I next use the information variables to produce some new values and
let us assume I create a new matrix in mata called results that has
the following form
Results = (Company Number, Trade Day, Result 1, Result 2, etc.)
where the rows are different observations.
Back in Stata I have variables such as
Company Number, Trade Day, Input Data1, Input Data 2, etc.
My goal is to append the mata based information into new stata
variables with matching observations that would appear as follows:
Company Number, Trade Day, Input Data 1, ...Input Data n, Result 1,
...Result n, etc.
I have struggled in vain to decipher the mata manual for commands such
as creating new stata variables from mata (st_addvar), adding
observations (st_addobs), and modifying or moving values (st_store)
but have had zero luck in getting anything to work. At this point I
am thinking I may simply need to write an output file from mata, close
mata and insheet the output file into stata to append to the dataset
there.
Can anyone provide any thoughts or suggestions on how to proceed?
As I have said in prior posts and in examples (such as that contained
in App B, IMEUS) I find the easiest thing to do is to avoid all of
this st_addvar stuff. My way around it:
(1) Create the set of new variables _in Stata_ with missing values.
(2) Pass their varlist to Mata, and create a view on them. If only
certain observations of the 'other' variables (those with which you
have created st_data from) are usable in the computations, and you
have flagged that with touse, use the same touse to create the view
(essential to get them to line up).
(3) Fill in the view, call it Z, with
Z[.,.] = result of computations
and your Stata variables are updated. It is _essential_ that you say Z
[.,.] = and not Z =. The version 10 manual more prominently mentions
this issue.
Much easier than creating variables, naming them, etc. with the Mata
functions IMHO.
For an example. ssc desc hprescott and viewsource hprescott.ado.
Kit
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
*
* 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/