Showing posts with label Stata. Show all posts
Showing posts with label Stata. Show all posts

Saturday, 24 March 2018

Time Series Analysis (Lecture 4 Part 1): Johansen Cointegration Test in EViews

After unit root testing, what next?
The outcome of unit root testing matters for the empirical model to be estimated. The following scenarios explain the implications of unit root testing for further analysis.  Still drawing on the previous tutorials (see here for EViews, Stata and Excel) on unit root testing with the augmented Dickey-Fuller procedure (see videos), we are using the same data from Gujarati and Porter Table 21.1 quarterly data of 1970q1 to 1991q4. The variables in question are pce, pdi and gdp in natural logarithms.

Scenario 1:  When series under scrutiny are stationary in levels.
In this scenario, it is assumed that lnpce, lnpdi and lngdp are stationary in levels, that is, they are I(0) series (integrated of order zero).  In this situation, performing a cointegration test is not necessary. This is because any shock to the system in the short run quickly adjusts to the long-run. Consequently, only the long run model should be estimated using OLS (where variables are neither lagged nor differenced). It is the static form of the model. In essence, the estimation of short run model is not necessary if series are I(0).  

Scenario 2: When series are stationary in first differences.
1.    Under this scenario, the series are assumed to be non-stationary but became stationary after first difference
2.    One special feature of this is that they are of the same order of integration.
3. Under this scenario, the model in question is not entirely useless although the variables are unpredictable. To verify further the relevance of the model, there is need to test for cointegration.  That is, can we assume a long run relationship in the model despite the fact that the series are drifting apart or trending either upward or downward?
4. There are however, two prominent cointegration tests for I(I) series in the literature. They are Engle-Granger cointegration test and Johansen Cointegration test.
5. The Engle-Granger test is meant for single equation model while Johansen cointegration test is considered when dealing with multiple equations.

If there is cointegration:
1.    Implies that the series in question are related and therefore can be combined in a linear fashion.
2.  That is, even if there are shocks in the short run, which may affect movement in the individual series, they would converge with time (in the long run).
3.    Estimate both long-run and short-run models.
4. The estimation will require the use of vector autoregressive (VAR) model and vector error correction model (VECM) analysis.

If there is no cointegration:
1.    Estimate only the short-run model, which is VAR and not VECM.

Johansen Cointegration Test in EViews
The hypothesis is stated as:
H0: no cointegrating equation
H1: H0 is not true
Rejection of the null hypothesis is at the 5% level.

Note: Cointegration test should be performed on the level form of the variables and not on their first difference. It is okay to also use the log-transformation of the raw variables, as I have done in this example.
Steps:
1.   Load data into EViews (see video on how to do this)
2.   Open as Group data (see video on how to do this)
3.   Go to Quick >> Group Statistics >> Johansen Cointegration >> dialog box opens >> list the variables >> Click OK >> Select option 3 [Intercept (no trend)] >> Click OK

Here is the EViews result on the Johansen Cointegration test of lnpce, lnpdi and lngdp:
EViews - Johansen Cointegration Test from cruncheconometrix.com.ng
EViews - Johansen Cointegration Test
Source: CrucnhEconometrix
Interpreting Johansen Cointegration Test Results
1.   The EViews output releases two statistics, Trace Statistic and Max-Eigen Statistic
2.   Rejection criteria is at 0.05 level
3.   Rejection of the null hypothesis is indicated by an asterisk sign (*)
4.   Reject the null hypothesis if the probability value is less than or equal to 0.05
5.   Reject the null hypothesis if the Trace or Max-Eigen statistic is higher than the 0.05 critical value

Decision: Given the results generated, the null hypothesis of no cointegrating equation is rejected at the 5% level. Hence, it is concluded that a long-run relationship exist among the three variables.

 [Watch video on how to conduct Johansen cointegration test in EViews]

However, if the null hypothesis cannot be rejected, it evidences no cointegration and hence there is no long-run relationship among the series. This implies that, if there are shocks to the system, the model is not likely to converge in the long-run. In addition, if there is no cointegration, only the short run model should be estimated. That is, estimates only VAR do not estimate a VECM!

If there are comments or areas requiring further clarification, kindly post them below….

Friday, 2 March 2018

