以下是引用hanszhu在2006-5-16 13:12:00的发言:I'm looking for a step-by-step explanation on how to perform a Granger causality test using SPSS, Mathematica, Java or Perl.
After writing it, I realised you probably needed more than that, so here goes.
I will explain this in SPSS. Not everything can be done automatically, so you have to do a bit of work yourself. (That's the price you pay for using SPSS, not SAS.)
Say your two variables that you want to test the direction of causality are called A and B. Create a variable which is the lag of A (let's call it LAG_A) and another which is the lag of B (let's call it LAG_B. In SPSS syntax:
Compute lag_A = lag(A).
Compute lag_B = lag(A).
Now run a regression analysis, where A is the DV, and lag_A is the IV. This is the unrestricted resgression. Look at the residual sum of squares from this analysis, and call this RSS_r.
Now run another regression analysis, where A is the DV, and lag_A and lag_B are the IVs - this is the unrestricted analysis. Look at the residual sum of squares from this analysis, and call it RSS_ur.
Now, you need to calculate F:
F=((RSS_r-RSS_ur)/m)/(RSS_ur/(n-k))
Where m is the number of lagged terms - currently 1, but we will come back to that. n is the number of cases in the analysis (careful, because you have lost one, because of the lag), and k is the number of parameters in the unrestricted regression (i.e. 2 in this case.)
This F is distributed with m, n-k df.
You can include more lagged terms - lag_2a = lag(lag(A)), for example. Hence m will increase. Always include both the A and B lagged terms in the equation. You can also include other covariates - include them in both analyses. When you have done that, do it all again backwards. If one is significant, you have unidirectional causality. If both are, you have bidirectional causality (or feedback), if neither are, you have independence. I hope all that makes sense - if not request clarification.
It could be done in Jave or Perl, but you would need to either write, or find, the algorithms to do the appropriate matrix algebra - and that might be another question. You would also need to know the algebra to do, and that's probably another question yet.