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)
)
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)]
#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)
#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)
cc.test(data[session == 1, var1], data[session == 2, var2], max.lag = 1)