Panel Data Analysis (Lecture 2): How to Perform the Hausman Test in EViews


Introduction to Panel Data Models

The panel data approach pools time series data with cross-sectional data. Depending on the application, it can comprise a sample of individuals, firms, countries, or regions over a specific time period. The general structure of such a model could be expressed as follows:

Yit = ao + bXit + uit

where uit ~ IID(0, 𝜎2) and i = 1, 2, ..., N individual-level observations, and t = 1, 2, ...,T time series observations.

In this application, it is assumed that Yit is a continuous variable. In this model, the observations of each individual, firm or country are simply stacked over time on top of each another. This is the standard pooled model where intercepts and slope coefficients are homogeneous across all N cross-sections and through all T time periods. The application of OLS to this model ignores the temporal and spatial dimension inherent in the data and thus throws away useful information. It is important to note that the temporal dimension captures the ‘within’ variation in the data while the spatial dimension captures the ‘between’ variation in the data. The pooled OLS estimator exploits both ‘between’ and ‘within’ dimensions of the data but does not do so efficiently. Thus, in this procedure each observation is given equal weight in estimation. In addition, the unbiasedness and consistency of the estimator requires that the explanatory variables are uncorrelated with any omitted factors. The limitations of OLS in such an application prompted interest in alternative procedures. There are a number of different panel estimators but the most popular is the fixed effects (or ‘within’) estimator.

Fixed Effects or Random Effects?
The question is usually asked which econometric model an investigator should use when modelling with panel data. The different models can generate considerably different results and this has been documented in many empirical studies. In terms of a model where time effects are assumed absent for simplicity, the model to be estimated may be given by:

Yit = ai bXit + uit

The question, therefore, is do we treat ai as fixed or random? The following points are worth noting.

·1) The estimation of the fixed effects model is costly in terms of degrees of freedom. This is a statistical and not a computing cost. It is particularly problematic when N is large and T is small. The occurrence of large N and small T currently tends to characterize most panel data applications encountered.
·2) The ai terms are taken to characterize (for want of a better expression) investigator ignorance. In the fixed effects model does it make sense to treat one type of investigator ignorance (ai) as fixed but another as random (uit)?
·3) The fixed effects formulation is viewed as one where investigators make inferences conditional on the fixed effects in the sample.
 4)The random effects formulation is viewed as one where investigators make unconditional inferences with respect to the population of all effects.
· 5) The random effects formulation treats the random effects as independent of the explanatory variables (i.e. E(ai Xit) = 0). Violation of this assumption leads to bias and inconsistency in the b vector.

Advantage and disadvantage of the fixed effects model
The main advantage of the fixed effects model is its relative ease of estimation and the fact that it does not require independence of the fixed effects from the other included explanatory variables. The main disadvantage is that it requires estimation of N separate intercepts. This causes problems because much of the variation that exists in the data may be used up in estimating these different intercept terms. As a consequence, the estimated effects (the bs) for other explanatory variables in the regression model may be imprecisely estimated. These might represent the more important parameters of interest from the perspective of policy. As noted above the fixed effects estimator is derived using the deviations between the cross-sectional observations and the long-run average value for the cross-sectional unit. This problem is most acute, therefore, when there is little variation or movement in the characteristics over time, that is when the variables are rarely-changing or they are time-invariant. In essence, the effects of these variables are eliminated from the analysis.

Advantage and disadvantage of the random effects model
The main advantage of the random effects estimator is that it uses up fewer degrees of freedom in estimation and allows for the inclusion of time invariant covariates. The main disadvantage of the model is the assumption that the random effects are independent of the included explanatory variables. It is fairly plausible that there may be unobservable attributes not included in the regression model that are correlated with the observable characteristics. This procedure, unlike fixed effects, does not allow for the elimination of the omitted heterogeneous effects.

The Hausman Test
In determining which model is the more appropriate to use, a statistical test can be implemented. The Hausman test compares the random effects estimator to the ‘within’ estimator. If the null is rejected, this favours the ‘within’ estimator’s treatment of the omitted effects (i.e., it favours the fixed effects but only relative to the random effects). The use of the test in this case is to discriminate between a model where the omitted heterogeneity is treated as fixed and correlated with the explanatory variables, and a model where the omitted heterogeneity is treated as random and independent of the explanatory variables.

