How to correlate two time series, with possible time differences The question is similar to How to correlate two time series with gaps and different time bases? but with regular sample frequency and identical time base between two time series.
Say I have two time series, one is the stock price of a given company, the other is the ratio of positive comment about the company on social networks. How can I statistically verify they are correlated? Assume they are both sampled weekly or daily. The challenge lies in the fact there might be time difference from each other. In the context of my example above, the positive evaluation from consumer might be later reflect on the stock price.
There are two things I want to investigate in this problem:
1) is the two time series are correlated? 2) what is the time window to maximize their correlation?
 A: The cross correlation function will give you the Pearson correlation for 2 time-series at different time lags. The R function is ccf().
For further study, a Granger causality test tries to determine a cause-effect relationship between the 2 correlated series by first removing the serial correlation in TS1 (the stock price series in this case).
A: Apply a lag operator on one time series, with the other fixed, and calculate the coherence of the cross-spectrum achieved against each lag. Find the lag that gives you the maximum coherence and interpret it.
Coherence is computed at each frequency-and hence is a vector. Hence, a sum of a weighted coherence would be a good measure. You would typically want to weight the coherences at frequencies that have a high energy in the power spectral density. That way, you would be measuring the similarities at the frequencies that dominate the time series instead of weighting the coherence with a large weight, when the content of that frequency in the time series is negligible.
http://www.stat.rutgers.edu/home/rebecka/Stat565/lab5-2007.pdf is a good link to look at to get started and
http://www.atmos.washington.edu/~dennis/552_Notes_6c.pdf is an excellent introduction to cross-spectral analysis.
