rspatial / predicts

Methods for spatial prediction
GNU General Public License v3.0
10 stars 5 forks source link

bcvars does not seem to be able to handle missing data #15

Closed ptitle closed 1 year ago

ptitle commented 1 year ago

Here I generate some random SpatRasters, and add some missing cells.

tmn <- tmx <- prc <- rast(nrow=10, ncol=10, nlyr=12)
values(tmn) <- replicate(12, runif(ncell(tmn), 0,50))
values(tmx) <- replicate(12, runif(ncell(tmx), 0,50))
values(prc) <- replicate(12, runif(ncell(prc), 0,2000))
naCell <- sample(1:ncell(tmn), 10)
values(tmn)[naCell] <- NA
values(tmx)[naCell] <- NA
values(prc)[naCell] <- NA

b <- bcvars(prc, tmn, tmx)
Error in if (m == 0) { : missing value where TRUE/FALSE needed
rhijmans commented 1 year ago

Thanks, I now get:


library(predicts)
#Loading required package: terra
#terra 1.7.50
tmn <- tmx <- prc <- rast(nrow=10, ncol=10, nlyr=12)
values(tmn) <- replicate(12, runif(ncell(tmn), 0,50))
values(tmx) <- replicate(12, runif(ncell(tmx), 0,50))
values(prc) <- replicate(12, runif(ncell(prc), 0,2000))
naCell <- sample(1:ncell(tmn), 10)
## you should not use  
## values(tmn)[naCell] <- NA
tmn[naCell] <- NA
tmx[naCell] <- NA
prc[naCell] <- NA

b <- bcvars(prc, tmn, tmx)
b
#class       : SpatRaster 
#dimensions  : 10, 10, 19  (nrow, ncol, nlyr)
#resolution  : 36, 18  (x, y)
#extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
#coord. ref. : lon/lat WGS 84 
#source(s)   : memory
#names       :     bio1,      bio2,      bio3,      bio4,     bio5,       bio6, ... 
#min values  : 17.50191, -12.60698, -38.25027,  479.0143, 28.85657,  0.0239315, ... 
#max values  : 30.18866,  12.07755,  26.61873, 1457.8055, 49.88754, 18.9475465, ... 
ptitle commented 1 year ago

Thanks!