Tutorials › Propensity Score Matching and Instrumental Variables › A Worked Clustered Panel 2SLS Analysis

Two-Stage Least Squares · Tutorial 508 of 699

A Worked Clustered Panel 2SLS Analysis

Work through a clustered panel 2SLS calculation from within-transformed data to cluster-robust uncertainty and a test of a coefficient restriction.

Advanced 11 min read

What You'll Learn

  • Specify the fixed-effects panel model and the moment conditions needed after within transformation.
  • Calculate a just-identified within 2SLS estimate from hypothetical panel differences.
  • Construct a cluster-robust standard error from cluster-level score contributions.
  • Test a linear restriction using a cluster-based reference distribution and interpret its p-value.
  • Identify failures involving instrument validity, relevance, clustering, and fixed-effects variation.
  • Compare fixed-effects 2SLS with pooled 2SLS and fixed-effects OLS.

Why the Panel Structure Matters

In panel data, repeated observations on the same unit may share unobserved characteristics and errors. Fixed effects remove time-constant unit characteristics; cluster-robust uncertainty allows the remaining errors to be dependent within a unit. When a regressor is endogenous, the coefficient is estimated using instruments after removing the fixed effects. These are distinct parts of the analysis: fixed effects address time-invariant confounding, an instrument addresses endogeneity under its validity assumptions, and clustering changes the estimated uncertainty.

This worked analysis uses a deliberately small, hypothetical, two-period panel with one endogenous regressor and one excluded instrument. Its small size makes the arithmetic transparent, not suitable for drawing a real-world conclusion. The model includes unit fixed effects and no time-varying exogenous controls or time fixed effects.

Model and Conditions

Definition: For unit \(i=1,\ldots,G\) and period \(t=1,\ldots,T\), consider \(y_{it}=\alpha_i+\beta x_{it}+u_{it}\), where \(y_{it}\) is the outcome, \(x_{it}\) is an endogenous regressor, \(\alpha_i\) is a time-constant unit effect, and \(u_{it}\) is the error. The excluded instrument is \(z_{it}\). A within-transformed variable is \(\tilde v_{it}=v_{it}-\bar v_i\), where \(\bar v_i=T^{-1}\sum_t v_{it}\). The fixed-effects 2SLS estimate uses \(\tilde z_{it}\) as an instrument for \(\tilde x_{it}\).

For this scalar, just-identified model, the estimate is the ratio of the within-transformed instrument-outcome cross-product to the instrument-regressor cross-product. The fixed effect disappears because \(\tilde\alpha_i=0\). This calculation requires a nonzero within first-stage cross-product, \(\sum_i\sum_t\tilde z_{it}\tilde x_{it}\ne 0\), as well as a valid instrument.

Instrument validity must concern the transformed variables, not merely a contemporaneous level moment. A sufficient moment condition for this model is \(E[\sum_t\tilde z_{it}\tilde u_{it}]=0\), along with relevance and suitable regularity conditions. Strict exogeneity of the instrument across periods, \(E[z_{is}u_{it}]=0\) for every pair of periods \(s,t\), is one stronger condition that can imply the needed within moment. Contemporaneous orthogonality alone need not do so: within transformation combines observations across time. These assumptions are substantive and cannot be verified from the calculation below.

For cluster-robust inference, take units as the clusters. The usual large-cluster justification requires independent clusters, allows dependence of errors within a cluster, and requires appropriate finite-moment and identification conditions. The number of clusters should be sufficiently large for the chosen reference approximation to be credible. In the numerical example, we use the uncorrected cluster sandwich variance and a \(t\) reference distribution with \(G-1\) degrees of freedom as an explicitly stated small-sample approximation.

Worked Example: Transforming a Two-Period Panel

Worked Example: Transforming a Two-Period Panel

Suppose eight hypothetical units are observed in periods 1 and 2. For each unit, the period-1 values of \(x\), \(z\), and \(y\) are set to zero, and their period-2 values are shown below. Adding a unit-specific constant to both outcome values would not change the within calculations. The table therefore gives the first differences \(\Delta v_i=v_{i2}-v_{i1}\).

Unit\(\Delta z_i\)\(\Delta x_i\)\(\Delta y_i\)
1114
220.50
3114
420.50
5112
620.51
7112
820.51

