{"id":595,"date":"2018-11-12T22:06:51","date_gmt":"2018-11-13T05:06:51","guid":{"rendered":"https:\/\/blogs.ubc.ca\/datawithstata\/?page_id=595"},"modified":"2019-05-07T15:24:55","modified_gmt":"2019-05-07T22:24:55","slug":"panel-regression","status":"publish","type":"page","link":"https:\/\/blogs.ubc.ca\/datawithstata\/home-page\/regression\/panel-regression\/","title":{"rendered":"Panel Regression"},"content":{"rendered":"<p>When the <em>same<\/em> <em>cross-section<\/em> of individuals is observed <em>across multiple periods of time<\/em>, the resulting dataset is called a <em>panel<\/em> dataset. For example, a dataset of annual GDP of 51 U.S. states from 1947 to 2018 is a panel data on the variable\u00a0gdp<em><sub>it<\/sub> <\/em>where <em>i=1,\u2026,51<\/em> and <em>t=1,\u2026,72.\u00a0<\/em><\/p>\n<p>The key difference in running regressions with panel data (with both cross-sectional and time-series variations) from a usual <abbr class='c2c-text-hover' title='Ordinary Least Square'>OLS<\/abbr> regression (with only cross-sectional variation) is that one needs to control for the common effect for all individuals in a particular time point, and also the idiosyncratic individual effect that is common across all years. These are called the time fixed effects and the individual fixed effects respectively. The variation that is left after controlling for these fixed effects is the variation at the interaction between individual and time. The most common specification for a panel regression is as follows:<\/p>\n<p>y<sub>it<\/sub> = b<sub>0<\/sub> <span style=\"display: inline !important; float: none; background-color: #ffffff; color: #444444; cursor: text; font-family: Georgia,'Bitstream Charter',serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;\">+ b<\/span><sub>1<\/sub><span style=\"display: inline !important; float: none; background-color: #ffffff; color: #444444; cursor: text; font-family: Georgia,'Bitstream Charter',serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;\">x<\/span><sub>it<\/sub> + <span style=\"display: inline !important; float: none; background-color: #ffffff; color: #444444; cursor: text; font-family: Georgia,'Bitstream Charter',serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;\">b<\/span><sub>2<\/sub>D<sub>i<\/sub> + <span style=\"display: inline !important; float: none; background-color: #ffffff; color: #444444; cursor: text; font-family: Georgia,'Bitstream Charter',serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;\">b<\/span><sub>3<\/sub>D<sub>t<\/sub> + e<sub>it<\/sub><\/p>\n<p>In the above regression, b<sub>2<\/sub> denotes the individual fixed effects, while b<sub>3<\/sub> denotes the time fixed effects. These fixed effects are nothing but the coefficients of the dummy variables D<sub>i<\/sub> and <span style=\"display: inline !important; float: none; background-color: #ffffff; color: #444444; cursor: text; font-family: Georgia,'Bitstream Charter',serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;\">D<\/span><sub>t<\/sub><em>. <\/em>Once again, the problem of the <em>dummy variable trap<\/em> becomes relevant, as discussed in the section on <a href=\"https:\/\/blogs.ubc.ca\/datawithstata\/home-page\/regression\/ordinary-least-square\/\">regression with dummy variables<\/a>. If there are N individuals, then only N-1 individual dummies (<span style=\"display: inline !important; float: none; background-color: #ffffff; color: #444444; cursor: text; font-family: Georgia,'Bitstream Charter',serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;\">D<\/span><sub>i<\/sub> &#8216;s) should be included, and if there are T time-points, then only T-1 time dummies (<span style=\"display: inline !important; float: none; background-color: #ffffff; color: #444444; cursor: text; font-family: Georgia,'Bitstream Charter',serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;\">D<\/span><sub>t<\/sub> &#8216;s) should be included in the panel regression that contains the intercept term b<sub>0<\/sub>. The individual dummies are defined as follows:\u00a0 D<sub>i<\/sub> takes the value 1 if the data-point corresponds to individual <em>i<\/em>, and otherwise takes the value 0. Thus, D<sub>i<\/sub> will be 1 for T data-points and 0 for (N-1)T data-points. Similarly for the time dummies, D<sub>t<\/sub>\u00a0 takes the value 1 if the data-point correponds to time-point <em>t<\/em>, and otherwise takes the value 0. Thus D<sub>t<\/sub> will be 1 for N data-points and 0 for (T-1)N data-points.<\/p>\n<p>Depending on whether the individual effects D<sub>i<\/sub> are allowed to be correlated with the explanatory variable x<sub>it<\/sub> , the regression model is either called a <strong>fixed effects (FE) model<\/strong> or a <strong>random effects (RE) model<\/strong>. While the uncorrelatedness of\u00a0x<sub>it<\/sub> is desirable for both the FE and RE models, the RE model additionally imposes the independence of the individual effects D<sub>i<\/sub> with the explanatory variable\u00a0x<sub>it<\/sub> . As a rule of thumb, it is always better to assume a fixed effects model because the estimates from an FE model is always consistent, while the RE model is consistent only if the underlying true model is RE. The only disadvantage of wrongly assuming an FE model when the true model is RE, is that the FE estimator will be inefficient (that is, the variance of the estimators will be larger). The <em>Durbin-Wu-Hausman specification test<\/em> helps the researcher to decide which model (RE or FE) to consider given a particular dataset.<\/p>\n<p>Depending on the nature of the dependent variable y<sub>it<\/sub>, e.g., categorical type (binary or polytomous), or the endogeneity of x<sub>it<\/sub>, the techniques that have been discussed in different sections using cross-sectional data, are still largely valid with panel data.<\/p>\n<div id=\"random-accordion-id-67\" class=\"accordion-shortcode  \"><h3 ><a href=\"#stata-code-without-example-0\" ><abbr class='c2c-text-hover' title='Statistical Software for Data Analysis'>STATA<\/abbr> code without Example<\/a><\/h3><div id=\"stata-code-without-example-0\" class=\"accordian-shortcode-content \" ><\/p>\n<p>In <abbr class='c2c-text-hover' title='Statistical Software for Data Analysis'>STATA<\/abbr>, before one can run a panel regression, one needs to first declare that the dataset is a panel dataset. This is done by the following command:<\/p>\n<p><strong>xtset <\/strong>id time<\/p>\n<p>The command <strong>xtset<\/strong> is used to declare the panel structure with 'id' being the cross-sectional identifying variable (e.g., the variable that identifies the 51 U.S. states as 1,2,...,51), and 'time' being the time-series identifying variable (e.g., the variable that records the year of observation 1947,1948,...,2018).<\/p>\n<p>A fixed effects (FE) panel regression can be implemented in <abbr class='c2c-text-hover' title='Statistical Software for Data Analysis'>STATA<\/abbr> using the following command:<\/p>\n<p><strong>regress<\/strong> y i.time i.id x<\/p>\n<p>The <em>i.time<\/em> variable tells <abbr class='c2c-text-hover' title='Statistical Software for Data Analysis'>STATA<\/abbr> to create a dummy for each time-point and estimate the corresponding time fixed effects. Similarly, <em>i.id<\/em> variable tells <abbr class='c2c-text-hover' title='Statistical Software for Data Analysis'>STATA<\/abbr> to create a dummy for each individual and estimate the corresponding individual fixed effects. Another way to implement the FE model in <abbr class='c2c-text-hover' title='Statistical Software for Data Analysis'>STATA<\/abbr> is to simply write the following command:<\/p>\n<p><strong>xtreg<\/strong> y i.time x, <strong>fe<\/strong><\/p>\n<p>The option <strong>fe<\/strong> tells <abbr class='c2c-text-hover' title='Statistical Software for Data Analysis'>STATA<\/abbr> to include the cross-sectional effects and estimate them assuming an FE model.\u00a0 It should be noted that this alternative way of estimating the fixed effects model suppresses the estimates of the individual fixed effects. Therefore, if it is important to the researcher to know the estimates of the individual fixed effects then the first method is preferrable. On the other hand, if an RE model is to be fit, then it can be done in <abbr class='c2c-text-hover' title='Statistical Software for Data Analysis'>STATA<\/abbr> using the following command:<\/p>\n<p><strong>xtreg<\/strong> y i.time x, <strong>re<\/strong><\/p>\n<p><abbr class='c2c-text-hover' title='Statistical Software for Data Analysis'>STATA<\/abbr> can also run the Durbin-Wu-Hausman specification test to help choose between the FE and RE models. The null hypothesis in the Hausman test is that the true model is RE against the alternative hypothesis that the true model is FE. Thus if the calculated test-statistic is large enough, or equivalently the p-value is small enough, then the FE model is preferred.<\/p>\n<p>To do this, one first needs to estimate and store the estimates from each of the FE and RE models, and then compute the Hausman test-statistic to run the test. This is done as shown below.<\/p>\n<p><b>xtset<\/b> id time<br \/>\n<b>xtreg<\/b> y x, <b>fe<br \/>\n<\/b><b>estimates store<\/b> fixed<br \/>\n<b>xtreg <\/b>y x,<b> re<br \/>\n<\/b><b>estimates store<\/b> random<br \/>\n<b>hausman<\/b> fixed random<\/p>\n<p>\n<\/div><\/div><!-- #random-accordion-id-67end of accordion shortcode -->\n<div id=\"random-accordion-id-294\" class=\"accordion-shortcode  \"><h3 ><a href=\"#stata-code-and-interpretation--1\" ><abbr class='c2c-text-hover' title='Statistical Software for Data Analysis'>STATA<\/abbr> code and Interpretation of output with Example<\/a><\/h3><div id=\"stata-code-and-interpretation--1\" class=\"accordian-shortcode-content \" ><\/p>\n<p>Suppose, we are interested in understanding the effect of financial development on GDP volatility. One might think that financial development might help households and firms to better manage unexpected events which will reduce the volatility in GDP. To perform such an analysis, we will need panel data on countries across time since countries differ in their levels of financial development and the rate of financial development (The data can be found <a href=\"https:\/\/www.dropbox.com\/s\/rdh11dk48ficgoj\/panel.dta?dl=0\">here<\/a>. <a href=\"http:\/\/documents.worldbank.org\/curated\/en\/453261468767094681\/Finance-and-macroeconomic-volatility\">Denzier et. al. (2002)<\/a> is the seminal paper in this literature.).<\/p>\n<p>The dataset also includes other macroeconomic variables such as degree of trade openness and GDP per capita. One might need to control for GDP since low income countries are expected to display higher volatility in output. Also, countries which are more open to trade might be more or less susceptible to foreign or domestic shocks. Obviously, this is not an exhaustive set of controls and more relevant controls can be added.<\/p>\n<p>First we will declare the dataset is panel.<\/p>\n<p><strong>xtset\u00a0<\/strong>ID time<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1022\" src=\"https:\/\/blogs.ubc.ca\/datawithstata\/files\/2019\/04\/xtset-300x68.png\" alt=\"\" width=\"330\" height=\"75\" srcset=\"https:\/\/blogs.ubc.ca\/datawithstata\/files\/2019\/04\/xtset-300x68.png 300w, https:\/\/blogs.ubc.ca\/datawithstata\/files\/2019\/04\/xtset.png 387w\" sizes=\"auto, (max-width: 330px) 100vw, 330px\" \/><\/p>\n<ul>\n<li><abbr class='c2c-text-hover' title='Statistical Software for Data Analysis'>Stata<\/abbr> displays that the panel variable (ID) is <em>strongly balanced\u00a0<\/em>implying that most countries are available with equal number of time periods otherwise it will show <em>unbalanced<\/em>. The analysis can be performed with unbalanced panel as well but having a balanced panel allows to better estimate the fixed effects.<\/li>\n<li>The time variable ranges from values 0 to 8. Depending on your dataset and application, you might require formating the time variable. (<u><a href=\"https:\/\/www.stata.com\/manuals13\/u24.pdf\">How to format time variable in <abbr class='c2c-text-hover' title='Statistical Software for Data Analysis'>Stata<\/abbr>?<\/a><\/u>)<\/li>\n<li>Delta shows the difference in the time units is 1.<\/li>\n<\/ul>\n<p>Second, we will run the fixed effects model to investigate the relationship between financial development, given by ratio of total deposits in bank to GDP (<em>deposits_gdp)<\/em> and fluctuations in output given by standard deviation of filtered output (<em>sd_gdp<\/em>). There are controls for trade openness (<em>tradeopenness<\/em>) and log of per capita income (<em>log_gdp_pc<\/em>). The command to do this in <abbr class='c2c-text-hover' title='Statistical Software for Data Analysis'>Stata<\/abbr> is the following:<\/p>\n<p><strong>xtreg<\/strong> sd_gdp deposits_gdp tradeopenness log_gdp_pc, fe<br \/>\n<strong>estimates<\/strong> store fixed<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1028\" src=\"https:\/\/blogs.ubc.ca\/datawithstata\/files\/2019\/04\/fixed_effects-300x219.png\" alt=\"\" width=\"547\" height=\"400\" srcset=\"https:\/\/blogs.ubc.ca\/datawithstata\/files\/2019\/04\/fixed_effects-300x219.png 300w, https:\/\/blogs.ubc.ca\/datawithstata\/files\/2019\/04\/fixed_effects-400x293.png 400w, https:\/\/blogs.ubc.ca\/datawithstata\/files\/2019\/04\/fixed_effects.png 637w\" sizes=\"auto, (max-width: 547px) 100vw, 547px\" \/><\/p>\n<ul>\n<li>The coefficient of <em>deposits_gdp\u00a0<\/em>is -0.009 and it implies that a unit increase in finance reduces the standard deviation of GDP by 0.009. The coefficient is significantly different from zero.<\/li>\n<li>The important thing to keep in mind here, is that the coefficient reflects the effect from the time-variation. The fixed effects model controls for the individual effects so, only changes in the independent variable across time are captured and not differences in the independent variable between countries.<\/li>\n<li>The R<sup>2<\/sup> within is the ordinary R<sup>2<\/sup> (R<sup>2<\/sup> in\u00a0the cross-section <abbr class='c2c-text-hover' title='Ordinary Least Square'>OLS<\/abbr>) in this case.<\/li>\n<li><em>sigma_u\u00a0<\/em>is the standard deviation of residuals within groups<\/li>\n<li><em>sigma_e\u00a0<\/em>is the standard deviation of the residuals of the idiosyncratic error term<\/li>\n<li><em>rho\u00a0<\/em>is the ratio of (sigma_u)<sup>2<\/sup> \/ ( (sigma_u)<sup>2 <\/sup>+ (sigma_e)<sup>2<\/sup> ). It represents the intraclass correlation of the error. Thus, it explains the within country relative contribution.<\/li>\n<li><strong>estimates\u00a0<\/strong>will store the coefficients from the <strong>xtreg\u00a0<\/strong>regression. We store it as <em>fixed<\/em>.<\/li>\n<\/ul>\n<p>Third, we will now estimate this link using a random effects model. The command to do this in <abbr class='c2c-text-hover' title='Statistical Software for Data Analysis'>Stata<\/abbr> is the following:<\/p>\n<p><strong>xtreg<\/strong> sd_gdp deposits_gdp tradeopenness log_gdp_pc, re<br \/>\n<strong>estimates<\/strong> store random<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1035\" src=\"https:\/\/blogs.ubc.ca\/datawithstata\/files\/2019\/04\/random_effects-300x207.png\" alt=\"\" width=\"580\" height=\"400\" srcset=\"https:\/\/blogs.ubc.ca\/datawithstata\/files\/2019\/04\/random_effects-300x207.png 300w, https:\/\/blogs.ubc.ca\/datawithstata\/files\/2019\/04\/random_effects-400x276.png 400w, https:\/\/blogs.ubc.ca\/datawithstata\/files\/2019\/04\/random_effects.png 638w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/p>\n<ul>\n<li>The coefficient of <em>deposits_gdp\u00a0<\/em>is -0.107 and it implies that a unit increase in finance reduces the standard deviation of GDP by 0.107. The coefficient is significantly different from zero.<\/li>\n<li>But the interpretation of the coefficient is different since it now measures the average effect of the independent variable on the dependent variable where the independent variables changes across time and countries by one unit.<\/li>\n<li>Since the random effects model is a weighted average of the between and within estimators, none of the three reported R<sup>2<\/sup> are meaningful.<\/li>\n<li>The interpretation of <em>sigma_u<\/em>, <em>sigma_e<\/em> and <em>rho<\/em> is same as before.<\/li>\n<li>We store the coefficients as <em>random<\/em>.<\/li>\n<\/ul>\n<p>To decide between fixed or random effects we can run a Hausman test where the null hypothesis is that the preferred model is random effects vs. the alternative that the preferred model is fixed effects.<\/p>\n<p><strong>hausman<\/strong> fixed random<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1036\" src=\"https:\/\/blogs.ubc.ca\/datawithstata\/files\/2019\/04\/hausman_panel-300x156.png\" alt=\"\" width=\"576\" height=\"300\" srcset=\"https:\/\/blogs.ubc.ca\/datawithstata\/files\/2019\/04\/hausman_panel-300x156.png 300w, https:\/\/blogs.ubc.ca\/datawithstata\/files\/2019\/04\/hausman_panel-400x208.png 400w, https:\/\/blogs.ubc.ca\/datawithstata\/files\/2019\/04\/hausman_panel.png 636w\" sizes=\"auto, (max-width: 576px) 100vw, 576px\" \/><\/p>\n<ul>\n<li>The important thing to look at is the p-value of the test statistic and it is 2%. Thus, the Random effects model can be rejected.<\/li>\n<\/ul>\n<p>\n<\/div><\/div><!-- #random-accordion-id-294end of accordion shortcode -->\n","protected":false},"excerpt":{"rendered":"<p>When the same cross-section of individuals is observed across multiple periods of time, the resulting dataset is called a panel dataset. For example, a dataset of annual GDP of 51 U.S. states from 1947 to 2018 is a panel data &hellip; <a href=\"https:\/\/blogs.ubc.ca\/datawithstata\/home-page\/regression\/panel-regression\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":62204,"featured_media":0,"parent":170,"menu_order":7,"comment_status":"closed","ping_status":"closed","template":"full-width-page.php","meta":{"footnotes":""},"class_list":["post-595","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blogs.ubc.ca\/datawithstata\/wp-json\/wp\/v2\/pages\/595","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.ubc.ca\/datawithstata\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.ubc.ca\/datawithstata\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.ubc.ca\/datawithstata\/wp-json\/wp\/v2\/users\/62204"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.ubc.ca\/datawithstata\/wp-json\/wp\/v2\/comments?post=595"}],"version-history":[{"count":31,"href":"https:\/\/blogs.ubc.ca\/datawithstata\/wp-json\/wp\/v2\/pages\/595\/revisions"}],"predecessor-version":[{"id":1228,"href":"https:\/\/blogs.ubc.ca\/datawithstata\/wp-json\/wp\/v2\/pages\/595\/revisions\/1228"}],"up":[{"embeddable":true,"href":"https:\/\/blogs.ubc.ca\/datawithstata\/wp-json\/wp\/v2\/pages\/170"}],"wp:attachment":[{"href":"https:\/\/blogs.ubc.ca\/datawithstata\/wp-json\/wp\/v2\/media?parent=595"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}