[英]Calculating running window Spearman correlation and pvalue in R
I wish to calculate a running window Spearman correlation in R. So far I've been using running
from gtools
package, but I could only get Pearson correlation out of it. 我希望在R中计算一个正在运行的窗口Spearman相关性。到目前为止,我一直在使用gtools
包中的running
,但是我只能从中获取Pearson相关性。 I tried to modify the fun
parameter but couldn't get it to produce anything but errors. 我试图修改fun
参数,但除了错误外无法使其产生任何结果。
My input is a data.frame: 我的输入是一个data.frame:
row.names Small Large
1 1 97.80341 88.71192
2 2 97.46807 87.96206
3 3 97.18862 88.13904
4 4 97.76615 87.67329
5 5 97.09081 87.52425
6 6 97.16067 87.85493
7 7 97.73820 88.43712
etc. And this is the basic running command I tried to manipulate: 等等。这是我尝试操作的基本运行命令:
corr_table <- running (mytable$Large, mytable$Small,fun=cor, width=20, by=10, allow.fewer=FALSE, pad=FALSE, align="left")
My second question is how can I add the pvalue of each "window" ? 我的第二个问题是如何添加每个“窗口”的pvalue?
Thanks! 谢谢!
OK found a solution to both questions: OK找到了两个问题的解决方案:
corr_table <- running (mytable$Large, mytable$Small,fun=cor.test, method="spearman", width=60, by=30, allow.fewer=FALSE, pad=FALSE, align="left")
newlist <- t(corr_table)
newlist <- as.matrix (newlist)
y <- newlist[,4]
x <- seq(from=1, to=92) #change the "to" according to number of values in Y
pvalue <- newlist[,3]
plot(x,y, xlim=c(1,92),ylim=c(-1,1), col=ifelse(pvalue < 0.05, "red", "black")) #change the "xlim" according to number of values in Y
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.