# Clustered standard error - intuitive explanation

I understand that the standard error is standard deviation of the sampling distribution about some parameter, for example the sample mean or a coefficient in a regression model.

I also know that when your data does not meet assumptions of independence, for example geographic or temporal relationships between randomization units, we need to "cluster" the standard errors of estimated parameters at the lowest level meeting assumptions of independence.

Let's take the temporal example. Suppose that we have panel data for various randomization units in an RCT. Because $$y_t$$ is not independent of $$y_{t-1}$$ we cannot claim independence. Thus our sample size cannot be $$NT$$ but rather $$N$$.

I believe I have a firm grasp on the why. Where I'm struggling is the how. How do we cluster standard errors in this context? Is clustered standard error a generalization of pooled standard error to include several units (rather than two)?

Does our approach differ depending on whether we seek to infer an observed parameter, for example the mean, vs a latent parameter, for example a regression coefficient $$\beta$$?

• A good start is Intuition behind clustering standard errors. The two most recent papers in this answer have lots of intuition too, though more technical. Commented Jun 19 at 21:12
• In the simplest case from survey sampling, the variance of a clustered sample is just the variance of the cluster average (rather than the unit average) divided by some constant. See proposition 1.2 from post here: stats.stackexchange.com/questions/628088/… This is very straightforward. What's complicated is trying to relate this back to simple random sampling. Commented Jun 19 at 23:20
• Thus our sample size cannot be $NT$ but rather $N$. I believe I have a firm grasp on the why. The first statement is not generally correct. In general, $T$ dependent observations are not equivalent to $1$ independent observation. Commented Jun 20 at 9:49

There are at least two ways to understand the clustered standard error estimator.

The first is to think about what units actually are independent. If you have $$T$$ time points for each of $$N$$ people, but the $$N$$ people are (or can be treated as) a random sample, then you can collapse your data down to one summary statistic for each person.

For example, if you want to compute the mean and (for simplicity) if the number of measurements on each person is the same, you can write $$\bar Y_{i\cdot}=\frac{1}{T}\sum_{t=1}^T Y_{it}$$ and take $$\bar Y = \frac{1}{N}\sum_{i=1}^N \bar Y_{i\cdot}.$$ So $$\bar Y$$ is a straightforward average of $$N$$ independent things, the $$\bar Y_{i\cdot}$$, and its variance is estimated by $$\widehat{\text{var}}[\bar Y]= \frac{1}{N-1}\sum_{i=1}^N (\bar Y_{i\cdot}-\bar Y)^2.$$ Writing this in terms of the original data $$\widehat{\text{var}}[\bar Y]= \frac{1}{T^2(N-1)}\sum_{i=1}^N (\sum_{t=1}^T Y_{it}-\bar Y)^2$$ or, expanding the variance of the sum to a sum of crossproducts $$\widehat{\text{var}}[\bar Y]= \frac{1}{T^2(N-1)}\sum_{i=1}^N \sum_{t,s=1}^T (Y_{it}-\bar Y)(Y_{is}-\bar Y).$$

To do this for any other statistic $$\hat\theta$$ you write $$\hat\theta$$ as a sum of influence functions or estimating functions and do it to them.

The other way is to start with the formula for the variance of a sum, which will give $${\text{var}}[\bar Y]= \frac{1}{T^2N}\sum_{i,j=1}^N \sum_{t,s=1}^T \text{cov}[Y_{it},Y_{is}].$$ If we knew the mean $$\mu$$ we could estimate $$\text{cov}[Y_{it},Y_{is}]$$ by $$(Y_{it}-\mu)(Y_{is}-\mu$$. It would not be a good estimate, since it's based on just one observation, but it would be an unbiased estimate. If we take a lot of unbiased estimates and add them up, the result will still be unbiased and we can hope it's a good estimate. Sadly, it isn't; it's too variable.

However, we know that a lot of the covariance terms are zero: the ones where $$i=\neq j$$. If we just set those to zero instead of estimating them, we get a new unbiased quantity, and this one actually is good: $${\text{var}}[\bar Y]= \frac{1}{T^2N}\sum_{i=1}^N \sum_{t,s=1}^T (Y_{it}-\mu)(Y_{is}-\mu)$$ We can't use this, either. It depends on $$\mu$$, which is precisely the thing we don't know. However, we can hope that substituting $$\bar Y$$ for $$\mu$$ will give a good estimator $$\widehat{\text{var}}[\bar Y]= \frac{1}{T^2N}\sum_{i=1}^N \sum_{t,s=1}^T (Y_{it}-\bar Y)(Y_{is}-\bar Y)$$ And it does, as long as $$N$$ is large enough. It's not unbiased any more; you can change $$N$$ for $$N-1$$ to get a slightly better estimate.

The useful aspect of this second approach is that it helps us work out variance estimators in more complicated settings: crossed clustering, time series, spatial data, genetic correlations, etc