title: Raster correlation and p-values from cor.test
body: <p>I am trying to get pixel-wise correlations and significance (p-value) between two raster bricks using <code>cor</code> and <code>cor.test</code>. My data are here: </p>
  
  <ul>
  <li><a href="https://www.dropbox.com/s/bzgz2w7ao8kld27/pet.zip" rel="nofollow">Brick 1</a></li>
  <li><a href="https://www.dropbox.com/s/jyuc0u0pt3bozn4/rain.zip" rel="nofollow">Brick 2</a></li>
  </ul>
  
  <p>They're both fairly small, less than 2MB altogether.</p>
  
  <p>I found the following two codes (both from Robert Hijmans) from a previous discussions on StackOverflow and r-sig-geo: </p>
  
  <pre><code>#load data
  require(raster)
  sa &lt;- brick("rain.grd")
  sb &lt;- brick("pet.grd")
  
  # code 1
  funcal &lt;- function(xy) {
      xy &lt;- na.omit(matrix(xy, ncol=2))
      if (ncol(xy) &lt; 2) {
          NA
      } else {
          cor(xy[, 1], xy[, 2])
      }
  }
  s &lt;- stack(sa, sb)
  calc(s, funcal)
  # code 2
  stackcor &lt;- function(s1, s2, method='spearman') { 
          mycor &lt;- function(v) { 
                  x &lt;- v[1:split] 
                  y &lt;- v[(split+1):(2*split)] 
                  cor(x, y, method=method) 
          } 
          s &lt;- stack(s1, s2) 
          split &lt;- nlayers(s)/2 
          calc(s, fun=mycor ) 
  } 
  </code></pre>
  
  <p>Both codes work as expected with the <code>cor</code> function producing correlation grids. However, I tried substituting the <code>cor</code> with <code>cor.test</code> in order to extract the p-values:</p>
  
  <pre><code># using the first code
  funcal &lt;- function(xy) {
      xy &lt;- na.omit(matrix(xy, ncol=2))
      if (ncol(xy) &lt; 2) {
          NA
      } else {
          cor.test(xy[, 1], xy[, 2],method="pearson")$p.value
      }
  }
  s &lt;- stack(sa, sb)
  calc(s, funcal)
  </code></pre>
  
  <p>I am met with the following error (with trackback in RStudio):</p>
  
  <pre><code>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) 
  </code></pre>
  
  <p>In a previous r-sig-geo discussion, a user had asked about this error but it was left unanswered. So I asked again and received one response to my inquiry where a user pointed out that <code>cor</code> is able to input matrices and <code>cor.test</code> cannot, but even after converting the data into a numeric vector:</p>
  
  <pre><code>cor.pval &lt;- function(xy) { # Pearson product moment correlation
    xy &lt;- na.omit(as.matrix(xy, ncol=2))
    x &lt;- xy[,1:11]
    y &lt;- xy[,12:22]
    #  if (ncol(xy) &lt; 2) {
     # NA
    #} else {
      cor.test(x[1,], y[1,])$p.value
    #}
  }
  calc(s, cor.pval)
  </code></pre>
  
  <p>I am faced with the following error:</p>
  
  <pre><code>Error in .calcTest(x[1:5], fun, na.rm, forcefun, forceapply) : 
    cannot use this function
  </code></pre>
  
  <p>I was wondering if anyone can help with this? </p>
  
  <p>My sessionInfo() is below:</p>
  
  <pre><code>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    
  </code></pre>
  
  <p>Thanks!</p>