·      If the omitted effects are uncorrelated with the explanatory variables, the random effects estimator is consistent and efficient. However, the fixed effects estimator is consistent but not efficient given the estimation of a large number of additional parameters (i.e., the fixed effects).

·      If the effects are correlated with the explanatory variables, the fixed effects estimator is consistent but the random effects estimator is inconsistent. The Hausman test provides the basis for discriminating between these two models and the matrix version of the Hausman test is expressed as:

[bRE– bFE][V(bFE) – V(bRE)]-1[bRE – bFE]′ ~   𝝌²k

where k is the number of covariates (excluding the constant) in the specification. If the random effects are correlated with the explanatory variables, then there will be a statistically significant difference between the random effects and the fixed effects estimates. Thus, the null and alternative hypotheses are expressed as:

            H0: Random effects are independent of explanatory variables

H1: H0 is not true.

The null hypothesis is the random effects model and if the test statistic exceeds the relevant critical value, the random effects model is rejected in favour of the fixed effects model. In finite samples the inversion of the matrix incorporating the difference in the variance-covariance matrices may be negative-definite (or negative semi-definite) thus yielding non-interpretable values for the chi-squared.

The selection of one model over the other might be dictated by the nature of the application. For example, if the cross-sectional units were countries and states, it may be plausible to assume that the omitted effects are fixed in nature and not the outcome of a random draw. However, if we are dealing with a sample of individuals or firms drawn from a population, the assumption of a random effects model has greater appeal. However, the choice of which model to choose is ultimately dictated empirically. If it does not prove possible to discriminate between the two models on the basis of the Hausman test, it may be safest to use the fixed effects model, where the consequences of a correlation between the fixed effects and the explanatory variables are less devastating than is the case with the random effects model where the consequences of failure result in inconsistent estimates. Of course, if the random effects are found to be independent of the covariates, the random effects model is the most appropriate because it provides a more efficient estimator than the fixed effects estimator.

**This tutorial is culled from my lecture note as given by Prof. Barry Reilly (Professor of Econometrics, University of Sussex, UK).


How to Perform the Hausman Test in EViews
First: Load file into EViews and create Group data (see video on how to do this)

Second: Perform fixed effects estimation: Quick >> Estimate Equation >> Panel Options >> Fixed >> OK
EViews: Equation Estimation Dialog Box from cruncheconometrix.com.ng
EViews: Equation Estimation Dialog Box
Source: CrunchEconometrix

Third: Perform random effects estimation: Quick >> Estimate Equation >> Panel Options >> Random >> OK

Fourth: Perform the Hausman test: View >> Fixed/Random Effects testing >> Correlated Random Effects – Hausman Test

Fifth: Interpret results:
Reject the null hypothesis if the prob-value is statistically significant at 5% level. It implies that the individual effects (ai) correlate with the explanatory variables. Therefore use the fixed effect estimator to run the analysis. Otherwise, use the random effects estimator.

[Watch video tutorial on performing the Hausman test in EViews]

If you still have comments or questions regarding how to perform the Hausman test, kindly post them in the comments section below…..

Wednesday, 28 February 2018

Panel Data Analysis (Lecture 2): How to Perform the Hausman Test in Stata

 

Introduction to Panel Data Models

The panel data approach pools time series data with cross-sectional data. Depending on the application, it can comprise a sample of individuals, firms, countries, or regions over a specific time period. The general structure of such a model could be expressed as follows:


Yit = ao + bXit + uit

where uit ~ IID(0, 𝜎2) and i = 1, 2, ..., N individual-level observations, and t = 1, 2, ...,T time series observations.

In this application, it is assumed that Yit is a continuous variable. In this model, the observations of each individual, firm or country are simply stacked over time on top of each another. This is the standard pooled model where intercepts and slope coefficients are homogeneous across all N cross-sections and through all T time periods. The application of OLS to this model ignores the temporal and spatial dimension inherent in the data and thus throws away useful information. It is important to note that the temporal dimension captures the ‘within’ variation in the data while the spatial dimension captures the ‘between’ variation in the data. The pooled OLS estimator exploits both ‘between’ and ‘within’ dimensions of the data but does not do so efficiently. Thus, in this procedure each observation is given equal weight in estimation. In addition, the unbiasedness and consistency of the estimator requires that the explanatory variables are uncorrelated with any omitted factors. The limitations of OLS in such an application prompted interest in alternative procedures. There are a number of different panel estimators but the most popular is the fixed effects (or ‘within’) estimator.