With two periods, the within-transformed values for a unit are \(-\Delta v_i/2\) in period 1 and \(+\Delta v_i/2\) in period 2. Thus each unit contributes \(\Delta z_i\Delta x_i/2\) to the within cross-product and \(\Delta z_i\Delta y_i/2\) to the instrument-outcome cross-product. Here \(\sum_i\Delta z_i\Delta x_i=8\) and \(\sum_i\Delta z_i\Delta y_i=16\). Consequently,

$$ \hat\beta_{\mathrm{FE\text{-}2SLS}} =\frac{\sum_i\sum_t\tilde z_{it}\tilde y_{it}} {\sum_i\sum_t\tilde z_{it}\tilde x_{it}} =\frac{16/2}{8/2} =\frac{8}{4} =2. $$

The factors of \(1/2\) arise because each demeaned difference is split across two periods; they cancel in the ratio. The estimate is 2 outcome units per unit of \(x\). This computation alone does not establish that the instrument is valid or that the estimate is precise.

Cluster-Robust Uncertainty

The cluster sandwich uses a score contribution from each unit rather than treating all unit-period observations as independent. In this just-identified scalar case, define the within residual \(\hat{\tilde u}_{it}=\tilde y_{it}-\hat\beta\tilde x_{it}\). Unit \(i\)'s instrument score is \(q_i=\sum_t\tilde z_{it}\hat{\tilde u}_{it}\). The uncorrected cluster-robust variance is the sum of squared cluster scores divided by the squared instrument-regressor cross-product.

$$ \widehat{\operatorname{Var}}_{\mathrm{CR0}}(\hat\beta) = \frac{\sum_{i=1}^{G}q_i^2} {\left(\sum_{i=1}^{G}\sum_{t=1}^{T}\tilde z_{it}\tilde x_{it}\right)^2}. $$

This formula retains within-unit dependence by forming one score for each entire unit. In a model with additional included exogenous regressors, the corresponding matrix sandwich uses the full instrument and regressor matrices; one must not apply this scalar formula unchanged.

Worked Example: Calculating the Cluster-Robust Standard Error

Use the estimate \(\hat\beta=2\) and the panel differences above. For each unit, let \(s_i=\Delta z_i(\Delta y_i-2\Delta x_i)\). Since the within score is \(q_i=s_i/2\), the arithmetic is:

Unit\(\Delta y_i-2\Delta x_i\)\(s_i=\Delta z_i(\Delta y_i-2\Delta x_i)\)\(q_i=s_i/2\)
1221
2-1-2-1
3221
4-1-2-1
5000
6000
7000
8000

The squared cluster scores sum to \(1+1+1+1=4\), and the within instrument-regressor cross-product is \(8/2=4\). Therefore,

$$ \widehat{\operatorname{Var}}_{\mathrm{CR0}}(\hat\beta) =\frac{4}{4^2}=0.25, \qquad SE(\hat\beta)=\sqrt{0.25}=0.500. $$

This standard error is in outcome units per unit of \(x\). It allows arbitrary covariance between the two errors within a unit, but does not correct bias from an invalid instrument, omitted time-varying confounding, or dependence across units.

Testing a Coefficient Restriction

A linear restriction on a scalar coefficient can be tested with a Wald statistic. For \(H_0:\beta=\beta_0\), divide the estimate’s distance from the null value by its standard error. Under the stated cluster-based approximation, compare the statistic with a \(t\) distribution using \(G-1\) degrees of freedom. The two-sided p-value is the probability, under \(H_0\) and this reference approximation, of a statistic at least as extreme in absolute value as the observed one.

Worked Example: Testing Whether the Coefficient Equals One

Test \(H_0:\beta=1\) against \(H_a:\beta\ne1\) at \(\alpha=0.05\). The estimate is 2, its cluster-robust standard error is 0.500, and there are \(G=8\) units. Using the specified \(t_7\) approximation,

$$ t=\frac{\hat\beta-1}{SE(\hat\beta)} =\frac{2-1}{0.500}=2.000, \qquad df=G-1=7. $$

The two-sided p-value is approximately \(0.0855\). Since \(0.0855>0.05\), this analysis does not reject \(H_0\) at the 5% level. It does not establish that \(\beta=1\); it indicates that these data, under the model and approximation, do not provide sufficient evidence against that restriction.

The corresponding 95% interval using \(t_{0.975,7}\approx2.365\) is

$$ 2\ \pm\ 2.365(0.500) =2\pm1.1825 =[0.8175,\ 3.1825]. $$

The interval includes 1, consistent with the test result. Its endpoints are in outcome units per unit of \(x\). The interval procedure does not assign a probability to the fixed parameter being inside this particular interval.

Justification, Diagnostics, and Alternatives

The within calculation follows directly from demeaning the model. Because \(\alpha_i\) is constant over time, subtracting the unit mean gives \(\tilde y_{it}=\beta\tilde x_{it}+\tilde u_{it}\). In the just-identified case, the instrument moment sets the sample cross-product \(\sum_{i,t}\tilde z_{it}(\tilde y_{it}-\hat\beta\tilde x_{it})\) to zero. Solving this equation for \(\hat\beta\) gives the ratio used above. For two periods, the within values are \((-\Delta v_i/2,+\Delta v_i/2)\), so summing their products gives \(\Delta z_i\Delta v_i/2\). This explains both the ratio calculation and why differencing gives the same point estimate in this special two-period setting.

Several checks are essential before interpreting a result. First, verify that \(z\) varies within units and predicts the endogenous regressor after removing fixed effects. An instrument that varies only between units is eliminated by this transformation. Second, examine whether the instrument’s exclusion and exogeneity arguments are credible for the transformed model. In particular, contemporaneous level orthogonality is not enough if cross-period relationships between instruments and errors violate the transformed moment. Third, ensure the cluster definition matches the dependence structure: clustering by unit does not protect against correlation across units, such as a common shock, unless the model or inference strategy addresses it. Finally, assess whether the number of independent clusters supports the approximation used. Eight clusters are few, so the example’s \(t_7\) inference should be treated cautiously, even though the arithmetic is fully specified.

Fixed-effects OLS would use within variation but would generally remain inconsistent if \(x_{it}\) is correlated with \(u_{it}\). Pooled 2SLS does not remove \(\alpha_i\) and can be confounded if the unit effect is related to the regressors or instrument. Fixed-effects 2SLS instead instruments the within-transformed endogenous regressor, relying on valid transformed moments. With exactly two periods, first-difference IV and within 2SLS yield the same coefficient in this simple model, though the presentation and small-sample variance conventions must still be specified.

A common mistake is to argue, “The instrument is uncorrelated with the error in each period, so it is valid after demeaning.” That conclusion does not follow: demeaning combines values across periods, and the transformed error is related to every period’s error. State and defend the moment condition after transformation, or give a stronger cross-period condition that implies it. Another mistake is to use a conventional second-stage OLS standard error. The fitted regressor is generated from the instruments, and that naive formula does not provide the required 2SLS uncertainty. Use a 2SLS sandwich estimator with the appropriate cluster-level scores.

Key takeaway: Fixed-effects 2SLS uses within-unit instrument variation and requires instrument validity for the transformed errors. Cluster-robust inference aggregates score contributions by unit; it does not repair invalid moments, weak identification, or an inadequate number of clusters.

Check Your Understanding

Answer each question, then compare your reasoning with the solutions.

  1. With two periods, what are the within-transformed values when a variable changes from 6 to 10?
  2. Why does an instrument that is constant within each unit fail to identify a within-effects coefficient?
  3. If the cluster scores are \(q_i=(2,-2,0)\) and the instrument-regressor cross-product is 4, calculate the uncorrected cluster-robust standard error.
  4. For \(\hat\beta=1.5\), \(SE(\hat\beta)=0.3\), and the restriction \(H_0:\beta=1\), calculate the Wald \(t\) statistic.

Solutions. (1) The unit mean is 8, so the transformed values are \(-2\) and \(+2\). (2) Demeaning removes a time-constant instrument, leaving \(\tilde z_{it}=0\) and no within first-stage variation. (3) The squared scores sum to \(2^2+(-2)^2+0^2=8\), so the variance is \(8/4^2=0.5\) and the standard error is \(\sqrt{0.5}\approx0.7071\). (4) \(t=(1.5-1)/0.3=1.667\). A p-value additionally requires specifying the reference distribution and degrees of freedom.

Clinical Biostats