Re: st: Adding output created in Mata to a Stata dataset.

Perhaps http://www.stata.com/statalist/archive/2007-10/msg00591.html
will answer your question.
Steve
sjsamuels@gmail.com
On Mon, Sep 27, 2010 at 9:51 PM, Amy Dunbar
<Amy.Dunbar@business.uconn.edu> wrote:
> My dataset has daily returns for 816 trading days for 1200 companies. To create an equally weighted daily return across this portfolio of firms, I could use bysort date: egen meanret=mean(ret).
> But I want to create a weighted return based on a company characteristic, e.g., size. My ultimate purpose is use
> reg meanret vwretd event1-event11
> where meanret would be the weighted return for each trading day, vwret is the value-weighted market return, and event1-event11 are event days for a market study, where the coefficient on an event date would be the abnormal return. See Sefcik, S.E., and R. Thompson. 1986. An Approach to Statistical Inference in Cross-Sectional Models with Security Abnormal Returns As Dependent Variable. Journal of Accounting Research 24 (2):316-334.
>
> The following do file creates a weighted daily return that varies by date but is the same for each company in the dataset (just as meanret would be the same). I created a small dataset below, where r is one day of returns for 10 companies, and v is a company characteristic. I read Gould. 2006. Mata Matters: Creating new variables -sounds boring, isn't. The Stata Journal 6 (1): 112-123, but I still don't understand how to use st_addvar() and st_store to add the second row of the following Y matrix (created by the do file below) to each observation in the original dataset. I also read Cameron and Trivedi, 2009, Microeconometrics Using Stata, Ch 3, OLS Using Mata, and Appendix B, Mata. The solution is probably there, but I am a mata newbie.
>
> 1
> +----------------+
> 1 | .0076411483 |
> 2 | -.0111004781 |
> +----------------+
> In the dataset below, I want each obs (row) to have -.0111004781 as the weighted return for trading date 1. If I had all 816 trading dates and 1200 companies, Y would be 2 X 816; I want to add the 816 daily weighted return variables to my dataset, which would be the same across the companies.
>
> ___________________________________________________
> * do file creates weighted return for a portfolio of 10 firms
> * create dataset for one trading day, where r is return and v is a firm characteristic.
> input r v
> 0.002 0.2
> 0.001 0.4
> 0.004 0.5
> 0.001 0.4
> 0.009 0.1
> 0.004 0.2
> 0.006 0.3
> 0.007 0.4
> 0.001 0.5
> 0.007 0.1
> end
>
> gen cons = 1
>
> mata
> st_view(R=., ., ("r"))
> st_view(F=., ., ("cons", "v"))
> FFinv=cholinv(cross(F,F))
> W = FFinv * F'
> Y = W*R //create weighted returns
> Y
> end
> ______________________________________
>
> How do I add the weighted return for trading day 1 (-.0111004781) to each observation in my Stata dataset?
> Thank you for considering my request for help.
>
> Amy Dunbar
> University of Connecticut
> School of Business
> Department of Accounting
> cell 860-208-2737
> amy.dunbar@business.uconn.edu
>
>
>
> *
> * 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/
>
*
* 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/