使用 R 中的循环获取 CCF 值
我有一个如下所示的数据框:
files | Time | Male | Female |
---|---|---|---|
A | 1.1 | 0 | 1 |
A | 1.2 | 0 | 1 |
A | 1.3 | 1 | 1 |
A | 1.4 | 1 | 0 |
B | 2.4 | 0 | 1 |
B | 2.5 | 1 | 1 |
B | 2.6 | 0 | 1 |
B | 2.7 | 1 | 1 |
“文件”列表示录音文件名,“时间”表示 0.1 秒的离散时间段, “男性”和“女性”列表示男性和女性在该时间段内是否正在呼叫(1)或没有呼叫(0)。
我想找出所有不同录音中女性和男性最相关的滞后。更具体地说,我希望输出是一个包含三列的数据框:记录文件名、女性和男性之间的峰值相关性得分以及滞后值(出现峰值相关性时)。
到目前为止,我可以单独测量文件的峰值互相关性:
file1 <- dataframe %>% filter(file == unique(dataframe$`Begin File`)[1])
#obtaining observations of first file entry
然后我使用以下函数来查找峰值相关性:
Find_Abs_Max_CCF <- function(a, b, e=0) {
d <- ccf(a, b, plot = FALSE, lag.max = length(a)/2)
cor = d$acf[,,1]
abscor = abs(d$acf[,,1])
lag = d$lag[,,1]
res = data.frame(cor, lag)
absres = data.frame(abscor, lag)
maxcor = max(absres$abscor)
absres_max = res[which(absres$abscor >= maxcor-maxcor*e & absres$abscor <= maxcor+maxcor*e),]
return(absres_max)
}
Find_Abs_Max_CCF(file1$f,file1$m,0.05)
有没有办法使用函数或循环来自动化该过程,以便我分别获得峰值相关值所有不同文件条目的滞后值? 非常感谢任何帮助。提前致谢。
编辑: 我使用 group_map() 函数和以下代码:
part.cor<-dataframe %>% group_by(files) %>% group_map(~Find_Abs_Max_CCF(datframe$f, dataframe$m, 0.05))
但是,它返回在整个输出数据帧中重复的峰值相关性和滞后的相同值。
I have a data frame which looks like this:
files | Time | Male | Female |
---|---|---|---|
A | 1.1 | 0 | 1 |
A | 1.2 | 0 | 1 |
A | 1.3 | 1 | 1 |
A | 1.4 | 1 | 0 |
B | 2.4 | 0 | 1 |
B | 2.5 | 1 | 1 |
B | 2.6 | 0 | 1 |
B | 2.7 | 1 | 1 |
The 'files' column represent recording file names, 'Time' represents discretised time bins of 0.1 seconds, the 'Male' and 'Female' column represents whether the male and female are calling (1) or not (0) during that time bin.
I want to find at which lags the female and male are most correlated for all different recordings. More specifically I want the output to be a dataframe with three columns: recording file names, peak correlation score between female and male, and the lag value (at which peak correlation occurred).
I have so far could measure the peak cross-correlation of the files individually:
file1 <- dataframe %>% filter(file == unique(dataframeI have a data frame which looks like this:
files
Time
Male
Female
A
1.1
0
1
A
1.2
0
1
A
1.3
1
1
A
1.4
1
0
B
2.4
0
1
B
2.5
1
1
B
2.6
0
1
B
2.7
1
1
The 'files' column represent recording file names, 'Time' represents discretised time bins of 0.1 seconds, the 'Male' and 'Female' column represents whether the male and female are calling (1) or not (0) during that time bin.
I want to find at which lags the female and male are most correlated for all different recordings. More specifically I want the output to be a dataframe with three columns: recording file names, peak correlation score between female and male, and the lag value (at which peak correlation occurred).
I have so far could measure the peak cross-correlation of the files individually:
Begin File`)[1])
#obtaining observations of first file entry
Then I used following function to find peak correlation:
Find_Abs_Max_CCF <- function(a, b, e=0) {
d <- ccf(a, b, plot = FALSE, lag.max = length(a)/2)
cor = d$acf[,,1]
abscor = abs(d$acf[,,1])
lag = d$lag[,,1]
res = data.frame(cor, lag)
absres = data.frame(abscor, lag)
maxcor = max(absres$abscor)
absres_max = res[which(absres$abscor >= maxcor-maxcor*e & absres$abscor <= maxcor+maxcor*e),]
return(absres_max)
}
Find_Abs_Max_CCF(file1$f,file1$m,0.05)
Is there a way to to use a function or loop to automate the process so that I get peak correlation value, respective lag value of all the distinct file entries?
Any help is highly appreciated. Thanks in advance.
Edits:
I used group_map() function with following code:
part.cor<-dataframe %>% group_by(files) %>% group_map(~Find_Abs_Max_CCF(datframe$f, dataframe$m, 0.05))
However, it is returning the same values of peak correlation and lag repeated throughout output dataframe.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论