Why Transform Before 2SLS?
Panel data follow the same units—such as patients, clinics, or firms—over multiple periods. A unit may have stable characteristics that affect both the outcome and an explanatory variable. If those characteristics are unobserved, a model with a unit-specific effect can account for them without requiring them to be independent of the regressors. In fixed-effects 2SLS, the key is to remove that unit effect from the outcome, regressors, and instruments before carrying out 2SLS.
Let \(i=1,\ldots,G\) index units and \(t=1,\ldots,T_i\) index observed periods for unit \(i\). Let \(y_{it}\) be the outcome, \(x_{it}\) a vector of endogenous regressors, \(w_{it}\) a vector of included exogenous controls, and \(z_{it}\) a vector of excluded instruments. A linear model with unit fixed effects is
The unit effect \(\alpha_i\) can be associated with \(x_{it}\) and \(w_{it}\). The structural error is \(u_{it}\). The instruments must be relevant for the endogenous regressors after removing unit effects, and their within-unit variation must satisfy the required exogeneity condition. In particular, the identifying moment condition is that the transformed instruments are uncorrelated with the transformed structural errors, in the population:
The transformation applies to each variable that appears in the model, including the instruments and included controls. The demeaned error is \(u_{it}^{*}=u_{it}-\bar u_i\). The fixed effect disappears because \(\alpha_i-\bar\alpha_i=0\). Thus the transformed model is \(y_{it}^{*}=x_{it}^{*'}\beta+w_{it}^{*'}\gamma+u_{it}^{*}\). For an unbalanced panel, each mean is over the periods observed for that unit.
Assumptions and the Within 2SLS Estimator
The procedure relies on several distinct conditions. First, the linear structural equation must be correctly specified for the parameter being estimated. Second, the instruments must be exogenous for the transformed equation: their within-unit variation must not systematically covary with the transformed structural error. This condition is substantive; demeaning removes the time-constant \(\alpha_i\), but does not make an otherwise invalid instrument valid. Third, the transformed instruments must be relevant and numerous enough to identify the endogenous coefficients. Specifically, the population matrix of instrument-regressor cross-moments must have full column rank after accounting for included controls. Finally, for the usual large-sample cluster-robust inference, there must be many independent (or suitably weakly dependent) units, with dependence allowed within a unit.
To state the estimator, stack the transformed observations across units and periods. Let \(R=[X^{*},W^{*}]\) be the matrix of all structural regressors and \(Q=[Z^{*},W^{*}]\) the matrix of instruments, including the included exogenous controls as their own instruments. With \(P_Q=Q(Q'Q)^{-1}Q'\), the 2SLS estimator is
This expression requires the relevant matrices to have full rank. In implementation, the first stage projects each column of \(X^{*}\) on all columns of \(Q\); the second stage uses those projected endogenous regressors together with \(W^{*}\). The expression describes the 2SLS estimator, not a recommendation to treat fitted values as observed data when calculating standard errors.
Worked Example: Demeaning Removes a Stable Unit Effect
Suppose a hypothetical unit has two observations. Its outcome is \(y=(10,14)\), its endogenous regressor is \(x=(4,6)\), and its instrument is \(z=(1,3)\). The unit effect is constant across the two observations, even if its value is unknown.
The unit means are \(\bar y=12\), \(\bar x=5\), and \(\bar z=2\). Subtract each mean from the corresponding observations:
If the original outcome equation is \(y_{it}=\alpha_i+\beta x_{it}+u_{it}\), its transformed first observation is \(10-12=\beta(4-5)+(u_{i1}-\bar u_i)\). The \(\alpha_i\) term is absent because \(\alpha_i-\alpha_i=0\). The same cancellation holds for the second observation. The transformed instrument is not constant, so it retains within-unit variation that could identify \(\beta\), provided it is relevant and exogenous for the transformed error.
A Numerical Fixed-Effects 2SLS Estimate
With one endogenous regressor and one excluded instrument, and no additional controls, the exactly identified within estimator is a ratio of cross-products. Each row below gives the demeaned value in period 1 for one unit; because there are two periods per unit, the period-2 demeaned values are their negatives. All values are hypothetical.
| Unit | \(x_i^{*}\), period 1 | \(z_i^{*}\), period 1 | \(y_i^{*}\), period 1 |
|---|---|---|---|
| 1 | 1 | 1 | 3 |
| 2 | 2 | 1 | 2 |
| 3 | 1 | 2 | 4 |
| 4 | 3 | 2 | 5 |
Worked Example: Calculating the Within 2SLS Coefficient
For a single instrument and a single endogenous regressor, the estimator is \(\hat\beta=\sum_{i,t}z_{it}^{*}y_{it}^{*}/\sum_{i,t}z_{it}^{*}x_{it}^{*}\). The negative period-2 values contribute the same products as period 1, so both sums are twice the sums shown in the table.
The estimate is 2.091 outcome units per one-unit increase in \(x\). This interpretation is a within-unit IV interpretation: the coefficient is identified from instrument-related changes over time within units. It is not a comparison of units with different average instrument values. The calculation alone does not establish that the instrument is valid or sufficiently strong.
Unit-Clustered Inference
Demeaning does not generally make observations from the same unit independent. The transformed errors can remain related across periods, and the within transformation itself induces dependence. Unit-clustered inference allows arbitrary covariance among observations within a unit while relying on independence, or an appropriate large-sample approximation, across units.
For the stacked transformed model, define the cluster instrument score \(s_i=Q_i'\hat u_i^{*}\), where \(Q_i\) and \(\hat u_i^{*}\) contain the transformed instruments and residuals for unit \(i\). The cluster-robust sandwich covariance estimator is
This is the uncorrected cluster sandwich; software may apply a finite-sample multiplier, and conventions for that adjustment differ. The cluster scores are calculated from the 2SLS residuals in the transformed structural equation. Clustering at the unit level is appropriate when the plausible dependence is within units. It does not account for arbitrary dependence across units, such as a common shock affecting all units in a period.
Worked Example: Computing a Unit-Clustered Standard Error
Use the four-unit example above. For unit \(i\), let \(a_i\) denote its period-1 demeaned values. Its period-2 values are \(-a_i\). The period-1 residual is \(\hat e_i=y_i^{*}-\hat\beta x_i^{*}\), and the period-2 residual is \(-\hat e_i\). With one instrument, the cluster score is \(s_i=2z_i^{*}\hat e_i\).
Using \(\hat\beta=23/11\), the period-1 residuals for units 1 through 4 are \(10/11,-24/11,21/11,-14/11\), respectively. The corresponding cluster scores are \(20/11,-48/11,84/11,-56/11\). The scalar bread term is \(\sum_{i,t}z_{it}^{*}x_{it}^{*}=22\). Therefore the uncorrected cluster variance is
This standard error allows the two observations from each unit to have dependent errors. It is an asymptotic cluster-robust estimate, not an exact small-sample standard error. There are only four clusters here, far fewer than would ordinarily support reliable cluster-asymptotic inference; the numerical result illustrates the calculation, not a recommendation to trust inference from so few units.
Diagnostics, Alternatives, and Limitations
The most important diagnostic question is whether the identifying variation survives the within transformation. A variable that never changes within a unit has \(v_{it}^{*}=0\), so it cannot identify a coefficient in a unit fixed-effects model. A time-invariant instrument is eliminated in the same way. Even when an instrument varies, its variation may be only weakly related to the transformed endogenous regressor. Assess relevance in the transformed first stage; a strong relationship in the untransformed data can be driven entirely by between-unit differences and offer little within-unit identification.
Other warning signs include very few independent units for clustered inference, substantial missingness that changes which periods contribute to unit means, and plausible common shocks across units that unit clustering cannot accommodate. Instrument validity also requires substantive justification: removing \(\alpha_i\) does not remove time-varying confounding or reverse causality between the instrument and the structural error.
Pooled 2SLS uses both between- and within-unit variation and does not remove unit effects by demeaning; it is not a substitute when those effects are correlated with regressors. Fixed-effects OLS removes the unit effects but does not address endogeneity of \(x_{it}\). Random-effects approaches impose restrictions on the relationship between unit effects and regressors that fixed effects do not require. First differences also eliminate a time-constant unit effect; with two periods, first differencing and within demeaning carry equivalent identifying variation up to scaling, but their forms differ with more periods. The choice among these approaches depends on the model and the source of credible identifying variation.
Common mistake: Demean \(y\) and \(x\), but leave \(z\) in levels, then run 2SLS. This does not estimate the intended fixed-effects IV model: the instrument retains its unit-level component even though the outcome and regressor have had theirs removed. Transform the instruments and included controls as well, and then perform both stages using the transformed variables.
Check Your Understanding
Answer each question, then compare with the solutions.
- A unit's instrument is constant over time. Can it identify a coefficient in a unit fixed-effects 2SLS model? Why?
- Why must the instruments be demeaned along with the outcome and regressors?
- In a one-instrument, one-endogenous-regressor model, suppose the within cross-product sum \(\sum z_{it}^{*}y_{it}^{*}=18\) and \(\sum z_{it}^{*}x_{it}^{*}=6\). What is the 2SLS coefficient?
- What dependence does unit-clustered inference allow, and what kind of dependence does it fail to address?
Solutions. (1) No. A time-constant instrument has \(z_{it}^{*}=0\) for every period, so it provides no within-unit variation. (2) Otherwise the instrument retains between-unit variation that has been removed from the structural variables, so the resulting estimator is not the within fixed-effects IV estimator. (3) The exactly identified estimate is \(18/6=3\) outcome units per unit of the regressor. (4) It allows arbitrary dependence among observations from the same unit; by itself it does not allow arbitrary dependence across units, such as a common shock shared by all units.