簡體   English   中英

是否有使用 Spearman 方法計算柵格 p 值的函數?

[英]Is there a function to calculate the p-value of the rasters with the Spearman method?

我無法成功計算一系列少數柵格的 Spearman 相關性的 p 值。 我創建了一個代碼來進行 spearman 相關。 它有效,但是如果我想使用 cor.test() 向我顯示 p 值,則會出現錯誤。

使用 6 層柵格計算第 1、2、3 和 4、5、6 層像元之間相關性的可重現示例。

library(raster)
set.seed(89)
z <- brick(nrow=10, ncol=10, nl=6)
values(z) <- runif(600)

### Spearman correlation 
r <- calc(z, fun=function(x) cor(x[1:3], x[4:6], method='spearman'))

### p-value -- here I have the error
r.pvalue <- calc(z, fun=function(x) cor.test(x[1:3], x[4:6], method='spearman'))
#Error in setValues(out, x) : 
#  values must be numeric, integer, logical or factor

calc使用的函數必須返回一個數字(或多個)。 cor返回一個數字,但cor.test不返回。

cor(1:3, 3:1)
#[1] -1

x <- cor.test(1:3, 3:1)
class(x)
#[1] "htest"
x
#        Pearson's product-moment correlation
#
#data:  1:3 and 3:1
#t = -Inf, df = 1, p-value < 2.2e-16
#alternative hypothesis: true correlation is not equal to 0
#sample estimates:
#cor 
# -1 

所以我們需要從cor.test返回的對象中提取 p 值。 如果你查看?cor.test或 inspect str(x)你可以看到你可以使用

cor.test(1:3, 3:1)$p.value
#[1] 0

因此你可以做

r.pvalue <- calc(z, fun=function(x) cor.test(x[1:3], x[4:6], method='spearman')$p.value)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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