Panel data (also known as longitudinal or cross-sectional time-series data) is a dataset in which the behavior of each individual or entity (e.g., country, state, company, industry) is observed at multiple points in time.
Example:
In the above Panel dataset, we have data for variables y, x1, x2, and x3 for each entity (i.e., countries - Angola, Brazil, and China) at multiple points in time (i.e., years - 2018, 2019, and 2020).
When all entities are observed across all times, we call it a balanced panel.
When some entities are not observed in some years, we call it an unbalanced panel.
Panel data enables us to control for individual heterogeneity. That means Panel data allows us to control for variables you cannot observe or measure, like cultural factors or differences in business practices across companies or variables that change over time but not across entities (i.e., national policies, federal regulations, international agreements, etc.)
When we work with panel data in Stata, we need to set the data as a panel first.
We will use an example dataset throughout this tutorial. To get the example dataset, type the following codes in the Stata command window:
use https://dss.princeton.edu/training/Panel101_new.dta
For setting the data as Panel, type:
xtset country year
Stata will give us the following message:
The term “(strongly balanced)” refers to the fact that all countries have data for all years. If, for example, a country does not have data for any year, then the data is unbalanced. Ideally, you would want to have a balanced dataset, but this is not always the case. Nevertheless, you can still run the model.
NOTE: If you get the following error after using xtset:
You need to convert ‘country’ to numeric. To do this, type:
Now you have to use ‘country1’ instead of ‘country’ for xtset declaration. That means you have to type:
xtset country1 year
Data inspection
After setting the data as a panel, you can use the xt command to visualize your variables. For instance, if you want to check how the dependent variable (y) varies over the years across entities (i.e., country A, B, C, etc.), type:
xtline y
Stata will give us the following graph:
We usually use the command sum to get the summary statistics of data. Bur for panel data, we can use xtsum, which has some advantages. For instance, it provides statistics for (i) the variation between different cross-sectional units (e.g., countries) and (ii) the variation within each cross-sectional unit over time. To get the summary stat for selected variables in our dataset, type:
xtsum y x1
Stata will give us the following table:
Interpretation for the variable x1:
Overall: the average value of x1 across the entire dataset is 0.648, the standard deviation is 0.468, and the values of x1 ranges from -0.568 to 1.446 across all observations.
Between: The standard deviation of 0.382 shows how much x1 varies between the cross-sectional units.
Within: The standard deviation of 0.304 indicates the variation of x1 values within each country over the 10 time periods.
This guide discusses two basic methods we commonly use to analyze panel data:
Fixed Effects Method
Random Effects Method
Concept
When using FE, we assume that something within the individual/entity may impact or bias the predictor or outcome variables, and we need to control for this. FE model removes the effects of individual or entity's time-invariant characteristics so we can assess the net effect of the predictors on the outcome variable.
The FE regression model has n different intercepts, one for each entity. These intercepts can be represented by a set of binary variables, and these binary variables absorb the influences of all omitted variables that differ from one entity to the next but are constant over time.
Estimation
(i) Accounting for Entity Fixed Effects
Entity fixed effects account for unobserved heterogeneity across entities (e.g., individuals, firms, countries) that is constant over time but varies between entities. This is the most frequently used model in panel data analysis. Follow the steps below to estimate an entity specific fixed effects model in Stata.
- First, get the example data (ignore this step if you have already opened the dataset in the previous section)
use https://dss.princeton.edu/training/Panel101_new.dta, clear
- Set the dataset as a panel using xtset (ignore this step if you have already set the dataset as a panel)
xtset country year
- Use the following command to estimate your entity specific fixed effects model
xtreg y x1 x2, fe
Note: using the fe option indicates we estimate a fixed effects model.
Stata will give us the following results:
Selected Interpretation
The coefficient of x1 indicates how much of y changes over time, on average per country, when x1 increases by one unit, holding all other variables constant. As the p-value is not less than 0.050, the effect is not statistically significant.
(ii) Accounting for both Entity and Time Fixed Effects
Including both entity (e.g., individuals, firms, states, countries, etc.) and time fixed effects controls for both entity-specific unobserved heterogeneity and common time-specific shocks or trends. Estimate the following model when you want to account for both entity and time specific fixed effects.
xtreg y x1 x2 i.year, fe
Note: adding i.year and fe indicates that we are accounting for both entity and time fixed effects.
Stata will give us the following results:
Selected Interpretation
The coefficient of x1 indicates how much of Y changes over time, on average per country, when x1 increases by one unit, holding all other variables constant. As the p-value is not less than 0.05, the effect is not statistically significant.
The coefficients for the year dummies show the effect of each year relative to the omitted reference year (which is 2011). For instance, in 2014, y increased by 2.92e+09 unit relative to the reference year, with a p-value of 0.059, which is marginally significant at the 10% level.
Test whether We Need to Include Time Fixed Effects
We can check whether we need to include time fixed effects in our model by using the command testparm. The command performs a joint F-test to assess if all years collectively equal zero. Type the following codes:
xtreg y x1 x2 i.year, fe
testparm i.year
Stata will give us the following outputs:
The Prob > F is not < 0.05, so we fail to reject the null that the coefficients for the years are jointly equal to zero. Therefore, we do not need to include time fixed effects for this data.
For more information, type help testparm
(iii) Estimating Fixed Effects using the Least Squares Dummy Variable (LSDV) Approach
When there are a small number of fixed effects to be estimated, it is convenient to just run dummy variable regression for a FE model.
- Use the following dataset (ignore this step if you have already opened the dataset for the previous section)
use https://dss.princeton.edu/training/Panel101_new.dta, clear
- Declare the dataset as a panel using xtset (ignore this step if you have already declared the dataset as a panel)
xtset country year
- Use the following command to estimate your fixed effects model
reg y x1 x2 i.country
Stata will give us the following results:
Notice that the estimated coefficients for x1 and x2 are the same for both the "Entity Fixed Effects" method and the "Least Squares Dummy Variable (LSDV)" method.
(iv) Estimating Entity Fixed Effects Models using 'reghdfe'
If you have a large number of fixed effects relative to the number of observations, use the reghdfe command as it is computationally more efficient. The reghdfe command is also useful if you need to account for multiple fixed effects in your models.
- Use the following dataset available by Stata
webuse nlswork, clear
- Use the following command to estimate your fixed effects model if you want to account for fixed effects for the entity idcode
reghdfe ln_w age ttl_exp tenure not_smsa south , absorb(idcode)
Stata will give us the following results:
Selected Interpretation
The negative coefficient for age indicates that, holding other variables constant, an increase in age is associated with a decrease in the natural log of wages (ln_wage). The p-value of 0.002 suggests that the effect is statistically significant at 1% level.
The positive coefficient for ttl_exp suggests that an increase in total work experience is associated with an increase in ln_wage. The effect is statistically significant at 1% level.
The negative coefficient for south indicates that being in the South region is associated with a decrease in ln_wage. The effect is statistically significant.
Absorbed FE: idcode 4147 indicates that the model accounts for individual-specific effects for 4,147 different idcode categories, controlling for their impact on the dependent variable.
(v) Estimating Entity and Time Fixed Effects Models using 'reghdfe'
- If you have a large number of fixed effects relative to the number of observations, and want to account for both entity and time fixed effects, use the following codes:
reghdfe ln_w age ttl_exp tenure not_smsa south , absorb(idcode year)
Stata will give us the following results:
Selected Interpretation
The negative coefficient for south indicates that being in the South region is associated with a decrease in ln_wage. The effect is statistically significant.
Absorbed FE
idcode 4147 indicates that the model accounts for individual-specific effects for 4,147 different idcode categories, controlling for their impact on the dependent variable.
year indicates that 15 year categories were included, with 1 redundant (likely due to collinearity with other fixed effects), leaving 14 year fixed effects included.
Notes
Including a lagged dependent variable as a regressor in a fixed effects model can introduce bias, a problem often referred to as the "Nickell bias" or "dynamic panel bias." This bias arises because the lagged dependent variable is correlated with the individual-specific effects, violating the assumption of strict exogeneity required for consistent estimation of fixed effects models. In this case, using dynamic panel data models such as the Arellano-Bond or the generalized method of moments (GMM) can provide consistent estimates.
Concept
If individual or entity-specific effects are strictly uncorrelated with the regressors, it may be appropriate to model the individual or entity-specific constant terms as randomly distributed across cross-sectional units. This view would be appropriate if we believe that sampled cross-sectional units were drawn from a large population.
An advantage of using the random effects method is that you can include time-invariant variables (e.g., geographical contiguity, distance between states) in your model. In the fixed effects model, these variables are absorbed by the intercept.
Estimation
- Use the following dataset
use https://dss.princeton.edu/training/Panel101_new.dta, clear
- Declare the dataset as a panel using
xtset country year
- Use the following command to estimate your random effects model
xtreg y x1 x2, re
Note: the use of re option indicates that we are estimating a random effects model.
Stata will give us the following results:
The coefficient of x1 indicates how much of Y changes over time, on average per country, when x1 increases by one unit, holding all other variables constant. As the p-value is not less than 0.05, the effect is not statistically significant.
Hausman Test
- Use the Hausman test to decide whether to use a fixed effects or random effects model.
- Procedures:
- Run a fixed effects model and save the estimates
- Run a random effects model and save the estimates
- Perform the Hausman test
- Use the following Stata commands
xtreg y x1 x2, fe
estimates store fixed
xtreg y x1 x2, re
estimates store random
hausman fixed random
Stata will give us the following results:
...
Decision rule: if the Prob > chi2 (p value) value is < 0.05, use a fixed effects model. In this case, we should use a random effect model as the p-value is = 0.05.
DSS Data Analysis Guides https://libguides.princeton.edu/c.php?g=1415215
Princeton DSS Libguides https://libguides.princeton.edu/dss
Stata Resources https://www.stata.com/features/overview/linear-fixed-and-random-effects-models/
Angrist, J. D., & Pischke, J. S. (2009). Mostly harmless econometrics: An empiricist's companion. Princeton University Press.
Baltagi, B. (2021). Econometric analysis of panel data (6th ed). Springer.
Bartels, B. (2008). "Beyond fixed versus random effects": a framework for improving substantive and statistical analysis of panel, time-series cross-sectional, and multilevel data. The Society for Political Methodology, 9, 1-43. Available at: https://home.gwu.edu/~bartels/cluster.pdf
Baum, C. F. (2006). An introduction to modern econometrics using Stata. Stata Press.
Gelman, A., & Hill, J. (2007). Data analysis using regression and multilevel/hierarchical models. Cambridge University Press.
Greene, W. H. (2018). Econometric analysis (8th ed.). Pearson.
Hamilton, L. C. (2012). Statistics with Stata: version 12. Cengage Learning.
Hoechle, D. (2007). Robust standard errors for panel regressions with cross-sectional dependence. The stata journal, 7(3), 281-312. Available at: https://journals.sagepub.com/doi/pdf/10.1177/1536867X0700700301
Kohler, U., & Kreuter, F. (2012). Data analysis using Stata (3rd ed.). Stata Press.
Stock, J. H., & Watson, M. W. (2019). Introduction to econometrics (4th ed.). Pearson.
Wooldridge, J. M. (2010). Econometric analysis of cross section and panel data. MIT Press.
Wooldridge, J. M. (2020). Introductory econometrics: a modern approach (7th ed). Cengage Learning.
If you have questions or comments about this guide or method, please email data@Princeton.edu.