## Introduction

In this blog post, we will explore the capabilities of asrol for calculating descriptive statistics in a rolling window or over a grouping variable while excluding focal observations. asrol is a Stata command that facilitates the calculation of descriptive statistics. It can handle various data structures, including time series and panel data, and efficiently manage missing values, duplicate values, and time series gaps. Some of the statistics that can be estimated using asrol include mean, standard deviation, minimum, maximum, percentiles, median, missing values, count, first and last observations, product, and geometric mean. Moreover, asrol provides options to exclude focal observations or observations defined by a grouping variable, enabling even more flexibility in the analysis.

## 1. Excluding Focal Observations

The option xf in asrol allows us to exclude focal observations while calculating the required statistics. There are two flavors of excluding focal observations: excluding only the current observation or excluding all observations of the relevant variable with similar (duplicate) values in the given window.

### Example A: Excluding Current Observation

Let’s consider the following dataset with 5 observations, where X is the variable of interest, and year is the range variable.

. asrol X, stat(mean) xf(focal) gen(xfocal) +---------------------------------------+ | year X xfocal xfyear | |---------------------------------------| | 2001 100 350 350 | | 2002 200 325 325 | | 2003 300 300 266.66667 | | 2003 400 275 266.66667 | | 2004 500 250 250 | +---------------------------------------+

In Example A, we use xf(focal) to exclude the current observation. The generated variable xfocal contains the mean values of the remaining observations in the given window, excluding the focal observation. For instance, in the year 2001 , xfocal has a value of 350, which is the average of the values of X in the years 2002, 2003, 2003, and 2004: (200+300+400+500)/4 = 350. Similarly, the second observation of xfocal is 325, calculated as (100+300+400+500)/4 = 325.

## 2. Exclude by groups

`xf(year)`

to define the focal observation(s), all observations belonging the variable year will be excluded that are similar to the current observation. See the following example and its explanation.
. asrol X, stat(mean) xf(year) gen(xfyear) obs 1: (200 + 300 + 400 + 500)/4 = 350 obs 2: (100 + 300 + 400 + 500)/4 = 325 obs 3: (100 + 200 + 500 ) /3 = 266.66667 obs 4: (100 + 200 + 500 ) /3 = 266.66667 obs 5: (100 + 200 + 300 + 400)/4 = 250

Example B differs from Example A in definition of the focal observation(s). In Example B, we invoke the option xf() as xf(year), where year is an existing numeric variable. With this option, the focal observation(s) is(are) defined as the current observation and other observations where the focal observation of the rangevar has duplicates. Our data set has two duplicate values in the rangevar , i.e., year 2003. Therefore, the mean values are calculated as shown above.

## Examples

**Example 1: Exclude focal in rolling window**

webuse grunfeld bys company: asrol invest, stat(mean) xf(focal)

**Example 2: Exclude focal in no-rolling window**

* Find Standard Deviation: last 5 years * excluding the current observation or the most recent one webuse grunfeld bys company: asrol invest, stat(sd) win(year -5 -1)

**Example 4: Exclude by groups in no-rolling window**

```
webuse grunfeld
bys company: asrol invest, stat(mean) xf(company)
```

Explanation: The above command finds standard deviation for whole sample while excluding the focal company. Therefore, when the company is equal to 1, the standard deviation is based on the remaining 19 companies. Similarly, when the company is 2, the standard deviation is based on the first company, and companies 3 to 20.

## See Alos

asm : for momentum portfolios

asdocx : Easily create publication-quality tables from Stata and export to Word, Excel, LaTeX

asdoc : Easily create publication-quality tables from Stata

astile : for creating fastest quantile groups

asgen : for weighted average mean

asrol : for rolling-window statistics

asreg : for rolling-window, by-group, and Fama and MacBeth regressions

ascol : for converting asset returns and prices from daily to a weekly, monthly, quarterly, and yearly frequency

searchfor : for searching text in data sets

flexmat : Creates a flexible matrix of real, string and complex elements

## Leave A Comment