Fixed Effects or Random Effects?
The question is usually asked which econometric model an investigator should use when modelling with panel data. The different models can generate considerably different results and this has been documented in many empirical studies. In terms of a model where time effects are assumed absent for simplicity, the model to be estimated may be given by:

Yit = ai + bXit + uit

The question, therefore, is do we treat ai as fixed or random? The following points are worth noting.

·  The estimation of the fixed effects model is costly in terms of degrees of freedom. This is a statistical and not a computing cost. It is particularly problematic when N is large and T is small. The occurrence of large N and small T currently tends to characterize most panel data applications encountered.
·   The ai terms are taken to characterize (for want of a better expression) investigator ignorance. In the fixed effects model does it make sense to treat one type of investigator ignorance (ai) as fixed but another as random (uit)?
·      The fixed effects formulation is viewed as one where investigators make inferences conditional on the fixed effects in the sample.
·  The random effects formulation is viewed as one where investigators make unconditional inferences with respect to the population of all effects.
·  The random effects formulation treats the random effects as independent of the explanatory variables (i.e. E(ai Xit) = 0). Violation of this assumption leads to bias and inconsistency in the b vector.

Advantage and disadvantage of the fixed effects model
The main advantage of the fixed effects model is its relative ease of estimation and the fact that it does not require independence of the fixed effects from the other included explanatory variables. The main disadvantage is that it requires estimation of N separate intercepts. This causes problems because much of the variation that exists in the data may be used up in estimating these different intercept terms. As a consequence, the estimated effects (the bs) for other explanatory variables in the regression model may be imprecisely estimated. These might represent the more important parameters of interest from the perspective of policy. As noted above the fixed effects estimator is derived using the deviations between the cross-sectional observations and the long-run average value for the cross-sectional unit. This problem is most acute, therefore, when there is little variation or movement in the characteristics over time, that is when the variables are rarely-changing or they are time-invariant. In essence, the effects of these variables are eliminated from the analysis.

Advantage and disadvantage of the random effects model
The main advantage of the random effects estimator is that it uses up fewer degrees of freedom in estimation and allows for the inclusion of time invariant covariates. The main disadvantage of the model is the assumption that the random effects are independent of the included explanatory variables. It is fairly plausible that there may be unobservable attributes not included in the regression model that are correlated with the observable characteristics. This procedure, unlike fixed effects, does not allow for the elimination of the omitted heterogeneous effects.

The Hausman Test
In determining which model is the more appropriate to use, a statistical test can be implemented. The Hausman test compares the random effects estimator to the ‘within’ estimator. If the null is rejected, this favours the ‘within’ estimator’s treatment of the omitted effects (i.e., it favours the fixed effects but only relative to the random effects). The use of the test in this case is to discriminate between a model where the omitted heterogeneity is treated as fixed and correlated with the explanatory variables, and a model where the omitted heterogeneity is treated as random and independent of the explanatory variables.

·      If the omitted effects are uncorrelated with the explanatory variables, the random effects estimator is consistent and efficient. However, the fixed effects estimator is consistent but not efficient given the estimation of a large number of additional parameters (i.e., the fixed effects).
·      If the effects are correlated with the explanatory variables, the fixed effects estimator is consistent but the random effects estimator is inconsistent. The Hausman test provides the basis for discriminating between these two models and the matrix version of the Hausman test is expressed as:

[bREbFE][V(bFE) – V(bRE)]-1[bREbFE]′ ~   𝝌²k

where k is the number of covariates (excluding the constant) in the specification. If the random effects are correlated with the explanatory variables, then there will be a statistically significant difference between the random effects and the fixed effects estimates. Thus, the null and alternative hypotheses are expressed as:

H0: Random effects are independent of explanatory variables
H1: H0 is not true.

