Journal of Statistical Software: Implementing Panel-Corrected Standard Errors in R: The Pcse Package
Journal of Statistical Software: Implementing Panel-Corrected Standard Errors in R: The Pcse Package
Abstract
Time-series–cross-section (TSCS) data are characterized by having repeated observa-
tions over time on some set of units, such as states or nations. TSCS data typically display
both contemporaneous correlation across units and unit level heteroskedasity making in-
ference from standard errors produced by ordinary least squares incorrect. Panel-corrected
standard errors (PCSE) account for these these deviations from spherical errors and allow
for better inference from linear models estimated from TSCS data. In this paper, we
discuss an implementation of them in the R system for statistical computing. The key
computational issue is how to handle unbalanced data.
1. Introduction
Time-series–cross-section (TSCS) data are characterized by having repeated observations over
time on some set of units, such as states or nations. TSCS data have become common in
applied studies in the social sciences, particularly in comparative political science applications.
These data often show non-spherical errors because of contemporaneous correlation across the
units and unit level heteroskedasity. When fitting linear models to TSCS data, it is common
to use this non-spherical error structure to improve inference and estimation efficiency by
a feasible generalized least squares (FGLS) estimator suggested by Parks (1967) and made
popular by Kmenta (1986).
However, Beck and Katz (1995) showed that the Parks (1967) model had poor finite sample
properties. In particular, in a simulation study they showed that the estimated standard
errors for this model generated confidence intervals that were significantly too small, often
underestimating variability by 50% or more, and with only minimal gains in efficiency over a
2 pcse: Panel-Corrected Standard Errors in R
simple linear model that ignored the non-spherical errors. Therefore, Beck and Katz (1995)
suggested estimating linear models of TSCS data by ordinary least squares (OLS)1 and they
proposed a sandwich type estimator of the covariance matrix of the estimated parameters,
which they called panel-corrected standard errors (PCSE), that is robust to the possibility of
non-spherical errors.2
Although the PCSE covariance estimator bears some resemblance to heteroskedasity con-
sistent (HC) estimators (see, for example, Huber 1967; White 1980; MacKinnon and White
1985), these other estimators do not explicitly incorporate the known TSCS structure of the
data.3 This leads to important differences in implementation.
This paper describes an implementation of PCSEs in the R system for statistical computing (R
Development Core Team 2011). All of the functions described here are available in the package
pcse that is available from Comprehensive R Archive Network (CRAN) at http://CRAN.
R-project.org/package=pcse. The key computational issue is how to handle unbalanced
data. TSCS data is unbalanced when the number of observations for units vary.
R packages that estimate various models for panel data include plm (Croissant and Millo 2008)
and systemfit (Henningsen and Hamann 2007), that also implement different types of robust
standard errors. Some of these are only robust to unit heteroskedasity and possible serial
correlation. The pcse standard error estimate is robust not only to unit heteroskedacity, but
it also robust against possible contemporaneous correlation across the units that is common
in TSCS data.4 Package plm also provides an implementation of Beck and Katz (1995) PCSE
in the function vcovBK()5 that can be applied to panel models estimated by plm().
The next section fixes notation by briefly reviewing the linear TSCE model and the derivation
of PCSEs. Section 3 considers the computational issues with unbalanced panels. Section 4
illustrates the use of the package pcse. Finally, Section 5 concludes.
where xi,t is a vector of one or more (k) exogenous variables and observations are indexed by
both unit (i) and time (t).
TSCS analysts typically put some structure on the assumed error process. In particular,
they usually assume that for any given unit, the error variance is constant, so that the only
source of heteroskedasticity is differing error variances across units. Analysts also assume
1
OLS is implemented in R’s function lm().
2
The estimator is actually rather poorly named as it really used for TSCS data, in which the time dimension
is large enough for serious averaging within units, as opposed to panel data, which typically have short time
dimensions. However, this is the nomenclature used in the literature.
3
These heteroskedastic constituent covariance estimators are available in the R in the sandwich package
(Zeileis 2004)
4
For a discussion of the differences between TSCS and panel data see Beck and Katz (2011).
5
The function vcovBK() was not yet part of plm when the first version of pcse was developed.
Journal of Statistical Software – Code Snippets 3
that all spatial correlation is both contemporary and does not vary with time. The temporal
dependence exhibited by the errors is also assumed to be time invariant, and may also be
invariant across units. We, however, will be ignoring temporal dependence for the remainder
of this paper by assuming that the analyst has controlled for it either by including the lagged
dependent variable, yi,t−1 , in the set of regressors, xi,t , or using some sort of differencing.
Since these assumptions are all based on the panel nature of the data, we call them the “panel
error assumptions.”
As is well known, the correct formula for the sampling variability of the OLS estimates from
Equation 1 is given by the square roots of the diagonal terms of
c2 (X> X)−1
σ
c2 is the usual OLS estimator of the common error variance, σ 2 . If the errors obey the
where σ
panel structure, then this provides incorrect standard errors. Equation 2, however, can still
be used, in combination with that panel structure of the errors to provide accurate PCSEs.
For panel models with contemporaneously correlated and panel heteroskedastic errors, Ω is
an N T × N T block diagonal matrix with an N × N matrix of contemporaneous covariances,
Σ, along the diagonal. To estimate Equation 2 we need an estimate of Σ. Since the OLS
estimates of Equation 1 are consistent, we can use the OLS residuals from that estimation to
provide a consistent estimate of Σ. Let ei,t be the OLS residual for unit i at time t. We can
estimate a typical element of Σ by
PTi,j
t=1 ei,t ej,t
Σ̂i,j = , (3)
Ti,j
with the estimate Σ̂ being comprised of all these elements. We then use this to form the
estimator Ω̂ by creating a block diagonal matrix with the Σ̂ matrices along the diagonal.
With balanced data where Ti,j = T, ∀i = 1, . . . , N , we can simplify this to
(E> E)
Σ̂ = (4)
T
where E is the T × N matrix of residuals and hence estimate Ω by
Ω̂ = Σ̂ ⊗ IT (5)
where ⊗ is the Kronecker product. PCSEs are thus computed by taking the square root of
the diagonal elements of
3. Computational issues
The first argument lmobj is a fitted linear model object as returned by lm. The argument
groupN is a vector indication which cross-sectional unit an observation is from and groupT
indicates which time period.
It first computes the unit and time identifiers and their respective number N and T. The index
brows gives all of the balanced rows. We can restrict the calculations of Σ to this balanced
subset of data. This allows us to once again use Equations 4. The downside to this approach
is that we are not using all of the available data to estimate Σ.
Recall that Σ is the contemporaneous correlation between every pair of units in our sample.
The alternative approach then is to use Equation 3 for each pair i, j ∈ N to construct our
Journal of Statistical Software – Code Snippets 5
estimate Σ̂. That is, for each pair of units we determine with temporal observations overlap
between the two. We use this pairwise balanced sample to estimate Σ̂i,j .
We could do this directly by looping over all possible pairs and using Equation 3. However,
for large N this can be a large set to loop over. We can improve on this by instead filling in
the residual vector with zeros for the missing observations needed to balance out the data.
Clearly, these filled in observation do not alter the sum of the product of the residuals, since
they contribute zero if either i or j have been filled in. As long as we divide by the appropriate
Ti,j = min(Ti , Tj ), we will appropriately calculate the correlation between i and j. This is
approach we use in pcse() when the option pairwise = TRUE is used.
4. Example
In this section we demonstrate the use of the package pcse. The data we will use is from
Alvarez, Garrett, and Lange (1991), hereafter AGL, and were reanalyzed using a simple linear
model in Beck, Katz, Alvarez, Garrett, and Lange (1993). The data set is available in the
package as the data frame agl. The data cover basic macro-economic and political variables
from 16 OECD nations from 1970 to 1984. AGL estimated a model relating political and labor
organization variables (and some economic controls) to economic growth, unemployment, and
inflation. The argument was that economic performance in advanced industrialized societies
was superior when labor was both encompassing and had political power or when labor was
weak both in politics and the market. Here we will only look at their model of economic
growth.
First both the package and the data need to be loaded into R with
R> library("pcse")
R> data("agl")
The model assumes that growth depends on lagged growth (lagg1), vulnerability to OECD
demand (opengdp), OECD export (openex), OECD import (openimp), labor organization
index (central), year fixed effects (as.factor(year)), the fraction of cabinet portfolios held
by “left” parties (leftc), and interaction of central and leftc (inter). The interest focuses
on the last three variables, particularly the interaction.
Here are the fit and standard errors without correcting for the panel structure of the data
(note to save space the estimates for the year effects have been excluded from the printout.):
R> summary(agl.lm)
Call:
lm(formula = growth ~ lagg1 + opengdp + openex + openimp + central +
leftc + inter + as.factor(year), data = agl)
6 pcse: Panel-Corrected Standard Errors in R
Residuals:
Min 1Q Median 3Q Max
-5.837 -1.197 0.067 1.170 4.529
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 5.968890 0.916402 6.51 5.0e-10 ***
lagg1 0.050315 0.139204 0.36 0.71812
opengdp -0.002330 0.001867 -1.25 0.21342
openex 0.002008 0.001208 1.66 0.09786 .
openimp -0.000609 0.001679 -0.36 0.71720
central -0.763563 0.216258 -3.53 0.00051 ***
leftc -0.024712 0.009276 -2.66 0.00829 **
inter 0.012868 0.003614 3.56 0.00045 ***
as.factor(year)1971 -1.205114 0.658282 -1.83 0.06851 .
as.factor(year)1972 0.530824 0.699475 0.76 0.44874
as.factor(year)1973 0.572342 0.702811 0.81 0.41633
as.factor(year)1974 -5.085447 1.100673 -4.62 6.6e-06 ***
as.factor(year)1975 -5.280737 0.953471 -5.54 8.7e-08 ***
as.factor(year)1976 -0.210344 0.885918 -0.24 0.81255
as.factor(year)1977 -2.267336 0.676865 -3.35 0.00095 ***
as.factor(year)1978 -1.258657 0.742950 -1.69 0.09167 .
as.factor(year)1979 -1.632229 0.702948 -2.32 0.02116 *
as.factor(year)1980 -3.902415 0.775032 -5.04 1.0e-06 ***
as.factor(year)1981 -4.652619 0.824901 -5.64 5.2e-08 ***
as.factor(year)1982 -4.532462 0.926328 -4.89 1.9e-06 ***
as.factor(year)1983 -2.775442 0.744784 -3.73 0.00025 ***
as.factor(year)1984 -0.770129 0.807864 -0.95 0.34150
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Included in the package is a summary function, summary.pcse, that can redisplay the esti-
mates with the PCSE used for inference:
R> summary(agl.pcse)
Results:
---------------------------------------------
We note that the standard error on central has increased a bit, but the standard errors of
the other two variables of interest, leftc and inter have actually decreased.
We have also included an unbalanced version of the AGL data set, aglUn, that was created
by randomly deleting some observations. This was only done to demonstrate how estimates
vary by casewise and pairwise estimation of the covariance matrix and is not a recommended
modeling strategy. As before, we can estimate the same model by:
R> data("aglUn")
R> aglUn.lm <- lm(growth ~ lagg1 + opengdp + openex + openimp +
+ central + leftc + inter + as.factor(year), data = aglUn)
R> aglUn.pcse1 <- pcse(aglUn.lm, groupN = aglUn$country,
+ groupT = aglUn$year, pairwise = TRUE)
R> summary(aglUn.pcse1)
Results:
---------------------------------------------
Here we see the estimates of the pairwise version of the PCSE, since the option pairwise =
TRUE was given. The results are close to the original results for the balanced data.
If we preferred the casewise estimate that uses the largest balanced subset to estimate the
contemporaneous correlation matrix, we do that by:
Warning message:
In pcse(aglUn.lm, groupN = aglUn$country, groupT = aglUn$year, ...
Caution! The number of CS observations per panel, 7, used to compute
the vcov matrix is less than half theaverage number of obs per panel
in the original data.You should consider using pairwise selection.
R> summary(aglUn.pcse2)
Results:
---------------------------------------------
Here we see that the software has issued a warning about the calculation of the standard
errors. Although there only 10 missing observations, they are intermingled through out the
data. This means that the largest balanced panel only has seven time points, whereas the
data runs for 14. In this case, it is not clear that PCSEs will be correctly estimated although
in this case they are not that different from the casewise estimate.
5. Summary
This paper briefly reviews estimation of panel-corrected standard errors for time-series–cross-
section (TSCS) data. It discusses an implementation of estimating them in the R system for
statistical computing in the pcse package.
Computational details
The results in this paper were obtained using R 2.13.0 with the package pcse 1.8. R and the
pcse package are available from CRAN at http://CRAN.R-Project.org/.
References
Beck N, Katz JN, Alvarez RM, Garrett G, Lange P (1993). “Government Partisanship, La-
bor Organization, and Macroeconomic Performance: A Corrigendum.” American Political
Science Review, 87, 945–948.
Croissant Y, Millo G (2008). “Panel Data Econometrics in R: The plm Package.” Journal of
Statistical Software, 27(2), 1–43. URL http://www.jstatsoft.org/v27/i02/.
Kmenta J (1986). Elements of Econometrics. 2nd edition. Macmillan, New York, NY.
R Development Core Team (2011). R: A Language and Environment for Statistical Computing.
R Foundation for Statistical Computing, Vienna, Austria. ISBN 3-900051-07-0, URL http:
//www.R-project.org/.
Zeileis A (2004). “Econometric Computing with HC and HAC Covariance Matrix Estimators.”
Journal of Statistical Software, 11(10), 1–17. URL http://www.jstatsoft.org/v11/i10/.
Affiliation:
Jonathan N. Katz
California Institute of Technology
DHSS 228-77
Journal of Statistical Software – Code Snippets 11