简体   繁体   中英

how to use apply on cross correlation function in R

I am attempting to run multiple cross correlation functions across pairs of columns from a large data frame. I have written a for loop that does the job, but the computational time is slow.

eg i want to run the CCF function on s1 and s2, then s3 and s4. real data has >100 columns and >50,000 rows, each pair of columns has a unique number of rows. also running monte carlo simulations and plotting max CCFs/lags, but for brevity i didnt include that code here. I did run a profiler and the CCF appears to take the most time to run, i was hoping an apply function could help resolve this?

blahthresholdtest <- data.frame("S1"=1:8, "S2" = (8:1), "S3"= 1:8, "S4"= 9:2)

for loop i have written:

     k=1
  for(i in seq(1,ncol(blahthresholdtest),2)){
    ccftime <- ccf(blahthresholdtest[,i],blahthresholdtest[,i+1], type="correlation", na.action=na.omit)
    #adds one to k each loop to shift columns for analysis over
    k <- k + 1
  }

I would first set the plot argument of ccf to FALSE . Then I would inspect the code of ccf (which is pure R code) to see whether a more bare-bones version could be written. If that does not help, you could check whether you can run the loop in parallel; see for instance the help for package parallel , which comes with R.

vignette("parallel") 

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