Cross-lagged Pearson correlation in R

87 Views Asked by At

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

set.seed(123)
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
library(testcorr)
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)

Perhaps this question should fit Cross Validated better, but ultimately I would like to get advice on how to code this using R. Thank you!

0

There are 0 best solutions below