<p>Version 2.5-0 of the R package ‘sandwich’ is available from CRAN now with enhanced object-oriented clustered covariances (for lm, glm, survreg, polr, hurdle, zeroinfl, betareg, …). The software and corresponding vignette have been improved considerably based on helpful and constructive reviewer feedback as well as various bug reports.</p>
<h2>Enhancements in version 2.5-0</h2>
<p>Most of the improvements and new features pertain to clustered covariances which had been introduced to the <a rel="nofollow" href="https://cran.r-project.org/package=sandwich" target="_blank">sandwich</a> package last year in version 2.4-0. For this my PhD student Susanne Berger and myself (= <a rel="nofollow" href="https://eeecon.uibk.ac.at/~zeileis/" target="_blank">Achim Zeileis</a>) teamed up with <a rel="nofollow" href="https://sites.google.com/site/npgraham1/" target="_blank">Nathaniel Graham</a>, the maintainer of the <a rel="nofollow" href="https://cran.r-project.org/package=multiwayvcov" target="_blank">multiwayvcov</a> package. With the new version 2.5-0 almost all features from <em>multiwayvcov</em> have been ported to <em>sandwich</em>, mostly implemented from scratch along with generalizations, extensions, speed-ups, etc.</p>
<p>The full list of changes can be seen in the <a rel="nofollow" href="https://cran.r-project.org/web/packages/sandwich/NEWS" target="_blank">NEWS</a> file. The most important changes are:</p>
<ul>
<li>
<p>The manuscript <code>vignette(&quot;sandwich-CL&quot;, package = &quot;sandwich&quot;)</code> has been significantly improved based on very helpful and constructive reviewer feedback. See also below.</p>
</li>
<li>
<p>The <code>cluster</code> argument for the <code>vcov*()</code> functions can now be a formula, simplifying its usage (see below). <code>NA</code> handling has been added as well.</p>
</li>
<li>
<p>Clustered bootstrap covariances have been reimplemented and extended in <code>vcovBS()</code>. A dedicated method for <code>lm</code> objects is considerably faster now and also includes various wild bootstraps.</p>
</li>
<li>
<p>Convenient parallelization for bootstrap covariances is now available.</p>
</li>
<li>
<p>Bugs reported by James Pustejovsky and Brian Tsay, respectively, have been fixed.</p>
</li>
</ul>
<h2>Manuscript/vignette</h2>
<p>Susanne Berger, Nathaniel Graham, Achim Zeileis: <a rel="nofollow" href="https://cran.r-project.org/web/packages/sandwich/vignettes/sandwich-CL.pdf" target="_blank">Various Versatile Variances: An Object-Oriented Implementation of Clustered Covariances in R</a></p>
<p>Clustered covariances or clustered standard errors are very widely used to account for correlated or clustered data, especially in economics, political sciences, or other social sciences. They are employed to adjust the inference following estimation of a standard least-squares regression or generalized linear model estimated by maximum likelihood. Although many publications just refer to “the” clustered standard errors, there is a surprisingly wide variety of clustered covariances, particularly due to different flavors of bias corrections. Furthermore, while the linear regression model is certainly the most important application case, the same strategies can be employed in more general models (e.g. for zero-inflated, censored, or limited responses).</p>
<p>In R, functions for covariances in clustered or panel models have been somewhat scattered or available only for certain modeling functions, notably the (generalized) linear regression model. In contrast, an object-oriented approach to “robust” covariance matrix estimation – applicable beyond <code>lm()</code> and <code>glm()</code> – is available in the sandwich package but has been limited to the case of cross-section or time series data. Now, this shortcoming has been corrected in <em>sandwich</em> (starting from version 2.4.0): Based on methods for two generic functions (<code>estfun()</code> and <code>bread()</code>), clustered and panel covariances are now provided in <code>vcovCL()</code>, <code>vcovPL()</code>, and <code>vcovPC()</code>. Moreover, clustered bootstrap covariances, based on <code>update()</code> for models on bootstrap samples of the data, are provided in <code>vcovBS()</code>. These are directly applicable to models from many packages, e.g., including <em>MASS</em>, <em>pscl</em>, <em>countreg</em>, <em>betareg</em>, among others. Some empirical illustrations are provided as well as an assessment of the methods’ performance in a simulation study.</p>
<h2>Illustrations</h2>
<p>To show how easily the clustered covariances from <code>sandwich</code> can be applied in practice, two short illustrations from the manuscript/vignette are used. In addition to the <code>sandwich</code> package the <code>lmtest</code> package is employed to easily obtain Wald tests of all coefficients:</p>
<pre><code>library(&quot;sandwich&quot;)
library(&quot;lmtest&quot;)
options(digits = 4)
</code></pre>
<p>First, a Poisson model with clustered standard errors from <a rel="nofollow" href="https://doi.org/10.1257/aer.103.1.277" target="_blank">Aghion <em>et al.</em> (2013, <em>American Economic Review</em>)</a> is replicated. To investigate the effect of institutional ownership on innovation (as captured by citation-weighted patent counts) they employ a (pseudo-)Poisson model with industry/year fixed effects and standard errors clustered by company, see their Table I(3):</p>
<pre><code>data(&quot;InstInnovation&quot;, package = &quot;sandwich&quot;)
ii &lt;- glm(cites ~ institutions + log(capital/employment) + log(sales) + industry + year,
  data = InstInnovation, family = poisson)
coeftest(ii, vcov = vcovCL, cluster = ~ company)[2:4, ]
##                         Estimate Std. Error z value  Pr(>|z|)
## institutions            0.009687   0.002406   4.026 5.682e-05
## log(capital/employment) 0.482884   0.135953   3.552 3.826e-04
## log(sales)              0.820318   0.041523  19.756 7.187e-87
</code></pre>
<p>Second, a simple linear regression model with double-clustered standard errors is replicated using the well-known <a rel="nofollow" href="https://www.kellogg.northwestern.edu/faculty/petersen/htm/papers/se/test_data.htm" target="_blank">Petersen data</a> from Petersen (2009, <em>Review of Financial Studies</em>):</p>
<pre><code>data(&quot;PetersenCL&quot;, package = &quot;sandwich&quot;)
p &lt;- lm(y ~ x, data = PetersenCL)
coeftest(p, vcov = vcovCL, cluster = ~ firm + year)
## t test of coefficients:
## 
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   0.0297     0.0651    0.46     0.65    
## x             1.0348     0.0536   19.32   &lt;2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
</code></pre>
<h2>Simulation</h2>
<p>In addition to the description of the methods and the software, the manuscript/vignette also contains a simulation study that investigates the properties of clustered covariances. In particular, this assesses how well the methods perfom in models beyond linear regression but also compares different types of bias adjustments (HC0-HC3) and alternative estimation techniques (generalized estimating equations, mixed effects).</p>
<p>The detailed results are presented in the manuscript – here we just show the results from one of the simulation experiments: The empirical coverage of 95% Wald confidence intervals is depicted for a beta regression, zero-inflated Poisson, and zero-truncated Poisson model. With increasing correlation within the clusters the conventional “standard” errors and “basic” robust sandwich standard errors become too small thus leading to a drop in empirical coverage. However, both clustered HC0 standard errors (CL-0) and clustered bootstrap standard errors (BS) perform reasonably well, leading to empirical coverages close to the nominal 0.95.</p>
<p><a rel="nofollow" href="https://i0.wp.com/eeecon.uibk.ac.at/~zeileis/assets/posts/2018-08-20-sandwich250/sim.png?ssl=1" target="_blank"><img alt="Simulation: Experiment III" src="https://i0.wp.com/eeecon.uibk.ac.at/~zeileis/assets/posts/2018-08-20-sandwich250/sim.png?w=456&ssl=1" data-recalc-dims="1"></a></p>
<p>Details: Data sets were simulated with 100 clusters of 5 observations each. The cluster correlation (on the x-axis) was generated with a Gaussian copula. The only regressor had a correlation of 0.25 with the clustering variable. Empirical coverages were computed from 10,000 replications.</p>
	<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/XFkndl9AhNk" height="1">