I have a dataset where I have two recordings (sessions) of two different variables.

data <- data.table(
    id = rep(1:20, each = 2),
    session = rep(1:2, times = 20),
    var1 = sample(1:100, size = 40, replace = TRUE),
    var2 = sample(5:10,  size = 40, replace = TRUE)

If I want to know the correlation between two variables at the same time point, I can simply calculate a Pearsons correlation:

#Cross-sectional Pearson correlation
data[session == 1, cor.test(var1, var2)]

However, if I want to know the correlation between var1 and var2 at different time points, should I use a cross-lagged Pearson correlation? And if so, how do I determine the lag?

I imagine it would look like so:

#Cross-lagged Pearson correlation
cc.test(data[session == 1, var1], data[session == 2, var1], max.lag = 1)

Since there are only two session, and the time lag between the two is 1 session, I imagine the lag should be defined as 1?

How about when I want to correlate the value of var1 at session 1 with the value of var2 at session 2? Do I use the same cross-lagged correlation?

cc.test(data[session == 1, var1], data[session == 2, var2], max.lag = 1)
  • 1
    $\begingroup$ Thank you for accepting my answer and awarding the bounty. Next time consider waiting a bit longer before awarding the bounty. Even if my answer does answer your question, there was still a chance someone else would post an even better answer. Anyway, I am glad you found my answer helpful! $\endgroup$ Commented Feb 3, 2023 at 13:35

1 Answer 1


If you only have two time points, the lag order can only be zero (no lag, thus contemporaneous relationship) or one.

While I have never used cc.test before, I wonder if it is applicable to your data in its current format. Your data is not stored as a time series. The rows do not increase in time; instead, they alternate between session=1 and session=2.

If you want to test $H_0\colon \text{Corr}(y_t,x_s)=0$ for some time points $t$ and $s$, you can run a linear regression of $y_t$ on $x_s$ and inspect whether the slope coefficient is different from zero. In your case that would be

model <- lm(data[session == 1, var1]~data[session == 2, var2])

For your data, I get

                         Estimate Std. Error t value Pr(>|t|)   
(Intercept)                79.904     23.332   3.425  0.00302 **
data[session == 2, var2]   -4.316      3.026  -1.426  0.17090   

which means the relevant $p$-value is 0.17090, above the conventional significance levels of 0.05 or 0.01. Thus $H_0$ of zero correlation between var1 in session 1 and var2 in session 2 would not be rejected.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.