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

# RE: st: RE: Difficulties in variable calculation using panel data

 From "Degas Wright" To Subject RE: st: RE: Difficulties in variable calculation using panel data Date Mon, 1 Nov 2010 07:43:39 -0400

```Leon,
Glad to be of assistance.

Degas A. Wright, CFA
Chief Investment Officer
Decatur Capital Management, Inc.
250 East Ponce De Leon Avenue, Suite 325
Decatur, Georgia  30030
Voice: 404.270.9838
Fax:404.270.9840
Website: www.decaturcapital.com
-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Léon Bueckins
Sent: Saturday, October 30, 2010 9:36 AM
To: statalist@hsphsun2.harvard.edu
Subject: Re: st: RE: Difficulties in variable calculation using panel data

Hi Degas,

thank you for fast response. I figured out the first two problems in the meantime myself. The return formula now computes the correct results but I will adjust it according to you suggestions regarding the missings thank you for that. For the second formula I now have a code that does exactly what I want but it is much to slow as it runs for each time point, and I have approx. 7,000. So is there anybody who might help me translate it into mata?

xtset group_id date, daily
gen return = 100 * (log(price) - log(L.price))

/*generate time index*/
bysort group_id : g trun =_n

/*number of groups*/
qui xtsum trun
local nT =r(Tbar)
local nGroups =r(n)

/*mean values of each group per t*/
g return_mt =.
forval t =1/`nT' {
qui xtsum return if trun ==`t'
replace return_mt =r(mean) if trun ==`t'
}

/*generate variable dev*/
bysort igroup : g dev =1/`nGroups' *(abs(return - return_mt))

/*drop not needed variables*/
drop trun return_mt

Regards

Am 30.10.2010 um 14:41 schrieb Degas Wright:

> Leon,
> I ran into similar challenges analyzing my panel data.
>
> On the gaps issue, you can use -tsfill- it fills in the gaps in the time variable but not the other variables in the dataset. You can review the data and drop the observations missing data(i.e. the weekend dates) and create a new time variable.  This works if you do not need weekend dates to conduct special analysis such as weekend anomalies research.
>
> Once you have dropped the weekend dates, the code will be;
>
> . generate newtimevariable = _n
>
> .tsset newtimevariable
>  Time variable: newtimevariable, 1 to _n
>            Delta: 1 unit
>
> Now your return calculations should work since Stata will use every observation.
>
> To conduct a cross sectional regression, you will need to use -reg- rather than -xtreg- . Also, you can use the -forvalues- command to loop through your data one observation at a time.
>
> The code will be:
>
> . forvalues test=1/_n{
>   2. display "Period="`test'
>   3. regress dev return
>   4. }
>
>
> Where _n is the number of observations.
>
> I do not understand which outputs that you want to average over all regressions. But I hope the other information will be helpful.
>
> Degas A. Wright, CFA
> Chief Investment Officer
> Decatur Capital Management, Inc.
> 250 East Ponce De Leon Avenue, Suite 325
> Decatur, Georgia  30030
> Voice: 404.270.9838
> Fax:404.270.9840
> Website: www.decaturcapital.com
> -----Original Message-----
> From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Léon Bueckins
> Sent: Friday, October 29, 2010 7:42 PM
> To: statalist@hsphsun2.harvard.edu
> Subject: st: Difficulties in variable calculation using panel data
>
> Hi,
>
> I hope somebody will be so kind to help me to get my approach "translated" to Stata, as I'm facing some difficulties:
>
> I have a panel dataset and set it this way via 'xtset group_id date, daily'
>
> Hence my data looks like this:
>
> date | price | group_name | group_id
>
> 01-01-2005 | 34,78 | group A | 1
> 01-01-2005 | 12,56 | group B | 2
> 01-01-2005 | 17,21 | group C | 3
> 01-02-2005 | 67,53 | group A | 1
> 01-02-2005 | 25,90 | group B | 2
> 01-02-2005 | 44,78 | group C | 3
> ...                  | ...        | ...           | ...
>
> a) Now I want to calculate the daily return for each group across time using:
>
> gen return = 100 * (log(price) - log(L.price))
>
> When doing this, I face two problems:
>
> I. Weekends are treated as gaps, thus no return for Monday is computed, but Stata should rather use Fridays' value to compute it. May I skip the 'daily' option in the xtset command?
>
> II. The results are wrong when I proof them randomly by hand. I guess Stata mixes up the groups. How can I tell Stata to look at the timeseries for each group separately?
>
> b) Second, I want to compute another variable using the following formula but don't know how to tell it Stata:
> dev = 1/N * sum( abs(return_i, t     -     return_m,t) )
>
> N = total number of groups within panel (can be substituted by [ `=_N' ], right?)
> return_i,t = return of group i at time point t
> return_m,t = average return across all groups at time point t
>
> c) finally I try to run a regression on dev
>
> xtreg dev return ...
>
> But in the regression I want Stata to (theoretically) do one regression per t and compute the average values over all regressions, so I need just the cross-sectional and not the timeseries information in the data.
>
> Help is greatly appreciated, so thank you very much for helping me!
>
> *
> *   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/

*
*   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/
```