简体   繁体   English

是否有使用 Spearman 方法计算栅格 p 值的函数?

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

I can not succeed to calculate the p value for the Spearman correlation for a series of the few rasters.我无法成功计算一系列少数栅格的 Spearman 相关性的 p 值。 I created a code to make spearman correlation.我创建了一个代码来进行 spearman 相关。 It works, but if I want to show me p value, using cor.test (), I have a error.它有效,但是如果我想使用 cor.test() 向我显示 p 值,则会出现错误。

A reproducible example using a raster with 6 layers to compute the correlation between the cells of layers 1,2,3 and 4,5,6.使用 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

The function used in calc must return a number (or numbers). calc使用的函数必须返回一个数字(或多个)。 cor returns a number, but cor.test does not. 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 

So we need to extract the p value from the object returned by cor.test .所以我们需要从cor.test返回的对象中提取 p 值。 If you look at ?cor.test or inspect str(x) you can see that you can use如果你查看?cor.test或 inspect str(x)你可以看到你可以使用

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

And thus you can do因此你可以做

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