Analytic weights: When, how, and why
Weights are a staple of empirical work, yet analytic weights (aweights) often suffer from a bit of an identity crisis. They are often misunderstood as a “one-size-fits-all” solution for data that feel weighted or misapplied as a substitute for sampling weights (pweights) when researchers want to adjust for population representation. In this post, we want to clarify this confusion and discuss the when, how, and why of using aweights in your research.
aweights have a very specific, technical job: they are intended for datasets where each observation represents a group average rather than a single individual. A classic example is regressing state-level average test scores on average class sizes. In this scenario, an average score from a state with a million students is fundamentally more precise than one from a state with only a few hundred; by using aweights, you allow Stata to account for that difference in precision, substantially improving the stability and efficiency of your estimator. You can specify aweights for many commands by including [aweight = varname]. See [U] 11.1.6 weight for more details.
Let’s see it work: The homoskedastic case
Suppose that the relationship between a student’s performance, y, and a set of characteristics, \(\mathbf{x}\), is given by
\[
y_{ij} = \mathbf{x}_{ij}\beta + \varepsilon_{ij}
\]
where i indexes students and j indexes school districts. Suppose that the errors \(\varepsilon_{ij}\) are identically distributed, and homoskedastic with a constant variance of \(\sigma^{2}\).
Researchers often do not have access to individual student data. Instead, they have access only to district-level averages \((\bar{y}_{j}\), \(\bar{x}_{j})\) and to the number of students \(w_{j}\) in each district. When we aggregate the student-level model to the district level, we get
\[
\bar{y}_j = \bar{\mathbf{x}}_j\mathbf{\beta} + \bar{\varepsilon}_j
\]
where the new error term \(\bar{\varepsilon}_{j}\) is simply the average of the errors of the students within that district. Therefore, its variance is equal to \(\sigma^{2}/w_{j}\). This makes the district-level model heteroskedastic by definition: the data from a large district are much more precise than data from a small one.
While a standard unweighted regression would still be consistent for \(\mathbf{\beta}\), it would be inefficient because it treats every district as if it provided the same amount of information. On the other hand, by aweighting your regression, you can substantially reduce the variance of your estimator, improve its stability, and derive tighter confidence intervals (CIs). Let’s see this in action with some Monte Carlo simulations.
We simulate data for 5,000 school districts. Each district is assigned a weight, \(w_j\), representing the number of students sampled in the district. This weight takes values from 1 to 50 inclusive, all with equal probability. Thus, our individual-level dataset of students has \(\sum_{j=1}^{5000} w_j\) observations in total. For each student i in district j, we generate two regressors: \(x_{ij} \sim N(0,1)\) and \(z_{ij} \sim \text{uniform}(-2,2)\). We model the individual error using a centered \(\chi^2\) distribution with 5 degrees of freedom and define the outcome as \(y_{ij} = 1 + x_{ij} + z_{ij} + \varepsilon_{ij}\). Finally, we collapse the data to district-level averages to produce the dataset used in the regression.
In each simulated dataset, we run three linear regressions using the regress command: two unweighted regressions—one with default (homoskedastic) standard errors and another with heteroskedasticity-robust standard errors—and an aweighted linear regression with default standard errors. In each simulation, we check the coefficient for \(x_1\), \(\widehat{\beta}\) the width of its CI, and check whether the CI covered the true population parameter \(\beta = 1\). Table 1 summarizes our results.
Table 1: Simulation results: Homoskedastic underlying model (5,000 replications)
| Estimator | Avg. \(\widehat{\boldsymbol\beta}\) | RMSE | CI coverage | Avg. CI width |
|---|---|---|---|---|
| Unweighted (homoskedastic) | 1.010 | 0.822 | 73.7% | 1.854 |
| Unweighted (robust) | 1.010 | 0.822 | 93.4% | 3.238 |
| Weighted (aweight) | 1.007 | 0.462 | 95.1% | 1.860 |
The simulation results highlight the efficiency gains of using aweights. While all three estimators are unbiased—with average \(\widehat{\beta}\) values across 5,000 replications remaining near the true value of 1—the aweighted estimator is substantially more precise, yielding a root mean squared error (RMSE) of 0.462 compared with 0.822 for the unweighted models. The unweighted regression with homoskedastic standard errors fails to correct for the heteroskedasticity in the district-level (averaged) model, resulting in a poor CI coverage of only 73.7%. Although specifying robust standard errors for the unweighted model restores coverage to a near-nominal 93.4%, it does so at a significant cost to precision; the average CI width of 3.238 is nearly double the 1.860 width achieved by the aweighted approach.
Why does this work? The generalized least-squares (GLS) estimator
Because the district-level data consist of averages, the variance–covariance matrix of the error terms is known up to a constant factor. This allows us to implement the GLS estimator—an efficient estimator that is typically infeasible because the exact error variance–covariance matrix is rarely known in practice.
In the district-level data, the said matrix takes the form \(\Omega = \sigma^2 \mathbf{W}^{-1}\), where \(\mathbf{W} = \text{diag}\big(w_1, w_2 \dots w_J\big)\). Therefore, the GLS estimator is given by
\[
\mathbf{\widehat{\beta}}_{\mathrm{GLS}} = \big(\mathbf{X}’ \Omega^{-1} \mathbf{X}\big)^{-1} \big(\mathbf{X}’ \Omega^{-1} \mathbf{y}\big)=\big(\mathbf{X}’ \mathbf{W} \mathbf{X}\big)^{-1} \big(\mathbf{X}’ \mathbf{W} \mathbf{y}\big)
\]
As you can see from the expression on the right, this is nothing more than a weighted linear regression that we can easily implement in Stata using aweights.
As a result of a generalized Gauss–Markov theorem (see Hansen [2022]), the GLS estimator is guaranteed to have a variance no larger than its ordinary least-squares (OLS) counterpart.
Beyond the homoskedastic case
Heteroskedastic individual errors
Can aweights improve efficiency when the underlying individual errors are not homoskedastic? In a case like this, the aweighted regression is not equivalent to the (infeasible) GLS estimator, but giving more weight to observations with a higher precision may still yield efficiency gains. Let’s run some more Monte Carlo simulations to explore this further.
The number of districts, the weights, the controls, and the outcome variable are all defined identically as in the homoskedastic case. We model two heteroskedastic data-generating processes (DGPs) using as error terms \(\varepsilon_1 = \chi^2 – 4 + x_1^2\) and \(\varepsilon_2 = \chi^2 + x_1^2 + x_2^2 – 6\).
Table 2: Simulation results: Heteroskedastic underlying models (5,000 replications)
| Estimator | Avg. \(\widehat{\boldsymbol\beta}\) | RMSE | CI coverage | Avg. CI width |
|---|---|---|---|---|
| Simulation 1 | ||||
| Unweighted (robust) | 1.006 | 0.898 | 93.4% | 3.551 |
| Weighted (aweight) | 1.002 | 0.507 | 95.3% | 2.122 |
| Simulation 2 | ||||
| Unweighted (robust) | 1.010 | 0.949 | 93.2% | 3.832 |
| Weighted (aweight) | 1.008 | 0.549 | 95.7% | 2.292 |
The aweighted estimators produced RMSE values of 0.507 and 0.549, which are roughly 40% lower than the corresponding unweighted RMSEs of 0.898 and 0.949. Furthermore, while both estimators provide valid CI coverage near 95%, the aweighted estimator achieves this with substantially narrower CIs. The average CI widths of the aweighted estimators (2.122 and 2.292) are about 40% smaller than those of the unweighted estimators (3.551 and 3.832). This evidence suggests that even when the underlying DGP is heteroskedastic, aweights can substantially reduce the variance of the estimator and yield more precise statistical inference.
Endogenous regressors
Let’s now see whether aweights can improve efficiency when there is an endogenous regressor. Again, in a case like this, the aweighted regression is not equivalent to the (infeasible) GLS estimator, but giving more weight to observations with a higher precision may still yield efficiency gains.
Again, we simulate data for school districts \(j= 1, \dots5000\), each with \(w_j\) students. In this case, \(w_j\) takes values from 1 to 200 inclusive, all with equal probability. We model an exogenous regressor \(x_{ij}\) and an instrumental variable (IV) \(z_{ij}\), both following independent standard normal distributions. We define the endogenous regressor as \(w_{ij} = \eta_{ij} + z_{ij}\) and the error term as \(\varepsilon_{ij} = \chi^2(5) – 5 + \eta_{ij}\), where \(\eta_{ij} \sim N(0,1)\). Finally, the student-level data are then collapsed to district-level averages \((\bar{y}_j, \bar{x}_{j}, \bar{w}_{j},
\bar{z}_j)\).
For each simulated dataset, we use the ivregress 2sls command to estimate an unweighted IV regression and an aweighted one. For the unweighted regression, we use robust standard errors with option vce(robust); for the weighted regression, we use the default standard errors instead, that is vce(unadjusted). Additionally, before we collapse the data, we run an unweighted IV regression on the student-level data as a benchmark.
Table 3: Simulation results: Two-stage least squares (2SLS) with endogenous regressors (5,000 replications)
| Estimator | Avg. \(\widehat{\boldsymbol\beta}\) | RMSE | CI coverage | Avg. CI width |
|---|---|---|---|---|
| Uncollapsed (benchmark) | 1.000 | — | 95.6% | — |
| Collapsed unweighted (robust) | 0.954 | 0.713 | 87.1% | 2.093 |
| Weighted (aweight) | 0.991 | 0.240 | 95.0% | 0.927 |
The aweighted regression achieved lower RMSE, tighter CIs, and a CI coverage closer to the 95% nominal size.
Clustered errors
Do aweights always improve the efficiency of estimators? Let’s check it out in two DGPs with clustered errors.
The first simulation starts with 5,000 observations grouped into 100 distinct weight-based categories (\(w_j\) from 1 to 100) and 50 clusters. For each cluster, we draw a cluster-specific error component from a standard normal distribution. We then expand the data by the district weight and generate the student-level variables. The error term \(\varepsilon_{ij}\) is constructed as the sum of a centered \(\chi^{2}\) draw and the shared cluster component, ensuring that students within the same cluster have correlated errors. Finally, we calculate the outcome \(y_{ij}\) and collapse the data to the weight-level averages, resulting in a dataset of 100 observations for the regression analysis.
The second clustered-error simulation introduces both endogeneity and a clustered error structure. As before, we begin with an initial sample of 5,000 observations mapped into 200 distinct weight categories (\(w_j\) from 1 to 200) and 50 clusters. A cluster-specific error component is drawn from a standard normal distribution. We then create the student variables, including an instrument \(z_{ij}\). Endogeneity is introduced via a standard normal latent factor (\(\eta_{ij}\)) that determines both the endogenous regressor (\(x_{ij} = \eta_{ij} + z_{ij}\)) and the structural error term [\(\varepsilon_{ij} = \chi^2(5) – 5 + \eta_{ij} + \text{cluster}_{j}\)].
Table 4: Simulation results: Clustered errors context (5,000 replications)
| Estimator | Avg. \(\widehat{\boldsymbol\beta}\) | RMSE | CI coverage | Avg. CI width |
|---|---|---|---|---|
| Case 1: Standard OLS (no endogeneity) | ||||
| Unweighted (robust) | 0.952 | 3.446 | 95.0% | 14.553 |
| Weighted (aweight) | 0.975 | 5.123 | 95.5% | 20.953 |
| Case 2: 2SLS (with endogeneity) | ||||
| Uncollapsed benchmark | 1.000 | — | 94.2% | — |
| Collapsed unweighted | 0.972 | 0.864 | 88.8% | 2.698 |
| Weighted (aweight) | 0.989 | 0.756 | 93.7% | 2.816 |
These two cases reveal that, when we work with clustered errors, the effect of aweights depends on the details of the DGP considered. In the standard OLS framework (case 1), the unweighted estimator is more efficient than the weighted estimator, yielding a lower RMSE of 3.446 versus 5.123 and narrower CIs of 14.553 versus 20.953, while both maintain nominal coverage near 95%. Conversely, in the 2SLS model with endogeneity (case 2), the weighted estimator offers improvements in both precision and inference. Specifically, applying aweights reduces the RMSE from 0.864 to 0.756 and increases CI coverage from 88.8% to 93.7%, closer to the nominal rate. These results indicate that while individual-level clustering can reduce the efficiency of weighted aggregate data in simpler settings, analytic weights can provide corrections to precision and coverage in aggregated IV designs.
Reference
Hansen, B. E. 2022. A modern Gauss–Markov theorem. Econometrica 90: 1283–1294. https://doi.org/10.3982/ECTA19255.