2

I am performing pairwise spatial correlation of 6 rasters using the raster.modified.ttest() function in R, as per this very useful post.

I have consistently had the following error with my data;

Error in SpatialPack::modified.ttest(x.var, y.var, sp::coordinates(x[nb[[i]],  : 
  NA/NaN/Inf in foreign function call (arg 6)

and yet no errors with any dummy data. The following will yield a result;

s.pix1 <- as(raster(xmn=0,xmx=10,ymn=0,ymx=10,res=1,vals=sample(1:5,100,replace=T)), "SpatialGridDataFrame")
s.pix1[sample(1:100,25)]<-NA
s.pix2 <- as(raster(xmn=0,xmx=10,ymn=0,ymx=10,res=1,vals=sample(1:5,100,replace=T)), "SpatialGridDataFrame")
s.pix2[sample(1:100,25)]<-NA

corr <- raster.modifed.ttest(x=s.pix1, y=s.pix2)

plot(raster(corr), main="spatially adjusted raster correlation")

Very small sample areas of my data would also yield results. However forcing larger contiguous areas of NA data into the dummy data recreated the error;

s.pix1 <- as(raster(xmn=0,xmx=10,ymn=0,ymx=10,res=1,vals=sample(1:5,100,replace=T)), "SpatialGridDataFrame")
s.pix1[c(1:5,11:15,21:25,31:35,41:45)]<-NA
s.pix2 <- as(raster(xmn=0,xmx=10,ymn=0,ymx=10,res=1,vals=sample(1:5,100,replace=T)), "SpatialGridDataFrame")
s.pix2[c(1:5,11:15,21:25,31:35,41:45)]<-NA

corr <- raster.modifed.ttest(x=s.pix1, y=s.pix2)

Error in SpatialPack::modified.ttest(x.var, y.var, sp::coordinates(x[nb[[i]],  : 
  NA/NaN/Inf in foreign function call (arg 6)

Could be a bug? @JeffreyEvans provided the excellent answer in the linked post

4
  • 2
    I am working on "retooling" this function so that it does not require spdep to construct a neighbor matrix but rather takes advantage of some of the base functionality in the raster package. The focal function itself, does not allow for more than one raster however, getValuesFocal will return an array if a stack or brick is passed to it. Using this approach is speeding up multivariate focal operations considerably. Unfortunately, I am swamped but will move this up on my ever-growing to-do list and post it in the GitHub development version of spatialEco. Commented Apr 18, 2019 at 14:19
  • Thanks @JeffreyEvans and good luck!
    – Sam
    Commented Apr 23, 2019 at 7:00
  • @JeffreyEvans, Thank you for this! Any news about this? I do run on the same issue.
    – geo_dd
    Commented Jul 17, 2019 at 13:07
  • These changes were made to the function several releases ago thus, making this a non-issue. The function now uses terra SpatRaster class objects. Commented Nov 27, 2023 at 2:09

1 Answer 1

0

This is an old post, but I am just sharing a similar experience for whoever will face this type of situation. My error message was:

Error in h(simpleError(msg, call)) : error in evaluating the argument 'x' in selecting a method for function 'raster': NA/NaN/Inf in foreign function call (arg 6)
In addition: Warning message:
In cor(cbind(x, y)) : the standard deviation is zero

Such an error occurred when either one of the rasters had repeating values (NA or identical values) over certain times within a given window. The workaround was to set "d" parameter larger than what "AUTO" option would set.

For instance, my raster had a cell-size of 1000 (meters), and if I used the "Auto" setting d would have been 4242.641. However, I found out that some cases required setting d over 9000 just to avoid crashing.

1
  • Sometime ago I added code to catch this error, comming from the stats::cor function, and simply write a NA value for the given pixel(s). This makes it so the function does not fail with an error in the face of zero sd or variance. Commented Nov 27, 2023 at 2:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.