rregressionstandard-errorpanel-dataplm

Double clustered standard errors for panel data


I have a panel data set in R (time and cross section) and would like to compute standard errors that are clustered by two dimensions, because my residuals are correlated both ways. Googling around I found http://thetarzan.wordpress.com/2011/06/11/clustered-standard-errors-in-r/ which provides a function to do this. It seems a bit ad-hoc so I wanted to know if there is a package that has been tested and does this?

I know sandwich does HAC standard errors, but it doesn't do double clustering (i.e. along two dimensions).


Solution

  • Frank Harrell's package rms (which used to be named Design) has a function that I use often when clustering: robcov.

    See this part of ?robcov, for example.

    cluster: a variable indicating groupings. ‘cluster’ may be any type of
          vector (factor, character, integer).  NAs are not allowed.
          Unique values of ‘cluster’ indicate possibly correlated
          groupings of observations. Note the data used in the fit and
          stored in ‘fit$x’ and ‘fit$y’ may have had observations
          containing missing values deleted. It is assumed that if any
          NAs were removed during the original model fitting, an
          ‘naresid’ function exists to restore NAs so that the rows of
          the score matrix coincide with ‘cluster’. If ‘cluster’ is
          omitted, it defaults to the integers 1,2,...,n to obtain the
          "sandwich" robust covariance matrix estimate.