Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# st: Estimating multiple regression on panel dataset by year, saving and testing results.

 From Chipengovich Shpeng To "statalist@hsphsun2.harvard.edu" Subject st: Estimating multiple regression on panel dataset by year, saving and testing results. Date Sun, 13 May 2012 17:47:33 +0400

```Thanks a lot. Probably I made my question in a clumsy form. So that I've also asked - how to itroduce year analysis?
I could bring everything fine I guess it'd look like, but it doesnt work

---------------------------------------
forval j = 1/100 {
forval year = 2001(1)2010 {
qui reg var`j' mcsi
di "`j' year {col 10}Beta: " %9.4f  _b[mcsi]  "  Rsq: " %9.4f   e(r2)
}

or

forval j = 1/100 {
sort year
by year:  qui reg var`j' mcsi
di "`j' year(t) {col 10}Beta: " %9.4f  _b[mcsi]  "  Rsq: " %9.4f   e(r2)
}
---------------------------------------------------

And how in general would look fuction to use _b of year(t-1) to test on regression of year(t)??
Or it's impossible to get it from memory or variable (saving there beforehand)?

--------------------------------------------------------
forval j = 1/100 {
sort year
by year:  qui reg var`j' mcsi
egen beta_`j'_`year' = _b
egen alpha_`j'_`year' = _a
di "`j' //** ?year(t)? //**  {col 10}Beta: " %9.4f  _b[mcsi]  "  Rsq: " %9.4f   e(r2)

// ** after it clear memmory i guess...

test beta_j_year(t) for regression by year year(t+1)   varj = alphaj + b  //** dream that work

di test "`j' //** ?year? //**  {col 10} Some nice statistics.... "  Rsq: " %9.4f   e(r2) - //** dream that test results
}
-----------------------------
Thanks again for help!

13.05.2012, 13:13, "Nick Cox" <njcoxstata@gmail.com>:

>  Better as
>
>  foreach ticker of var var1-var100 {
>          qui reg `ticker' mcsi
>          di "`ticker'{col 10}Beta: " %9.4f  _b[mcsi]  "  Rsq: " %9.4f   e(r2)
>  }
>
>  Note also
>
>  forval j = 1/100 {
>          qui reg var`j' mcsi
>          di "`j'{col 10}Beta: " %9.4f  _b[mcsi]  "  Rsq: " %9.4f   e(r2)
>  }
>
>  On Sun, May 13, 2012 at 9:01 AM, Nick Cox <njcoxstata@gmail.com> wrote:
>>   Plus matching "}"
>>
>>   On Sun, May 13, 2012 at 9:00 AM, Nick Cox <njcoxstata@gmail.com> wrote:
>>>   See
>>>
>>>   SJ-8-4  pr0045  . . . . . . . . Stata tip 70: Beware the evaluating equal sign
>>>          . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
>>>          Q4/08   SJ 8(4):586--587                                 (no commands)
>>>          tip explaining the pitfall of losing content in a macro
>>>          because of limits on the length of string expressions
>>>
>>>   which you can access at
>>>
>>>   http://www.stata-journal.com/sjpdf.html?articlenum=pr0045
>>>
>>>   Your code can be simplified in any case.
>>>
>>>   foreach ticker of var var1-var100 {
>>>          qui reg `ticker' mcsi
>>>          di "Beta: " %9.4f  _b[mcsi]  "  Rsq: " %9.4f   e(r2)
>>>
>>>   Nick
>>>
>>>   On Sat, May 12, 2012 at 1:54 PM, Chipengovich Shpeng
>>>   <shpenglipeng@yandex.com> wrote:
>>>>   Good time of the day, I've been using Stata no so long - but I have a research, that requires some programming skills i unfortunately miss.
>>>>   Analysis concerns market-risk models estimations (CAPM in particular, but I'm stuck even with that one so doesn't follow to Fama-French models and etc)
>>>>   So that dataset contain weekly return data of numerous stocks (from 100 to 800 depending on dataset) in columns, and columns with corresponding week and year of observation. Looks like following var# stand for stock#, mcsi – for stock index.
>>>>
>>>>   year week var1 ... varn mcsi
>>>>   1        1      .      .    .       .
>>>>   1        2      .      .    .       .
>>>>
>>>>   The entire task could be described as estimating and testing CAPM model on this data. As to approach I need to use, it is estimation of CAPM models by each year (simple linear correlation of particular stock (var#) with index) and to test them on following year returns. The task could be separated into 2 parts - first one estimating CAPM. So that for each year i need to regress each var on index, then to save the regression results somehow to be useful for further analysis.
>>>>   That would be nice to have this results for example as a table with stock number, year, beta, r-squared
>>>>   I only know how to use loop to make regression for the whole data (all years). But even with such a code I got problems – It doesn’t want to allow to trace more than 40 results….
>>>>
>>>>   Code I’ve tried:
>>>>   local symbols = "var1 var2 var3 var4 var5 var6 var7 var8 var9 var10 var11 var12 var13 var14 var15 var16 var17 var18 var19 var20 var21 var22 var23 var24 var25 var26 var27 var28 var29 var30 var31 var32 var33 var34 var35 var36 var37 var38 var39 var40 var41 var42 var43 var44 var45 var46 var47 var48 var49 var50 var51 var52 var53 var54 var55 var56 var57 var58 var59 var60 var61 var62 var63 var64 var65 var66 var67 var68 var69 var70 var71 var72 var73 var74 var75 var76 var77 var78 var79 var80 var81 var82 var83 var84 var85 var86 var87 var88 var89 var90 var91 var92 var93 var94 var95 var96 var97 var98 var99 var100"
>>>>   foreach ticker in `symbols' {
>>>>          reg `ticker' mcsi
>>>>          local _b_`ticker'=_b[mcsi]
>>>>          local _r_`ticker'=`e(r2)'
>>>>   }
>>>>
>>>>   foreach ticker in `symbols' {
>>>>          di "Beta: " %9.4f `_b_`ticker'' "  Rsq: " %9.4f `_r_`ticker'' "
>>>>   }
>>>>   As a result Stata traces 40 regression tables, then gives “ va ambiguous abbreviation “ of red color, and moves to tracing  “Beta” and “Rsq” also for 40 estimations then stops.
>>>>   Also I’d like to ask, how I could run regressions, using my beta-estimations as coefficients for predicting next year stock returns.
>>>>   I could imagine we got these beta estimations (I’ve partly did it in excel, for the purpose I’d fail with stata) and as I could see the data for analysis would look like following, so that by each year we need to pich up var#_b of corresponding previous year beta and var#_a and to test regression
>>>>   Var# (of year n+1, week z) = var#_a(of year n) + var#_b (of year n) * index (of year n+1, week z)
>>>>
>>>>   year week var1 ... varn index      var1_a  var1_b   var2_a var2_b...    varn_b v index
>>>>   1        1      .      .    .       .      betavar1_y1
>>>>   1        2      .      .    .       .      betavar1_y1
>>>>   ..........................
>>>>   2         1                                betavar1_y2
>  *
>  *   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/
```