The null hypothesis is the random effects model and if the test statistic exceeds the relevant critical value, the random effects model is rejected in favour of the fixed effects model. In finite samples the inversion of the matrix incorporating the difference in the variance-covariance matrices may be negative-definite (or negative semi-definite) thus yielding non-interpretable values for the chi-squared.

The selection of one model over the other might be dictated by the nature of the application. For example, if the cross-sectional units were countries and states, it may be plausible to assume that the omitted effects are fixed in nature and not the outcome of a random draw. However, if we are dealing with a sample of individuals or firms drawn from a population, the assumption of a random effects model has greater appeal. However, the choice of which model to choose is ultimately dictated empirically. If it does not prove possible to discriminate between the two models on the basis of the Hausman test, it may be safest to use the fixed effects model, where the consequences of a correlation between the fixed effects and the explanatory variables are less devastating than is the case with the random effects model where the consequences of failure result in inconsistent estimates. Of course, if the random effects are found to be independent of the covariates, the random effects model is the most appropriate because it provides a more efficient estimator than the fixed effects estimator.

**This tutorial is culled from my lecture note as given by Prof. Barry Reilly (Professor of Econometrics, University of Sussex, UK).

How to Perform the Hausman Test in Stata
First: Open a log file, load data into Stata, use a do-file (to replicate your research)

Second: Inform Stata that you are using a panel with ‘id’ the cross-sectional indicator and 'year' the time period indicator to prepare for panel data analysis.
xtset id year

Third: Create year dummies (to capture time variations in the data)
tab year, gen(yr)

Fourth: Run the fixed effects model and store the results
eststo fixed: xtreg y x1 x2 x3 x4 yr2 – yr..., fe i(c_id)

Fifth: Run the random effects model and store the results
eststo random: xtreg y x1 x2 x3 x4 yr2 – yr..., re i(c_id)

Sixth: Run the Hausman test
hausman fixed random

Seventh: Interpret results: Reject the null hypothesis if the prob-value is statistically significant at 5% level. It implies that the individual effects (ai) correlate with the explanatory variables. Therefore use the fixed effect estimator to run the analysis. Otherwise, use the random effects estimator.


[Watch video tutorial on performing the Hausman test in Stata]
If you still have comments or questions regarding how to perform the Hausman test, kindly post them in the comments section below…..

Wednesday, 14 February 2018

Time Series Analysis (Lecture 2): Choosing Optimal Lags in Stata


General Overview on Lag Selection

Since this blog is tailored for beginners in econometrics, I will not be engaging an advanced discussion on the topic but an introductory approach by which a beginner can understand the essence of using lags in a model and the pitfalls that may occur if lags are excessively used. Interested readers who require advanced information on selecting optimal lags can consult appropriate econometric textbooks. Having said that, in economics the dependence of a variable Y (outcome variable or regressand) on another variable(s) X (the predictor variable or regressor) is rarely instantaneous. Very often, Y responds to X with a lapse of time. Such a lapse of time is called a lag. Therefore, in time series analysis, some level of care must be exercised when including lags in a model.


So how many lags should be used in a model? There is no hard-and-fast-rule on the choice of lag length. It is basically an empirical issue. As noted in Damodar Gujarati Basic Econometrics, there is no a priori guide as to what the maximum length of the lag should be. The researcher must bear in mind that, as one estimates successive lags, there are fewer degrees of freedom left, making statistical inference somewhat unstable. Economists are usually not that lucky to have a long series of data so that they can go on estimating numerous lags. More importantly, in economic time series data, successive values (lags) tend to be highly correlated increasing the likelihood of multicollinearity in the model.

Also, from Jeffery Wooldridge’s Introductory Econometrics: A Modern Approach with annual data, the number of lags is typically small, 1 or 2 lags in order not to lose degrees of freedom. With quarterly data, 1 to 8 lags is appropriate, and for monthly data, 6, 12 or 24 lags can be used given sufficient data points. Again, in the words of Damodar Gujarati Basic Econometrics “the sequential search for the lag length opens the researcher to the charge of data mining”. He further stated that the nominal and true level of significance to test statistical hypotheses becomes an important issue in such sequential searches”. For instance, if the lag length, k, is incorrectly specified, the researcher will have to contend with the problem of misspecification errors. In addition, because of the lags involved, distributed and or autoregressive models raise the topic of causality in economic variables.

Hence, before you estimate a time series equation, it is necessary to decide on the maximum lag length. Like I mentioned earlier, this is purely an empirical question. Suppose there are 40 observations in all, by including too many lagged values, your model consumes degrees of freedom, not to mention introducing the likelihood of multicollinearity occurring. As noted in my previous tutorial on multicollinearity, it leads to imprecise estimation; that is, the standard errors tend to be inflated in relation to the estimated coefficients. As a result, based on the routinely computed t ratios, we may tend to declare (erroneously), that a lagged coefficient(s) is statistically insignificant. In the same vein, including too few lags will lead to specification errors. The easiest way out of this quagmire, is to decide using a criterion like the Akaike or Schwarz and choose that model that gives the lowest values of these criteria. Most econometric packages easily compute these optimal lag length but note some trial and error is inevitable.

Choosing Optimal Lags in Stata

For this tutorial, I will extract data from Gujarati and Porter Table 21.1 dataset. It is a quarterly data on United States from 1970 to 1991, which is 88 observations. The variables are gdp (gross domestic product), pdi (personal disposable income) and pce (personal consumption expenditure).

Step 1: Load data into Stata
 
Stata Dataset from cruncheconometrix.com.ng
Stata Dataset
Source: CrunchEconometrix
 Step 2: Prepare Stata for Analysis
Inform Stata that you are about to perform a time series analysis by typing this code into the Command box: tsset qtrly

and you will obtain this response:
Stata - tsset Command from cruncheconometrix.com.ng
Stata - tsset Command
Source: CrunchEconometrix
Stata now recognises that you are about conducting a time series analysis using quarterly data from 1st quarter of 1970 to the 4th quarter of 1991. If you don’t issue this command, Stata will not run your analysis.

Step 3: Obtain Model Lag Length
Type this code into the Command box: varsoc gdp pce pdi

…and the Stata output for the model (shown below) indicates that lag 2 is the optimal lag and that AIC is the best criterion for the model given it has the lowest value, 26.8144.

Stata - Optimal Lags for the Model from cruncheconometrix.com.ng
Stata - Optimal Lags for the Model
Source: CrunchEconometrix

Step 4: Obtain Variables Lag Length
Optimal lags can be obtained for the respective variables and the rule-of-thumb remains the same. We select that lag identified by the criterion which gives the lowest value.

So, for gdp, type this code into the Command box: varsoc gdp

…and the Stata output indicates that the optimal lag length for gdp is 2.

Stata - Optimal Lags for gdp from cruncheconometrix.com.ng
Stata - Optimal Lags for gdp
Source: CrunchEconometrix

 To obtain optimal lag for pce, type: varsoc pce

…Stata output indicates that the optimal lag length for pce is 4.

Stata - Optimal Lags for pce from cruncheconometrix.com.ng
Stata - Optimal Lags for pce
Source: CrunchEconometrix

 varsoc pdi
…Stata output indicates that the optimal lag length for pdi is 1.

Stata - Optimal Lags for pdi from cruncheconometrix.com.ng
Stata - Optimal Lags for pdi
Source: CrunchEconometrix

Caveat: There are also cases where the used lag length is that which is most selected by the criterion named after the econometricians who developed them, like HQ, SIC, AIC and LR, etc. Some researchers prefer Schwartz criterion when the variables are more than 4 and use the AIC when the variables are less than 4. As, mentioned in the introductory part of this tutorial, the decision on the choice of lag is purely an empirical issue. Generally, we choose the lag length for which the values of most of these lag length criteria are minimized, indicated by asterisks in the EViews output.

[Watch video tutorial on lag selection using Stata]

Having gone through this tutorial, it will be easy for you to determine the optimal lag for your model regardless of the analytical package used. The basics are the same. Remember that “Lag length criteria” indicate a definite way of selecting the optimal lag after estimating the initial VAR model (in EViews). Also VAR and ARDL models are susceptible to arbitrary use of lags as this may erode the degrees of freedom, weaken the significance of the coefficients, may induce auto-correlation and weaken the strength of diagnostic tests.

Try these steps on your models and if there are further and comments, do post them below…..