*! version 1.0 14 Apr 2021

Title

xtglsr -- User written post estimation command xtglsr, mnemonic for xtglsr[obust]:
Calculates robust, or cluster-robust variance post
[XT] xtgls -- Fit panel-data models by using GLS.
Requires Stata 11. Written by Gueorgui I. Kolev in April 2021.

Syntax

xtglsr
[,
options]

Optional
cluster(varname) Allows the cluster variable to be manually specified. By default the command xtglsr calculates heteroskedasticity robust
variance if in the xtgls you have specified panel structure to be panels(iid) or panels(heteroskedastic). If panels(correlated) has been specified in the xtgls estimation, the default is to calculate cluster-robust
variance clustered by the time variable set at the xtset stage.

minus(#) Controls the degrees of freedom adjustment factor in the robust, or cluster-robust variance calculation.
Default value is minus(0), which is equivalent to no degrees of freedom adjustment.
This option is inherited from [P] _robust and detailed instructions of how to apply the degrees of freedom adjustment through the option minus(#) are given in the manual entry for [P] _robust.

Description

Stata's [XT] xtgls -- Fit panel-data models by using GLS estimates panel data models by generalised least squares.
However [XT] xtgls cannot estimate robust or cluster-robust variance matrix and standard errors for the parameter estimates.
Gueorgui I. Kolev (2014) "Robust variance estimation in panel data generalized least squares regression" shows the relevant formulae for robust and
cluster-robust variance and standard errors, and these formulae are automatically implemented by the post estimation command xtglsr.
Important note 1): all that xtglsr does is replace the estimated non-robust variance with robust or cluster-robust variance.
xtglsr returns the robust or cluster-robust variance in the proper format, so that further post estimation commands
after xtglsr proceed as usual, e.g., [R] test, [R] lincom, [R] nlcom, etc., proceed after xtglsr as usual, and work as usual.
Important note 2): xtglsr can handle panelvar identifier which contains (not necessarily equally spaced) non-negative integers,
e.g., it is fine if your panelvar takes the values 0, 29, 5003, 200000, etc. If your panelvar takes more exotic values
(negative values, or non-integer values) such as say panelvar takes the values -56, -.29, .5003, 200.536, etc., xtglsr will generate an error.
If you have negative and/or non-integer values in panelvar, then please use firstly

egen newpanelvar = group(panelvar)

to create a new nicely spaced panel identifier, see [D] egen, group() before you

xtset newpanelvar timevar

your data, and before you fit the xtgls .

Options

Main

cluster(varname)

minus(#)

Examples

webuse invest2, clear
* The panel variable is Company, values 1,2,3,4,5. Time is the time variable.
* -xtglsr- will of course work with such nicely spaced Company values, but I will make them a bit more awkward:

replace company = round(log(company)*10000)

. tab company

company | Freq. Percent Cum.
------------+-----------------------------------
0 | 20 20.00 20.00
6931 | 20 20.00 40.00
10986 | 20 20.00 60.00
13863 | 20 20.00 80.00
16094 | 20 20.00 100.00
------------+-----------------------------------
Total | 100 100.00

* With such unequally spaced non-negative integers, -xtgls- still works.

xtset company time

* Typical use of -xtglsr- follows: we quietly fit -xtgls- and then we noisily robustify the variance:

quietly xtgls invest market stock	// If we want to compare non-robust with robust variance we can fit -xtgls- noisily as well.

xtglsr	// Because -xtgls- model fit was "Panels: homoskedastic" (the defailt) the variance is robust only.

quietly xtgls invest market stock, panels(correlated)

xtglsr, minus(1) // Because the -xtgls- model fit was with arbitrary correlation pattern within the panel,
// the default variance computed by -xtglsr- is cluster-robust, clustered on the time variable.
// I also applied the degrees of freedom adjustment, which is common in Stata for
// clustered variance: (# clusters)/(# clusters - 1).

* If the user wants to manually control the clustering, the user can specify the cluster variable.
* In such a case, it becomes responsibility of the user to make sure that clustering makes sense.
* For example, if I am fitting -xtgls- with panels(correlated), I have admitted that my companies in the same year are correlated.
* Therefore it would not make sense to cluster by anything which is a sub-group of time. Clustering by a super-group of time would make sense.

* I generate 5 super-groups of time, each consecutive 4 years are put together in a group

gen clustervar = ceil(5*time/20) // time = 1,2,3,4 is first group, time = 5,6,7,8 is the second group, etc.

quietly xtgls invest market stock, panels(correlated)

xtglsr, cluster(clustervar)

* Finally I show that -xtglsr- cannot handle panel identifiers like these

replace company = (company-10986)/10000

. tab company

company | Freq. Percent Cum.
------------+-----------------------------------
-1.0986 | 20 20.00 20.00
-.4055 | 20 20.00 40.00
0 | 20 20.00 60.00
.2877 | 20 20.00 80.00
.5108 | 20 20.00 100.00
------------+-----------------------------------
Total | 100 100.00

xtset company time

quietly xtgls invest market stock, panels(hetero)

. xtglsr
__000000.5108000040054321 invalid variable name
r(198);

* In this case, we need to make the company values take easier to handle values.

egen newcompany = group(company)

. tab newcompany

group(compa |
ny) | Freq. Percent Cum.
------------+-----------------------------------
1 | 20 20.00 20.00
2 | 20 20.00 40.00
3 | 20 20.00 60.00
4 | 20 20.00 80.00
5 | 20 20.00 100.00
------------+-----------------------------------
Total | 100 100.00

xtset newcompany time

quietly xtgls invest market stock, panels(hetero)

xtglsr // It works fine now.

* In this and all previous examples, post estimation after -xtglsr- works as usual

test market

nlcom _b[market]/_b[stock]

Author

version 1: 10 April 2021, Gueorgui I. Kolev
email: joro.kolev@gmail.com