Implementing Panel-Corrected Standard Errors in R : The pcse Package

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 typically display both contemporaneous correlation across units and unit level heteroskedasity making inference 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.


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 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 consistent (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.

TSCS data and estimation
The critical assumption of TSCS models is that of "pooling," that is, all units are characterized by the same regression equation at all points in time. Given this assumption we can write the generic TSCS model as: where x i,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.
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, y i,t−1 , in the set of regressors, x i,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 If the errors obey the spherical error assumption -i.e., Ω = σ 2 I, where I is an N T × N T identity matrix -this simplifies to the usual OLS formula, where the OLS standard errors are the square roots of the diagonal terms of where σ 2 is the usual OLS estimator of the common error variance, σ 2 . If the errors obey the 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 e i,t be the OLS residual for unit i at time t. We can estimate a typical element of Σ byΣ 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 T i,j = T, ∀i = 1, . . . , N , we can simplify this tô where E is the T × N matrix of residuals and hence estimate Ω bŷ where ⊗ is the Kronecker product. PCSEs are thus computed by taking the square root of the diagonal elements of 3. Computational issues 3.1. Balanced data The computational issues are fairly straightforward for balanced data. We need only the vector of residuals from a linear fit, the model matrix (X), and indicators for group and time. Given the indicators for group and time, we can appropriately reshape the vector of residuals into an N × T matrix. We can then directly calculateΣ from Equation 4 and, therefore, the PCSE for the fit. Within R, the function lm returns a lm object that contains, among other items, the residuals and the model matrix. It does not, however, include indicators for unit and time and these must be supplied by the user. The package pcse implements the estimation described above in the function pcse, which takes the following arguments: pcse(lmobj, groupN, groupT, ...) 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.

Unbalanced data
The only interesting computational issue is how to handle unbalanced data sets. With an unbalanced dataset, Equation 4 is no longer valid. There have been two alternatives estimation procedures suggested for unbalanced data. The first is to estimate Σ using a balanced subset of the data. The second alternative is to calculate the elements of Σ pairwise. We will consider each in turn.
The advantage of using the balanced subset approach is its computation ease. The largest balanced subset of the data can be found using the following simple R code: 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 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 T i,j = min(T i , T j ), we will appropriately calculate the correlation between i and j. This is approach we use in pcse() when the option pairwise = TRUE is used.

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") We can then fit their basic model of economic growth with R> agl.lm <-lm(growth~lagg1 + opengdp + openex + openimp + + central + leftc + inter + as.factor(year), 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)  We can correct the standard errors by using: R> agl.pcse <-pcse(agl.lm, groupN = agl$country, groupT = agl$year) Included in the package is a summary function, summary.pcse, that can redisplay the estimates with the PCSE used for inference: 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)  (year) 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: R> aglUn.pcse2 <-pcse(aglUn.lm, groupN = aglUn$country, + groupT = aglUn$year, pairwise = FALSE)  (year) 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.

Summary
This paper briefly reviews estimation of panel-corrected standard errors for time-series-crosssection (TSCS) data. It discusses an implementation of estimating them in the R system for statistical computing in the pcse package.