简体   繁体   中英

cross-correlation for list of data frames

I have three data frames Tushka , ARQE and ARQW

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

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.

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.

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)
  }
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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