Bookmark and Share

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


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

st: Extract and save R2 as a variable from looping regressions


From   "Aleksej Rechytskyi" <[email protected]>
To   [email protected]
Subject   st: Extract and save R2 as a variable from looping regressions
Date   Mon, 30 Sep 2013 19:24:07 +0200 (CEST)

Hello everyone,

I have problems with extracting and storing R2 as a variable from looping regressions. My dataset is a panel dataset including company ID, date, daily stock return and daily market return. It is sorted by the company ID (permno) and for each company ID I there is a running time series.

Company ID || date || stock return || daily market return
1 || 01.01.1990 || y || x
1 || 02.01.1990 || y || x
1|| 03.01.1990 || y || x
2|| 01.01.1990 || y || x
2|| 02.01.1990 || y || x
2|| 03.01.1990 || y || x
…

I need to regress the stock return on the market return in a loop for every company ID and month on the basis of the daily returns. I constructed the following:

gen R2=.
gen mofd = mofd(date)
gen gr_compIDmofd=group (companyID mofd)
local i=1
foreach i in gr_compIDmofd {
bysort gr_compIDmofd: regress stock_return market_return, robust
replace R2_restri=e(r2) if gr_compIDmofd ==`i'
clear e(r2)
local i=`i'+1
}

Instead of replacing the missing value in R2 by the e(r2) for EACH DISTINCT gr_compIDmofd, the code replaces ALL missing values with the R2 from the first regression irrespective of the gr_compIDmofd:

Company ID || date || stock return || daily market return||gr_compIDmofd||R2
1 || 01.01.1990 || y || x||1||0.5
1 || 02.01.1990 || y || x||1||0.5
1|| 03.01.1990 || y || x||1||0.5
2|| 01.01.1990 || y || x||2||0.5
2|| 02.01.1990 || y || x||2||0.5
2|| 03.01.1990 || y || x||2||0.5
…

Thank you very much in advance for your help.

Best regards,
Aleksej Rechytskyi

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index