Panel data regression: a powerful time series modeling technique | by Mahbubul Alam | Towards Data Science

In a previous piece, I briefly mentioned about panel data models; and in this post, I am getting a bit deeper into it with some technical details. As I said in that post, econometrics has some of the most important tools for data scientists’ toolbox. It has numerous use cases — from measuring impacts of temperature variability on agriculture to time series data modeling and forecasting.
So what exactly is panel data? First, let’s take a look at the following definition:
A panel data is a multi-dimensional data of an observation that is measured repeatedly over time.
This is a one-liner definition, but a lot to unpack from. This definition implicitly describes three key properties of a panel dataset:
property 1: the same objects/individuals are observed repeatedly
property 2: multiple variables are measured of those same individuals/objects
property 3: the observations take place at multiple points in time
To illustrate these properties the following is a hypothetical (I made it up on the fly) dataset with 4 columns and 4 rows.
The dataset contains 4 observations of two (imaginary) US counties— Crow County and Bull county (property 1).
For each county, 2 variables were measured — the number of speed cameras installed and the number of traffic violations (property 2).
And finally, measurements took place in 2 different discrete time points — 2018 and 2019 (property 3).
How cool!
I’ll now move along to describe how panel data modeling techniques can answer specific questions. Let’s say using the above 4 x 4 dataset we want to answer the following question:
Does number of installed speed cameras impact number of traffic violation cases?
If we run a simple linear OLS regression we should be able to quickly check the association — if there is any — between the two variables:
traffic_violation = f(speed_camera)
However, remember that this is no ordinary dataset, it’s a panel data. Which means we can use it far effectively than running a simple OLS regression.
How?
First, we shouldn’t forget that the independent variable has two other properties — county and year. Meaning that there is a variation along individual and time dimensions, which we can capture in more advanced models that we are calling panel data regression.
There are three main types of panel data models (i.e. estimators) and briefly described below are their formulation.
a) Pooled OLS model
Pooled OLS (Ordinary Least Square) model treats a dataset like any other cross-sectional data and ignores that the data has a time and individual dimensions. That is why the assumptions are similar to that of ordinary linear regression.
b) Fixed effects model
While speed camera installation might have an impact on traffic violations, it is also possible that each individual county is different in terms of traffic violation because of reasons other than speed camera (e.g. higher rate of highway patrol?). However, this is not reflected in the above OLS model. Fixed effects models go a step further by taking into account the differences between individual entities (counties in our case):
c) Random effects model
In fixed effects model we have controlled for differences between individual counties. But what about variables that are constant across individuals but change over time? A random effects model takes into consideration these individual variations as well as time dependent variations. The model eliminates biases from variables that are unobserved and change over time.
It is really just a few lines of codes (assuming that you have done the other 80% work of data wrangling!). plm is the best R library in town that implements your model in just 3 easy steps: (1) it takes in input the data; (2) converts data into a panel data frame; (3) implements the model as you specify.
# import packagelibrary(plm)# import data df =   read.table("../data.csv")# convert the data frame to a data format recognizable by `plm` pdf = pdata.frame(df, index = c("county", "year"))# specify and run the modelmodel = plm(traffic_violation~speed_camera, data = pdf, model = "random")summary(model)
Data science is an evolving discipline and is being enriched with diverse tools and techniques coming from many disciplines. Econometrics offers tools which are traditionally used in social and economic research but are also adding value in data science. For those interested, two open access books can get you started in econometrics and advanced social science research:
Introduction to Econometrics with R and
Principles of Econometrics with R.
These two books describe complex techniques in an accessible way along with applications and implementation in R programming language.