Statalist


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

Re: st: Returning Mata Results to Stata


From   "Thomas Jacobs" <[email protected]>
To   [email protected]
Subject   Re: st: Returning Mata Results to Stata
Date   Wed, 17 Oct 2007 17:50:37 -0500

Dear Bill,

Thanks so much!  That was VERY helpful in understanding how to use the
Mata-Stata interface commands I had struggled with.  While I already
coded the long re-import into Stata, either of your approaches would
be much prettier and more efficient!  Thanks again.

Sincerely,

Tom

On 10/17/07, William Gould, StataCorp LP <[email protected]> wrote:
> Thomas Jacobs <[email protected]> is using Mata to add new variables
> to his dataset and having difficulty doing it.  Thomas writes
> that in Stata, he has variables
>
>         Company Number, Trade Day, Input Data1, Input Data 2, etc.
>
> and that he has no difficulty accessing that data in Stata, which he
> does by coding
>
>         I = st_data(., "var1", "var2", etc.)
>
> He also has no difficulty producing the new variables he wants and ends
> up with a new matrix in Mata of the form
>
>         Results = (Company Number, Trade Day, Result 1, Result 2, etc.)
>
> where the rows are the observations.
>
> Thomas' difficulty is getting Results back into new Stata variables, and
> he writes,
>
> > 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.
>
> Writing the data to disk and and merging the results back into the
> Stata dataset is certainly one way to proceed, but it will be more
> work than just doing what Thomas originally set out to do:  copy the
> values from Mata matrix Results back into the Stata dataset.
>
> I'll show how, but first I am going to make some assumptions:
>
>      1.  Thomas didn't say whether the number of observations in the
>          Stata dataset are the same as the number of rows in Results,
>          but what he wrote implied it.
>
>      2.  Thomas didn't say whether the results in the Stata dataset
>          and the results in Results are in the same order, but I will
>          assume that they are.
>
> To fix ideas, I will assume that
>
>         Results = (Company_Number, Trade_Day, Result1, Result2, Result3)
>
> I will also assume that Company_Number and Trade_day are in the original
> dataset.  The problem is to add new variables Result1, Result2, and Result3.
>
>
> I'll give two solutions.
>
>
> Solution 1: Stata/Mata approach
> -------------------------------
>
>       At the outset, in Stata, create the three new variables to hold
>       the results:
>
>           . gen Result1 = .
>           . gen Result2 = .
>           . gen Result3 = .
>
>       Now, back in Mata, after calculations of mata matrix Results, code,
>
>           : st_store(., ("Result1", "Result2", "Result3"),
>                         Results[|1,3 \ .,.|] )
>
>      Explanation:  Matrix Results is N x 5, but the first two variables
>      (columns) in the matrix already appear in the data, simply want
>      to add 3, 4, and 5 to the Stata dataset.
>
>      There are lots of other ways I could have coded st_store().  Among them:
>
>           : st_store(., "Result1", Results[3,.])
>           : st_store(., "Result2", Results[4,.])
>           : st_store(., "Result3", Results[5,.])
>
>      or
>
>           : st_store(., ("Result1", "Result2", "Result3"),
>                         Results[(., (3,4,5)]  )
>
>      The way I chose, storing Results[|1,3 \ .,.|], says to pull the
>      submatrix of Results from top-left 1,3 to bottom right number of
>      rows and number of columns.
>
>
> Solution 2: Pure approach
> -------------------------
>
>     The final line of this approach is going to be
>
>           : st_store(., ("Result1", "Result2", "Result3"),
>                         Results[|1,3 \ .,.|] )
>
>     just as it was in Solution 1.  The difference is that, rather than
>     create the variables in Stata,
>
>           . gen Result1 = .
>           . gen Result2 = .
>           . gen Result3 = .
>
>     I'm going to create them in Mata:
>
>           : st_addvar("float", ("Result1", "Result2", "Result3"))
>
>     When I do that, the result will look like this:
>
>           : st_addvar("float", ("Result1", "Result2", "Result3"))
>                   1   2   3
>                +-------------+
>              1 |  3   4   5  |
>                +-------------+
>
>      That's just st_addvar() returning the vector (3,4,5) saying those
>      are the indices of the new variables created.  Actually, rather than
>      coding
>
>           : st_addvar("float", ("Result1", "Result2", "Result3"))
>           : st_store(., ("Result1", "Result2", "Result3"),
>                         Results[|1,3 \ .,.|] )
>
>      I could code
>
>           : newvars = st_addvar("float", ("Result1", "Result2", "Result3"))
>           : st_store(., newvars, Results[|1,3 \ .,.|] )
>
>      The second argument of st_store() specifies the Stata variables to
>      receive the results.  That can be specified as a vector of names,
>      or a vector of variable numbers.
>
> -- 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/
>


-- 
Thomas Jacobs
*
*   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/



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