For this discussion post, I use the CigaretteSW data set from the AER package which includes data on cigarette consumption in the 48 continental United States in 1985 and 1995.
Within this data set, we have 2 time periods for each of the 48 states, and 7 variables - CPI the consumer price index, Population the state’s population at the time, Packs the number of cigarette packs purchased per capita, Income state personal income, Tax average taxes for the fiscal year, Price average price per pack of cigarettes during fiscal year, and Taxs the average excise taxes on cigarettes for the fiscal year.
Thus, our entities here in our panel data set are the48 states and our time components are the two years we have observations - 1985 & 1995.
A panel data set is balanced when each entity has an observation for each time component. We can confirm that this data set is balanced, by checking that there are 96 total observations, 48 levels of state, and 2 levels of year:
# Load the AER packagelibrary(AER)library(dplyr)library(stargazer)library(ggplot2)library(plm)# Load the Cigarette datasetdata("CigarettesSW")distinct_count_states <-n_distinct(CigarettesSW$state)distinct_count_years <-n_distinct(CigarettesSW$year)distinct_count_obs <-nrow(CigarettesSW)# Print the resultcat("Distinct states:", paste(distinct_count_states, collapse =", "), "\n")
We can also visualize this data set to confirm it is balanced, where the number of observations for each state & for each year are equal across states / years:
Although we already know this is not the best method given this data, I will first run an ordinary least squares linear regression on the data. For this example, I will use cigarette Price and Tax to predict the number of Packs of cigarettes per capita. The model will be implemented as follows:
As shown above in the summary table, our model is statistically significant with an F-statistic of 36.792 and p-value <0.01, but not all of our coefficients are. Our intercept and Price are statistically significant, and the coefficient for Price does intuitively make sense - where a $1 increase in price results in a decrease of 0.231 packs of cigarettes per capita. Our coefficient for Tax is not statistically significant.
There could definitely be omitted variable bias in this data, as there could be other factors that impact the packs of cigarettes per capita in each state besides price. For instance, advertising and marketing for cigarettes or health campaigns in each state could vary, as well as cultural/social norms differ across states - all of which are not accounted for in this OLS regression.
To help reduce this omitted variable bias, we can implement a fixed effects model.
Fixed Effects Model on CigaretteSW
To control for some of the variability across the states, we can include a fixed effect dummy variable(s) for the states with our original OLS model:
We can also add a fixed effects variable for years - if we expect there to be a change across all states for one year or another. I do hypothesize that over the 10 years there will be a shift in the whole country in terms of cigarette smoking, so I will try adding that as well:
packs_fe_lm_mod_time <-lm(packs ~ price+tax+state+year, data = CigarettesSW )stargazer(packs_fe_lm_mod_time, type ="text")
Now, we can compare this model with fixed effects to our original OLS regression and see that the impact of price on packs per capita is actually more significant (higher p-value) and higher economic magnitude in the fixed effects model with dummy variables for both states & year:
stargazer(ols_model, packs_fe_lm_mod, packs_fe_lm_mod_time,type ='text',column.labels =c("OLS with no State Dummy","OLS with State Dummy", "OLS with State + Year Dummy") )
There are different ways to build a fixed effects model than the one I demonstrated above. Below, I will implement the plm() function, where we declare our data as panel. We can see that the coefficient on price is the same for this fixed effects model as it was for the one run above (two right most models in the table). There is also essentially zero impact from tax on packs per capita (coefficient insignificant and very small economic magnitude):
# estimate the fixed effects regression with plm()plm_fe_mod <-plm(packs~price+tax+state+year, data = CigarettesSW,index =c("state", "year"), # declaring data to be panelmodel ="within"# fixed effects mdodel )stargazer(ols_model, packs_fe_lm_mod, packs_fe_lm_mod_time,plm_fe_mod,type ='text',column.labels =c("OLS with no State Dummy","OLS with State Dummy", "OLS with State + Year Dummy", 'PLM Function') )