簡體   English   中英

計算R中的運行窗口Spearman相關性和p值

[英]Calculating running window Spearman correlation and pvalue in R

我希望在R中計算一個正在運行的窗口Spearman相關性。到目前為止,我一直在使用gtools包中的running ,但是我只能從中獲取Pearson相關性。 我試圖修改fun參數,但除了錯誤外無法使其產生任何結果。

我的輸入是一個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

等等。這是我嘗試操作的基本運行命令:

corr_table <- running (mytable$Large, mytable$Small,fun=cor, width=20, by=10, allow.fewer=FALSE, pad=FALSE, align="left")

我的第二個問題是如何添加每個“窗口”的pvalue?

謝謝!

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM