繁体   English   中英

cor.test 中的栅格相关性和 p 值

[英]Raster correlation and p-values from cor.test

我正在尝试使用corcor.test两个光栅砖之间的像素级相关性和显着性(p 值)。 我的数据在这里:

它们都相当小,总共不到 2MB。

我在之前关于 StackOverflow 和 r-sig-geo 的讨论中发现了以下两个代码(均来自 Robert Hijmans):

#load data
require(raster)
sa <- brick("rain.grd")
sb <- brick("pet.grd")

# code 1
funcal <- function(xy) {
    xy <- na.omit(matrix(xy, ncol=2))
    if (ncol(xy) < 2) {
        NA
    } else {
        cor(xy[, 1], xy[, 2])
    }
}
s <- stack(sa, sb)
calc(s, funcal)
# code 2
stackcor <- function(s1, s2, method='spearman') { 
        mycor <- function(v) { 
                x <- v[1:split] 
                y <- v[(split+1):(2*split)] 
                cor(x, y, method=method) 
        } 
        s <- stack(s1, s2) 
        split <- nlayers(s)/2 
        calc(s, fun=mycor ) 
} 

两个代码都按预期使用cor函数生成相关网格。 但是,我尝试用cor.test替换cor以提取 p 值:

# using the first code
funcal <- function(xy) {
    xy <- na.omit(matrix(xy, ncol=2))
    if (ncol(xy) < 2) {
        NA
    } else {
        cor.test(xy[, 1], xy[, 2],method="pearson")$p.value
    }
}
s <- stack(sa, sb)
calc(s, funcal)

我遇到了以下错误(在 RStudio 中有引用):

Error in cor.test.default(xy[, 1], xy[, 2], method = "pearson") : 
  not enough finite observations 
8 stop("not enough finite observations") 
7 cor.test.default(xy[, 1], xy[, 2], method = "pearson") 
6 cor.test(xy[, 1], xy[, 2], method = "pearson") 
5 FUN(newX[, i], ...) 
4 apply(v, 1, fun) 
3 .local(x, fun, ...) 
2 calc(meistack, brick.cor.pval) 
1 calc(meistack, brick.cor.pval) 

在之前的 r-sig-geo 讨论中,用户曾询问过此错误,但未得到答复。 所以我再次询问并收到了对我的询问的回复,其中一位用户指出cor能够输入矩阵而cor.test不能,但即使在将数据转换为数字向量之后:

cor.pval <- function(xy) { # Pearson product moment correlation
  xy <- na.omit(as.matrix(xy, ncol=2))
  x <- xy[,1:11]
  y <- xy[,12:22]
  #  if (ncol(xy) < 2) {
   # NA
  #} else {
    cor.test(x[1,], y[1,])$p.value
  #}
}
calc(s, cor.pval)

我面临以下错误:

Error in .calcTest(x[1:5], fun, na.rm, forcefun, forceapply) : 
  cannot use this function

我想知道是否有人可以帮助解决这个问题?

我的 sessionInfo() 如下:

R version 3.0.1 (2013-05-16)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] car_2.0-18          nnet_7.3-7          MASS_7.3-29         rgdal_0.8-11       
 [5] plyr_1.8            rasterVis_0.21      hexbin_1.26.2       latticeExtra_0.6-26
 [9] RColorBrewer_1.0-5  lattice_0.20-23     raster_2.1-49       maptools_0.8-27    
[13] sp_1.0-13          

loaded via a namespace (and not attached):
[1] foreign_0.8-55 tools_3.0.1    zoo_1.7-10    

谢谢!

使用raster包中的corLocal函数。

corLocal(堆栈1,堆栈2,测试= T)

返回的结果是具有两层的 rasterBrick,第一个 'r' 值和第二个 p.value 在此处输入图片说明

此处的区别在于,它们与空向量的行为不同。

cor(numeric(0), numeric(0))
# -> NA
cor.test(numeric(0), numeric(0))
#->   Error in cor.test.default(numeric(0), numeric(0)) : 
#       not enough finite observations

您的na.omit似乎可以从某些组合中删除所有记录。 现在,您只需要检查列数,还应该检查是否有行。

这个

funcal <- function(xy) {    
    xy <- na.omit(matrix(xy, ncol=2))
    if (ncol(xy) < 2 | nrow(xy)<1) {
        NA
    } else {
        cor.test(xy[, 1], xy[, 2], method="pearson")$p.value
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM