Why Random-Intercept GLS Uses Quasi-Demeaning
In panel data, observations from the same unit can share an unobserved component. A random intercept represents that shared component, so the resulting errors are correlated within units. Ordinary least squares on the untransformed observations does not account for this covariance pattern. Generalized least squares (GLS) does so by changing the scale of the common, between-unit direction relative to the within-unit directions. In this model, that operation has a particularly simple form: subtract a fraction of each unit’s time average from each observation.
The fraction is neither arbitrary nor generally equal to zero or one. It is determined by the variance of the random intercept, the variance of the idiosyncratic error, and the number of observations for that unit. Deriving it makes clear both why the transformation works and what its limiting cases mean.
Model and Assumptions
For unit \(i=1,\ldots,N\) and period \(t=1,\ldots,T_i\), consider the random-intercept model
Here \(y_{it}\) is the outcome, \(x_{it}\) is a vector of observed regressors, and \(\beta\) is the coefficient vector. The unit-specific random intercept \(a_i\) is shared across periods for unit \(i\); \(\epsilon_{it}\) is the period-specific error. The panel may be unbalanced, so the number of observed periods \(T_i\) can vary across units. Define unit averages by \(\bar y_i=T_i^{-1}\sum_{t=1}^{T_i}y_{it}\) and \(\bar x_i=T_i^{-1}\sum_{t=1}^{T_i}x_{it}\).
For the covariance structure and transformation below, assume \(E[a_i]=0\), \(\operatorname{Var}(a_i)=\sigma_a^2\), \(E[\epsilon_{it}]=0\), and \(\operatorname{Var}(\epsilon_{it})=\sigma_\epsilon^2\). Assume idiosyncratic errors are uncorrelated across periods and units, and are uncorrelated with the random intercepts. Random intercepts are uncorrelated across units. Thus, within a unit, the composite error \(u_{it}=a_i+\epsilon_{it}\) has covariance matrix
For unit \(i\)'s transformed errors to have zero conditional mean given \(X_i\), also require \(E[a_i\mid X_i]=0\) and \(E[\epsilon_{it}\mid X_i,a_i]=0\), where \(X_i\) collects the regressors for unit \(i\). These exogeneity conditions are distinct from the covariance assumptions: a correct covariance transformation does not by itself guarantee that the regressors are exogenous.
Deriving the Transformation Parameter
Let \(P_i=\mathbf{1}_{T_i}\mathbf{1}_{T_i}'/T_i\), the projection onto the direction that is constant across unit \(i\)’s observed periods. The covariance matrix has eigenvalue \(\sigma_\epsilon^2\) in every direction orthogonal to \(\mathbf{1}_{T_i}\), and eigenvalue \(\sigma_\epsilon^2+T_i\sigma_a^2\) in the constant direction. A transformation that leaves the within-unit directions unchanged and multiplies the constant direction by \(1-\theta_i\) is \(Q_i=I_{T_i}-\theta_iP_i\). Since \(P_i\) is a projection, \(Q_i\) acts as the identity on the within directions and as multiplication by \(1-\theta_i\) on the constant direction.
To make the transformed covariance equal to \(\sigma_\epsilon^2 I_{T_i}\), choose \(1-\theta_i\) so that the variance in the constant direction is reduced to \(\sigma_\epsilon^2\). The resulting equation and solution are
The positive square root is used so that \(1-\theta_i\) is positive. Equivalently, \(Q_i\operatorname{Var}(u_i)Q_i'=\sigma_\epsilon^2I_{T_i}\). Also, \(P_i y_i\) is the vector whose entries are all \(\bar y_i\), so \(Q_i y_i\) has entries \(y_{it}-\theta_i\bar y_i\). The identical operation on each regressor gives the stated quasi-demeaned regression. Because the transformed errors have the same scalar covariance within every unit, least squares on all transformed observations is GLS when the variance components are known.
Worked Transformations
Worked Example: Transforming One Balanced Unit
Suppose a balanced panel has \(T=3\), with \(\sigma_a^2=4\) and \(\sigma_\epsilon^2=6\). For one hypothetical unit, the three outcomes are \(10,14,16\), and a single regressor takes values \(1,2,4\). Calculate the quasi-demeaned observations.
First, the transformation parameter is
The unit means are \(\bar y=(10+14+16)/3=13.3333\ldots\) and \(\bar x=(1+2+4)/3=2.3333\ldots\). Use the unrounded expression for \(\theta\) when calculating. The amount subtracted from each outcome is
The second value is the amount subtracted from each regressor observation. Therefore, to four decimal places, the transformed outcomes are \(4.3647, 8.3647, 10.3647\), and the transformed regressor values are \(0.0138, 1.0138, 3.0138\). For instance, the first transformed outcome is \(10-5.6353297\ldots=4.3646703\ldots\), and the first transformed regressor is \(1-0.9861827\ldots=0.0138173\ldots\). Rounding \(\theta\) before multiplying can create small inconsistencies in these results, so retain precision through the calculation and round the final values.
Worked Example: Different Panel Lengths, Different Transformations
Suppose \(\sigma_\epsilon^2=4\) and \(\sigma_a^2=1\). Unit A has two observed periods, with outcomes \(5,9\) and regressor values \(2,6\). Unit B has four observed periods, with outcomes \(3,4,6,7\) and regressor values \(1,2,3,5\). Calculate each unit’s parameter and transformed values.
For unit A, \(T_A=2\), so
Its means are \(\bar y_A=7\) and \(\bar x_A=4\). Thus the transformed outcomes are \(5-\theta_A(7)=3.7155\) and \(9-\theta_A(7)=7.7155\); the transformed regressor values are \(2-\theta_A(4)=1.2660\) and \(6-\theta_A(4)=5.2660\).
For unit B, \(T_B=4\), giving
Here \(\bar y_B=5\) and \(\bar x_B=2.75\). The amount subtracted from each outcome is \(\theta_B(5)=1.4645\), and the amount subtracted from each regressor value is \(\theta_B(2.75)=0.8055\), to four decimal places. The transformed outcomes are therefore \(1.5355, 2.5355, 4.5355, 5.5355\), and the transformed regressor values are \(0.1945, 1.1945, 2.1945, 4.1945\). Unit B has a larger \(\theta_i\) because its longer panel makes the shared intercept contribute more variance to the unit’s common direction.
Worked Example: Transforming the Intercept in an Unbalanced Panel
Suppose a regression includes a constant and the variance components are \(\sigma_\epsilon^2=9\) and \(\sigma_a^2=3\). Unit C has \(T_C=1\), while unit D has \(T_D=4\). What happens to the constant column?
A constant regressor equals one in every period, so its unit mean is also one. For unit C,
The transformed constant entry for that unit is \(1-\theta_C=0.8660\ldots\). For unit D,
Every transformed constant entry for unit D is \(0.6547\ldots\). Since the panel is unbalanced, these two transformed values differ. The constant column must therefore be transformed along with the other design columns; it is not generally an unchanged column of ones. In a balanced panel, all units have the same \(T_i\), so the transformed constant is the same nonzero value in every row and remains proportional to an ordinary constant column.
What the Transformation Retains
The decomposition into a unit’s average and its deviations from that average clarifies the transformation’s meaning. Write an observation as \(y_{it}=(y_{it}-\bar y_i)+\bar y_i\). Then
The within-unit deviation is retained in full, while the unit mean is multiplied by \(1-\theta_i\). When \(\theta_i=0\), both within and between variation remain unchanged, as in pooled least squares. As \(\theta_i\) approaches one, the between-unit component is increasingly attenuated; at \(\theta_i=1\), only deviations from unit means remain. Quasi-demeaning is therefore not simply “subtracting the random intercept”: the intercept is unobserved, and the transformation instead rescales the common direction in the way required by the covariance matrix.
This distinction matters for interpretation and estimation. Intermediate quasi-demeaning uses both within- and between-unit variation, with their relative contribution shaped by the covariance parameters. It does not, by itself, resolve possible correlation between \(a_i\) and the regressors. If the random-intercept exogeneity condition fails, the transformation can still whiten the specified covariance matrix, but the resulting coefficient estimator need not have the desired conditional mean.
Common Mistakes
- Using a single parameter in an unbalanced panel. Since \(\theta_i\) depends on \(T_i\), units with different numbers of observed periods generally require different transformations.
- Transforming only the outcome. GLS requires applying the same operation to every regressor and to any constant column. Otherwise the transformed equation is not the original model’s GLS regression.
- Calling quasi-demeaning full demeaning. Unless \(\theta_i=1\), the transformed observation still contains \((1-\theta_i)\bar y_i\), so between-unit information has not been removed entirely.
- Rounding too early. A rounded \(\theta_i\) may not reproduce displayed products or transformed values. Keep full precision during multiplication and round the final results consistently.
- Confusing covariance correction with exogeneity. The transformation handles the stipulated within-unit covariance. It does not make a random intercept uncorrelated with regressors if that assumption is false.
Check Your Understanding
Try these questions before reviewing the derivation and examples.
- For fixed positive variance components, how does increasing \(T_i\) affect \(\theta_i\), and what does that imply about the unit mean?
- If \(\sigma_a^2=0\), what is \(\theta_i\), and how does the transformation change the observations?
- A unit has \(T_i=2\), \(\sigma_\epsilon^2=5\), and \(\sigma_a^2=1\). Write the formula for its \(\theta_i\) and calculate it to four decimal places.
- Why must the constant column be transformed in an unbalanced panel?
- Does correct quasi-demeaning guarantee that the random-intercept estimator is unbiased if \(a_i\) is correlated with the regressors? Explain.