
When analyzing data with repeated measurements or grouped observations—students within schools, patients within hospitals, or countries over time—researchers face a fundamental modeling decision: should group-level differences be treated as fixed or random effects? This choice shapes everything from parameter estimation to the conclusions a study can support.
Fixed effects treat each group’s influence as a distinct, unknown constant to be estimated directly, making them ideal when the groups themselves are the entire population of interest. Random effects, by contrast, treat group-level variation as drawn from a larger probability distribution, allowing researchers to generalize findings beyond the specific groups sampled.
Mixed models, which combine both approaches, have become standard tools in fields ranging from psychology to econometrics. Understanding when to apply each method—and why the wrong choice can produce misleading standard errors or biased estimates—is essential for anyone working with hierarchical or longitudinal data.
A fixed effect is a variable whose specific levels are of direct interest to the researcher and are treated as constant, unknown parameters to be estimated. Rather than assuming these levels come from a broader population, fixed effects models assume the categories observed in the data are the only categories that matter for the analysis.
The Core Idea
Consider a study examining test scores across four specific teaching methods: lecture-based, flipped classroom, project-based, and hybrid. If a researcher is only interested in comparing these four particular methods—not generalizing to “all possible teaching methods”—each method is modeled as a fixed effect.
The general form of a fixed effects regression model is:
Where:
Each αi absorbs all time-invariant characteristics of unit i, whether or not those characteristics are measured.
Worked Example
Suppose a researcher tracks quarterly sales performance for five specific regional offices over two years. Because these are the only five offices the company operates—not a sample representing hundreds of possible offices—the office identifier is treated as a fixed effect. Each office gets its own intercept term ( through ), capturing unmeasured, office-specific factors like local market conditions or management style that remain stable over time.
When Fixed Effects Make Sense
Fixed effects are appropriate when:
| Condition | Explanation |
|---|---|
| Groups are the entire population | Not a sample from a larger population |
| Interest lies in specific group differences | You want to know how Office A differs from Office B |
| Controlling for unobserved heterogeneity | Removes bias from time-invariant confounders |
| Small number of groups | Estimating many fixed effects with few groups is more stable |
Fixed effects models are widely implemented using tools like R’s plm package or Stata’s xtreg, fe command, both of which handle the within-group transformation automatically.
A random effect is a variable whose levels are treated as a random sample drawn from a larger population of possible levels, rather than as fixed, unique quantities to be individually estimated. Instead of assigning each group its own separate parameter, random effects models assume group-level differences follow a probability distribution—typically normal—centered around an overall mean.
The Core Idea
Random effects models are built on the assumption that the observed groups are just one possible draw from a much larger set of groups that could have been sampled. This allows conclusions to extend beyond the specific groups in the dataset to the broader population they represent.
The general form of a random effects regression model is:
Where:
Unlike αi in the fixed effects model, ui is not a fixed constant but a random variable, and the model estimates its variance rather than a separate value for every group.
Worked Example
Suppose a researcher studies quarterly sales performance, but this time across fifty regional offices randomly selected from a national chain of several hundred locations. Since these fifty offices represent a sample rather than the complete population of interest, office identity is treated as a random effect. The model estimates how much variance in sales is attributable to differences between offices (σu2), without spending a separate parameter on each one.
When Random Effects Make Sense
Random effects are appropriate when:
| Condition | Explanation |
|---|---|
| Groups are a sample | Represent a larger population of possible groups |
| Interest lies in overall variance, not specific groups | You want to know how much offices vary, not how Office A differs from Office B |
| Generalizing beyond the sample | Findings should extend to unobserved groups |
| Large number of groups | Estimating one variance term is more efficient than many intercepts |
Random effects models are commonly fit using R’s lme4 package or Stata’s xtreg, re command, both of which use maximum likelihood or restricted maximum likelihood (REML) to estimate the variance components.
Comparison Table
| Aspect | Fixed Effect | Random Effect |
|---|---|---|
| Nature of groups | Entire population of interest | Sample from a larger population |
| Parameter treatment | Each group gets its own estimated intercept | Group differences modeled as a variance component |
| Statistical goal | Estimate specific group effects | Estimate variance across groups |
| Generalizability | Limited to groups in the dataset | Extends to the broader population |
| Degrees of freedom | Uses one per group (can be costly with many groups) | Uses one parameter for variance, regardless of group count |
| Handles time-invariant confounders | Yes, automatically absorbed | Only if uncorrelated with predictors |
| Best suited for | Small number of groups, specific comparisons | Large number of groups, hierarchical or nested data |
| Common software | plm (R), xtreg, fe (Stata) | lme4 (R), xtreg, re (Stata) |
The Correlation Assumption
The most consequential technical difference lies in an assumption about correlation. Random effects models assume that the group-level effect () is uncorrelated with the explanatory variables (). Fixed effects models make no such assumption—they allow to correlate freely with the predictors, which is precisely why they’re effective at controlling for unobserved, group-specific confounders.
This distinction matters in practice. If an unmeasured characteristic of a group (say, management quality at a regional office) influences both the outcome and the predictor variables, a random effects model will produce biased estimates, while a fixed effects model will not.
Testing Which Model Fits: The Hausman Test
Researchers rarely choose between fixed and random effects on theoretical grounds alone. The Hausman test offers a formal statistical comparison:
The null hypothesis states that the random effects estimator is consistent (i.e., no correlation between and ). A statistically significant result (typically p<0.05) suggests the random effects assumption is violated, favoring the fixed effects model instead. This test is available in both R (via the phtest function in the plm package) and Stata (hausman command).
Practical Trade-offs
| Consideration | Favors Fixed Effects | Favors Random Effects |
|---|---|---|
| Sample size of groups | Small (fewer than ~20-30) | Large |
| Interest in group-level predictors | No (these get absorbed) | Yes (can be included) |
| Concern about omitted variable bias | High | Low |
| Efficiency of estimates | Lower (more parameters) | Higher (fewer parameters) |
Neither approach is universally superior—the right choice depends on the research question, the structure of the data, and the assumptions a researcher is willing to defend.
Mathematical Foundation
The fixed effects model builds on a simple but powerful idea: subtract each unit’s average from its observations, removing anything that doesn’t vary within that unit. Starting with the basic model:
Taking the average over time for each unit :
Subtracting the second equation from the first eliminates entirely, since it doesn’t change over time:
This is known as the within transformation, and it’s the mathematical mechanism that allows fixed effects models to control for unobserved heterogeneity without ever directly estimating .
Three Ways to Estimate Fixed Effects
| Method | Description | Best Used When |
|---|---|---|
| Least Squares Dummy Variable (LSDV) | Includes a dummy variable for each unit directly in the regression | Small number of units |
| Within (Demeaning) Estimator | Subtracts unit-specific means before running the regression | Large number of units |
| First-Differencing | Uses changes between consecutive time periods instead of demeaning | Two time periods, or when serial correlation is a concern |
All three methods produce mathematically identical coefficient estimates for under standard conditions, though standard errors can differ slightly.
Worked Example
Returning to the five regional offices tracked over two years: a fixed effects model would estimate the effect of, say, advertising spend on sales while automatically controlling for anything unique and unchanging about each office—local competition, store layout, or customer demographics. The within transformation removes each office’s average sales and average advertising spend, leaving only the year-to-year fluctuations to estimate the relationship.
Strengths and Limitations
| Strengths | Limitations |
|---|---|
| Controls for all time-invariant confounders, measured or not | Cannot estimate effects of time-invariant variables (e.g., gender, region) |
| No distributional assumptions about αi | Reduces degrees of freedom with many units |
| Consistent even when αi correlates with predictors | Less efficient than random effects when assumptions hold |
| Well-suited for panel data with few, specific units | Cannot generalize findings beyond the units studied |
Implementation
Fixed effects models are straightforward to implement across common statistical software:
model = "within"xtreg depvar indepvars, fePanelOLS function from the linearmodels packageDiagnostic checks—such as testing for serial correlation or heteroskedasticity in the residuals—are typically recommended after fitting, since panel data often violates the classical regression assumption of independent errors.
Mathematical Foundation
The random effects model starts from the same basic structure as the fixed effects model, but treats the group-specific term differently:
Here, ui is not a fixed constant but a random variable drawn from a distribution:
Because both ui and ϵit contribute to the total error, the composite error term becomes:
This composite structure creates correlation between error terms within the same group over time, since is shared across all observations of unit . The model accounts for this using Generalized Least Squares (GLS), which down-weights observations based on the ratio of between-group to within-group variance, captured by the parameter :
Where T is the number of time periods per unit. When approaches 1, the model behaves similarly to the within (fixed effects) estimator; when approaches 0, it behaves like simple pooled OLS.
Estimation Methods
| Method | Description | Best Used When |
|---|---|---|
| Generalized Least Squares (GLS) | Weights observations using estimated variance components | Variance components are known or estimated in a first stage |
| Maximum Likelihood (ML) | Estimates variance components and coefficients jointly | Sample sizes are moderate to large |
| Restricted Maximum Likelihood (REML) | Adjusts ML to reduce bias in variance estimates | Preferred default in most modern software |
REML has become the standard approach in packages like lme4 because it produces less biased estimates of variance components, particularly when the number of groups is small.
Worked Example
Consider the fifty regional offices sampled from a national chain of several hundred locations. A random effects model estimates the relationship between advertising spend and sales while also estimating , the variance in baseline sales attributable to differences between offices. Because these offices are treated as a sample, the model’s conclusions about the advertising-sales relationship are understood to generalize to the full population of offices, not just the fifty observed.
Unlike the fixed effects version, this model can also include time-invariant, office-level predictors, such as whether an office is located in an urban or rural area, since ui is not absorbing all office-specific variation into a separate intercept.
Strengths and Limitations
| Strengths | Limitations |
|---|---|
| Can include time-invariant predictors | Requires ui to be uncorrelated with regressors |
| More efficient than fixed effects when assumptions hold | Biased if unobserved heterogeneity correlates with predictors |
| Generalizes findings to the broader population | Relies on distributional assumptions about ui |
| Fewer parameters estimated, more degrees of freedom | Less appropriate with a small number of groups |
Implementation
Random effects models are supported across the same major platforms used for fixed effects:
model = "random", or lme4 for more flexible hierarchical structuresxtreg depvar indepvars, reRandomEffects function from the linearmodels package, or mixedlm in statsmodelsAs with fixed effects, it’s good practice to check the residual structure after fitting, since violations of the normality or independence assumptions for can undermine the validity of standard errors and confidence intervals.
