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

# RE: st: RE: combination of rolling and if

 From Nick Cox To "'statalist@hsphsun2.harvard.edu'" Subject RE: st: RE: combination of rolling and if Date Wed, 14 Dec 2011 14:18:54 +0000

```We can't see your data and you do not show your exact detailed commands.

So we have no chance of reproducing your results and little chance of understanding _precisely_ what -if ST == 1- means.

Your best chance of getting good specific feedback is to replicate your problem with a concocted dataset or with one downloadable into Stata by everyone. -help q_cross- shows what panel datasets there are.

Nick
n.j.cox@durham.ac.uk

qing ye

I found that if I use if with tssmonth , what it does is still,
for every month, calulate average return for the next 12 months for
the observations whose 'ST'==1 at each period.

But this is not what I want,  I want to take the stocks whose current
period 'ST'==1 and calculate the returns for this group over the next
12 months. so my group of stocks are those whose current period
'ST'==1, I don't care whether their 'ST'==1 or not in any of the next
12 period.  But -if- seems only care whether 'ST'==1 in the periods
included in the 'windows'.

How do I solve this?

On 14 December 2011 06:08, Nick Cox <n.j.cox@durham.ac.uk> wrote:

> So, you don't like or don't believe my advice but you want some more!
>
> Please do try reading the help and do _some experimenting yourself_.
>
> For example, the help for -tssmooth- says
>
> "Setup
>        . webuse sales1
>        . tsset
>
> Create uniformly weighted moving average of sales by using two lagged terms, using three
> forward terms, and including the current observation in the filter
>        . tssmooth ma sm1=sales, window(2 1 3)"
>
> So, your problem sounds like -window(0 0 12)- after the equivalent set-up. Or perhaps 0 1 11. Does the next 12 months include the current month or not? Only you know for certain.
>
> In other words, smoothing here emphatically does include moving averages, contrary to your assertion.
>
> You want to select a group of stocks. You can try this command with or without -if- conditions. That will help you understand what it does.
>
> It's still my understanding that -rolling- is more complicated than you want. If you work out how to use it and find that I am wrong, I would be interested to hear about that.

qing ye

> hi, Thanks for this!
>
> I have checked the -tssmooth-, not exactly sure what it does as I
> don't know anything about smooth.  But I probably do not need it as I
> only want to select a group of stocks every month and calculate their
> mean returns over the next 12 months.
>
> is there anyway I can realize this using rolling? Rolling seems to be
> the only command I can think of in this context.
>
> "More commonly, you are interested in some _results_ only, but it is
> usually easiest to calculate every average in sight and then just
> ignore what you don't want. " -- You probably told me what to do, but
> I am still cannot get it.

On 14 December 2011 05:28, Nick Cox <n.j.cox@durham.ac.uk> wrote:

>> -if- in general selects observations and then does something with those observations alone.
>>
>> It does not try to interpret what you really want or extend any operation beyond the observations selected.
>>
>> I would be surprised if -rolling- were different, but (disclaimer) I've never studied it carefully.
>>
>> With Kit Baum I sorted out these questions to our mutual satisfaction in -mvsumm- (SSC), which has Remarks addressed to precisely this question.
>>
>> -rolling- came rolling along later, as I recall, and it (necessarily) is pitched much more generally and to allow many more kinds of result.
>>
>> But your example shows an interest in moving averages only. I guess you would find -mvsumm- or -tssmooth- more straightforward.
>>
>> In general, with -rolling- or anything else rolling -if- and/or -in- should only be used to exclude observations which should never participate in _any_ of the operations specified. More commonly, you are interested in some _results_ only, but it is usually easiest to calculate every average in sight and then just ignore what you don't want.

qing ye

>> I am a bit confused with combining the rolling and if.
>>
>> what does the following commend do?
>>
>> I have monthly panel data, variables are
>>
>> stock, date, return, ST
>>
>>
>> for the following code
>> rolling mean=r(mean), windows (12): sum return if ST=1, detail
>>
>> Did it, for every month, take observations whose 'ST'=1 and then
>> calculate mean return for this group of stocks over the next 12
>> months? This is what I wanted to do.
>>
>> Or, did it, for every month, take all the observations in the next 12
>> months, and calculate mean returns of all the observation for which
>> 'ST'==1?

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