[英]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. 我已经写了一个for循环来完成这项工作,但是计算时间很慢。
eg i want to run the CCF function on s1 and s2, then s3 and s4. 例如,我想在s1和s2上运行CCF功能,然后在s3和s4上运行。 real data has >100 columns and >50,000 rows, each pair of columns has a unique number of rows. 实际数据有> 100列和> 50,000行,每对列都有唯一的行数。 also running monte carlo simulations and plotting max CCFs/lags, but for brevity i didnt include that code here. 还运行蒙特卡洛模拟并绘制最大CCF /滞后,但为简便起见,我没有在此处包含该代码。 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? 我确实运行了探查器,而CCF似乎花费了最多的时间,我希望apply函数可以帮助解决这个问题?
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
. 我首先将ccf
的plot
参数设置为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. 然后,我将检查ccf
的代码(这是纯R代码),看是否可以编写更多的准系统版本。 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. 例如,参见R附带的parallel
软件包的帮助。
vignette("parallel")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.