Repository navigation
Description
trying to figure out what xtpcse in Stata is doing
based on the calculations (I didn't see a reference yet)
xtpcse in the case below assumes that we have a common covariance matrix across time periods. In contrast, cluster robust with time as group index does not assume a contemporaneous covariance matrix that is common across periods (heteroscedasticity is included)
(The inner E(u u') is based on covariance across periods in xtpcse versus the outer product for each period separately in cluster.)
example case Grunfeld from unittests in #1189
xtpcse invest mvalue kstock
contemporaneous correlation only, balanced panel
E(u u') = Sigma_N kron I_T, where
Sigma is contemporaneous correlation of errors, similar to SUR
I_T is identity matrix
playing in the interpreter to build the sandwich, I get the same results as xtpcse
>>> exog = res.model.exog
>>> resid2d = res.resid.reshape((10, -1)).T
>>> c = np.dot(resid2d.T, resid2d) / resid2d.shape[0]
>>> S_inner = sum([np.dot(exog[time==ti, :].T, np.dot(c, exog[time==ti, :])) for ti in np.unique(time)])
>>> np.sqrt(np.diag(np.dot(res.normalized_cov_params, np.dot(S_inner, res.normalized_cov_params))))
array([ 0.00721244, 0.02788621, 6.78096485])
>>> _ - [.0072124, .0278862, 6.780965] # compare with Stata results for bse
array([ 3.76733978e-08, 1.30352255e-08, -1.52535000e-07])
setup for OLS
import statsmodels.datasets.grunfeld as gr
dtapa = gr.data.load_pandas()
#Stata example/data seems to miss last firm
dtapa_endog = dtapa.endog[:200]
dtapa_exog = dtapa.exog[:200]
res = OLS(dtapa_endog, add_constant(dtapa_exog[['value', 'capital']],
prepend=False)).fit()
#time indicator in range(max Ti)
time = np.asarray(dtapa_exog[['year']])
xtpcse can do more, can also estimate AR(1) autocorrelation.
The pure contemporaneous correlation version is closer to SUR and ShortPanel (with time as group index)
Aside: as comparison, cluster robust with time index has larger standard errors (10% to 50%)
>>> res_cltc = res.get_robustcov_results(cov_type='cluster', groups=time, use_correction=True)
>>> res_cltc.bse
array([ 7.90949350e-03, 3.86723375e-02, 1.02728909e+01])
>>> res_cltc.bse / [.0072124, .0278862, 6.780965]
array([ 1.09665208, 1.38679123, 1.51496003])
Activity