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: RE: loop over regressions per year and calculate variables
From
Nick Cox <[email protected]>
To
"'[email protected]'" <[email protected]>
Subject
st: RE: loop over regressions per year and calculate variables
Date
Fri, 3 Dec 2010 17:42:24 +0000
You don't want to loop over the varlist -year-. That is one just variable, so one iteration of the loop. You want to loop over the distinct (some say unique) values of year. One approach starts with
egen order = group(year), label
su order, meanonly
forval y = 1/`r(max)' {
<regressions>
}
But set that aside, as there's no need to reinvent the wheel. Check out -statsby-. You can do most of the work in -statsby-.
Nick
[email protected]
P.S. Also, the following are usually bad practices:
1. Storing single constants in variables.
2. Using -egen- to calculate standard deviation. That's the job of -summarize-.
Léon Bueckins
I'm having some trouble in calculating variables that are based on the coefficients from my regression, since I use the -by:- option. So my goal is to generate new variables that store the calculation of each regression.
My basic setup is as follow:
* get the year information from my date variable
gen year=year(date)
* regression should be ran for each year
sort year
by year: xtscc ln_csad return_ma absreturn_ma return2_ma
* Calculate Standardized Betas (for each year), since this option is not available for -xtscc- regression
gen b_return_ma = _b[return_ma]
gen b_absreturn_ma = _b[absreturn_ma]
gen b_return2_ma = _b[return2_ma]
egen sd_return_ma = sd(return_ma)
egen sd_absreturn_ma = sd(absreturn_ma)
egen sd_return2_ma = sd(return2_ma)
egen sd_ln_csad = sd(ln_csad)
gen Z_return = b_return_ma * (sd_return_ma/sd_ln_csad)
gen Z_absreturn = b_absreturn_ma * (sd_absreturn_ma/sd_ln_csad)
gen Z_return2 = b_return2_ma * (sd_return2_ma/sd_ln_csad)
* Calculate the variable that I actually need (should have one value per year)
gen phi = abs(Z_return2/Z_absreturn)
***************
I tried to change this setup into a foreach loop but could not get it to work properly, can somebody help me out?
***************
foreach x of varlist year {
display `x'
* How can I now run the regression per year?
xtscc ln_csad return_ma absreturn_ma return2_ma
* And how to calculate the former variables and store them?
}
*
* 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/