简体   繁体   English

数据帧列表的互相关

[英]cross-correlation for list of data frames

I have three data frames Tushka , ARQE and ARQW 我有三个数据帧TushkaARQEARQW

I used cross-correlation code for every data frame separately like this 我像这样分别为每个数据帧使用互相关代码

# cross correlations with US column

#Tushka

head(Tushka)

#  US    PW1    PW2    PW3    PW4

#1 173.62 153.01 144.65 152.53 137.05

#2 173.57 152.97 144.64 152.52 137.10

#3 173.52 152.95 144.64 152.52 137.11

lag = 200

t3 = data.frame(lag = c(-lag:lag))

vars = names(Tushka[, -1])

for (j in vars) {

 t4 = ccf(Tushka[, 1], Tushka[, j], lag = lag, na.action = na.pass)

 t3[, j] = data.frame(t4$acf)[, 1]

}

But if I create a list of the three data frame, how I can use cross-correlation for the list of data frames using for-loop or any other function 但是,如果我创建三个数据帧的列表,那么如何使用for循环或任何其他函数对数据帧列表使用互相关

Here is some reproducible sample data: 以下是一些可重现的样本数据:

n <- 100
data_list <- list(
  d1 = data.frame(x = runif(n)),
  d2 = data.frame(x = runif(n)),
  d3 = data.frame(x = runif(n))
)

The trick is to use expand.grid to get pairs of data frames. 诀窍是使用expand.grid来获取数据帧对。

pairs <- expand.grid(first = names(data_list), second = names(data_list))

Now you can loops over rows of pairs , running cross correlation for each pair of data frames named in that row. 现在,您可以遍历pairs行,为该行中命名的每对数据帧运行互相关。

lapply(
  seq_len(nrow(pairs)),
  function(i)
  {
    first_data <- data_list[[pairs[i, "first"]]]
    second_data <- data_list[[pairs[i, "second"]]]
    ccf(first_data$x, second_data$x, plot = FALSE)
  }
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM