EconStatsCraft › Summary by group

Summary by group

Any statistic you like, for every group in the data - Stata's tabstat and collapse. Supply a survey weight and the means become population means rather than means of whoever was sampled, and the effective sample size is reported so you know what the weighting cost you in precision.

What it needs

The intuition

Collapsing rows to group means is the most-used operation in applied work and the least examined. Two things go wrong quietly. An average of averages is not the overall average unless the groups are the same size, and any relationship visible between groups can be the reverse of the relationship within them.

When to use it

Before any model, to see what the data looks like where it matters. And after one, to check that a result holds in each subgroup rather than only on average.

Where the estimator comes from

For groups g with sizes n_g and means \bar{x}_g, the overall mean is the WEIGHTED average \bar{x}=\sum_g n_g\bar{x}_g/\sum_g n_g, not \frac{1}{G}\sum_g\bar{x}_g. The two coincide only when every n_g is equal. The variance decomposes as \underbrace{\textstyle\sum_g\sum_{i\in g}(x_i-\bar{x})^2}_{\text{total}}=\underbrace{\textstyle\sum_g n_g(\bar{x}_g-\bar{x})^2}_{\text{between}}+\underbrace{\textstyle\sum_g\sum_{i\in g}(x_i-\bar{x}_g)^2}_{\text{within}}, and aggregation keeps only the between part. Everything the within part contained - including any within-group relationship - is discarded, which is why a regression on group means answers a different question from a regression on the rows.

The model

\bar{y}_g=\frac{\sum_{i\in g} w_i y_i}{\sum_{i\in g} w_i}\qquad n^{\text{eff}}=\frac{\left(\sum w_i\right)^2}{\sum w_i^2}

What it assumes

Statistical properties

How to read the output

A worked reading

Two groups, one with 900 rows averaging 10 and one with 100 rows averaging 20. The unweighted mean of the group means is 15; the actual overall mean is (900 x 10 + 100 x 20)/1000 = 11. A five-point error, from an operation that felt like arithmetic rather than a modelling choice. And if the small group is systematically different in some other way - which is usually why it is a separate group - then any regression run on the two group means has two observations and answers a question about groups rather than about the underlying units.

Symptom → diagnosis → what to do

If you see thisIt meansDo this
Group mean of means differing from the overall meanUnequal group sizesWeight by size. Correct, not a bug
R-squared jumping after aggregatingWithin-group noise removedNot a better model; a different question
A relationship reversing on aggregationSimpson's paradoxShow both levels; neither is automatically right
Averaging percentagesThe denominators differSum numerators and denominators, then divide
Aggregated mediansNot computable from group mediansAggregate from the raw rows
One group with a single observationIts mean has no varianceReport group sizes alongside

Common mistakes

Where to read this properly

See also

Pivot tables, for the two-way version of the same summary. Panel regression, which keeps the within variation aggregation discards.

Related Explore

Descriptive statistics · Two-way table · Classical statistical tests

All analyses

Charts

Line chart · Area chart · Bar chart · Stacked bar · Scatter plot · Bubble chart · Histogram · Box plot · Pie chart · Heat map · Rebased to 100 · Lorenz curve and Gini

Maps

World map · Bivariate map · Flow map · India - state map · India - district map

Explore

Descriptive statistics · Summary by group · Two-way table · Classical statistical tests

Regression

Multiple regression (OLS) · Logit / Probit · Tobit (censored) · Truncated regression · Multinomial logit · Ordered logit / probit · Quantile regression · Poisson / Negative binomial

Time series - one series

Time-varying coefficients (Kalman) · Unit root tests · Correlogram (ACF / PACF) · ARIMA / SARIMA · Rolling-origin cross-validation · Theta method · Unobserved components · STL forecasting · Forecast combination · Markov regime switching · Change frequency (resample) · ARCH / GARCH volatility · Decompose & filter · Spectral analysis · Exponential smoothing · Forecast accuracy

Time series - several series

Yield curve (Nelson–Siegel / Svensson) · Dynamic factor model · Dynamic regression (ARIMAX) · Structural VAR · VAR · Granger causality · Cointegration · VECM · ARDL & bounds test

Panel data

Panel regression · Panel cointegration tests · Heterogeneous panel: Mean Group & PMG · Gravity model of trade · Dynamic panel GMM · Panel diagnostics · Panel unit-root tests

Multivariate statistics

Principal components (PCA) · Factor analysis · Cluster analysis

Marketing research

Conjoint analysis · MaxDiff (best–worst scaling) · TURF - reach of a product line · Price sensitivity (Van Westendorp) · Market basket analysis · Perceptual map · Scale reliability (Cronbach’s alpha) · Key driver analysis

Causal

Spatial autocorrelation · Spatial regression · Heckman selection · Instrumental variables (2SLS) · Difference-in-differences · Staggered difference-in-differences · Regression discontinuity · Matching & weighting · Oaxaca–Blinder decomposition

Finance & risk

Black–Scholes option pricing · Bond price, duration & convexity · Merton distance to default · EMI / home loan planner · CAPM / beta estimation · Multi-factor model · Portfolio optimization · Value at Risk & Expected Shortfall · Performance & drawdown · Time value of money & NPV/IRR · Equity valuation (DDM, FCF, residual income) · Cost of capital (WACC) · Binomial option pricing · Option strategies (payoff & profit) · Forwards, futures & swaps · Credit risk & Basel capital · Extreme value theory (tail risk)

Actuarial

Life table · Life annuity · Life insurance pricing · Chain ladder reserving · Buhlmann credibility · Survival analysis · Mortality graduation · Aggregate loss model · Ruin probability · Multi-state Markov model · Loss severity fitting

Tests & breaks

Monte Carlo experiment · F-test, Wald & Chow · Structural breaks & stability

Machine learning

Regression vs machine learning · Ridge / Lasso / Elastic Net · Random forest · Gradient boosting

Open the workspace →

Starting engine…

EconStatsCraft rigorous, plain-spoken econometrics

Econometrics that explains itself.

Regression, panel data, time series and the whole battery of tests. Every result is followed by a verdict you can defend.

Load a dataset to see it here.