r - Raster correlation and p-values from cor.test -


i trying pixel-wise correlations , significance (p-value) between 2 raster bricks using cor , cor.test. data here:

they're both small, less 2mb altogether.

i found following 2 codes (both robert hijmans) previous discussions on stackoverflow , r-sig-geo:

#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 )  }  

both codes work expected cor function producing correlation grids. however, tried substituting cor cor.test in order extract p-values:

# using 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) 

i met following error (with trackback in 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)  

in previous r-sig-geo discussion, user had asked error left unanswered. asked again , received 1 response inquiry user pointed out cor able input matrices , cor.test cannot, after converting data numeric vector:

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) 

i faced following error:

error in .calctest(x[1:5], fun, na.rm, forcefun, forceapply) :    cannot use function 

i wondering if can this?

my sessioninfo() below:

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 namespace (and not attached): [1] foreign_0.8-55 tools_3.0.1    zoo_1.7-10     

thanks!

the difference here behave differently empty vectors.

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 

it seems na.omit can remove records combinations. right check number of columns, when should check see if there rows.

this

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     } } 

Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -