Re:implementation of Granger causality tests in Mathematica
- To: mathgroup at smc.vnet.net
- Subject: [mg64077] Re:[mg63992] implementation of Granger causality tests in Mathematica
- From: Darren Glosemeyer <darreng at wolfram.com>
- Date: Tue, 31 Jan 2006 01:14:24 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
Hi Johann, The following shows an example of the Granger test. First, take two series ydata and xdata. In[1]:= ydata = Table[Random[], {50}]; In[2]:= xdata = Table[Random[], {50}]; In[3]:= lag[data_, i_] := Drop[data, -i] The test is performed for a given lag. We will use 3 for this example. In[4]:= maxlag = 3; Fitting will be done using Regress, so we will take the original data and create a new data set with entries of the form {ylag1, ylag2, ylag3, xlag1, xlag2, xlag3, y}. In[5]:= laggeddata = Transpose[ Map[Take[#, -Length[ydata] + maxlag] &, Join[Table[lag[ydata, i], {i, maxlag}], Table[lag[xdata, i], {i, maxlag}], {ydata}]]]; Now, the two models to fit are the model with all three x and y lags, and the model without the x lags. In[6]:= << Statistics` In[7]:= rssu = Total[(resids1 = FitResiduals /. Regress[laggeddata, {y1, y2, y3, x1, x2, x3}, {y1, y2, y3, x1, x2, x3}, RegressionReport -> FitResiduals])^2] Out[7]= 3.43417 In[8]:= rssg = Total[(resids2 = FitResiduals /. Regress[laggeddata, {y1, y2, y3}, {y1, y2, y3, x1, x2, x3}, RegressionReport -> FitResiduals])^2] Out[8]= 3.58539 The test statistic only requires the residual sums of squares for each of these models, so that is all that I have saved from the fitting. Other diagnostics can be saved as well if desired. Here is the test statistic in this example. In[9]:= grangerstat = ((rssg - rssu)/ maxlag)/(rssu/(Length[laggeddata] - 2*maxlag - 1)) Out[9]= 0.587118 The p-value is obtained from FRatioDistribution[maxlag, Length[laggeddata] - 2*maxlag - 1]. In[10]:= 1 - CDF[FRatioDistribution[maxlag, Length[laggeddata] - 2*maxlag - 1], grangerstat] Out[10]= 0.626993 Darren Glosemeyer Wolfram Research