0
votes

I am creating ASCII files to include in a MaxEnt model in R.

When I create the layers and look at the raster values I can see them and they are correct, e.g.:

> r_test3
class      : RasterLayer 
dimensions : 4698, 3919, 18411462  (nrow, ncol, ncell)
resolution : 50, 50  (x, y)
extent     : 159500, 355450, 160454.6, 395354.6  (xmin, xmax, ymin, ymax)
crs        : +proj=tmerc +lat_0=49 +lon_0=-2 +k=0.999601272 +x_0=400000 +y_0=-100000 +datum=OSGB36 +units=m +no_defs +ellps=airy +towgs84=446.448,-125.157,542.060,0.1502,0.2470,0.8421,-20.4894 
source     : memory
names      : layer 
values     : 1, 43385  (min, max)

then when I stack the three rasters everything seems to be correct:

RegionalGridsBest
class      : RasterStack 
dimensions : 4698, 3919, 18411462, 3  (nrow, ncol, ncell, nlayers)
resolution : 50, 50  (x, y)
extent     : 159500, 355450, 160454.6, 395354.6  (xmin, xmax, ymin, ymax)
crs        : +proj=tmerc +lat_0=49 +lon_0=-2 +k=0.999601272 +x_0=400000 +y_0=-100000 +datum=OSGB36 +units=m +no_defs +ellps=airy +towgs84=446.448,-125.157,542.060,0.1502,0.2470,0.8421,-20.4894 
names      : layer.1, layer.2, layer.3 
min values :       1,       1,       1 
max values :   43385,   92917,   95704 

However when I getValues for the rasters they just show NAs:

> getValues(RegionalGridsBest)
            layer.1 layer.2 layer.3
       [1,]      NA      NA      NA
       [2,]      NA      NA      NA
       [3,]      NA      NA      NA
       [4,]      NA      NA      NA
       [5,]      NA      NA      NA
       [6,]      NA      NA      NA
       [7,]      NA      NA      NA
       [8,]      NA      NA      NA
       [9,]      NA      NA      NA
      [10,]      NA      NA      NA
      [11,]      NA      NA      NA
      [12,]      NA      NA      NA
      [13,]      NA      NA      NA

Then due to this, I get this error when I try and use the vifstep function:

vifstep(RegionalGridsBest, th = 3, maxobservations = 10000) 

Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 
  0 (non-NA) cases

I am not sure how tog et around this or wha tI am doing wrong.

EDIT

NA values are in the files despite being set to 0 values when creating the layer:

> summary(RegionalGridsBest)
Error in apply(object@data@values, 2, quantile, na.rm = TRUE) : 
  no slot of name "data" for this object of class "RasterStack"

> summary(r_test3)
             layer
Min.           1.0
1st Qu.    18407.0
Median     28176.5
3rd Qu.    41293.0
Max.       43385.0
NA's    17850028.0

> summary(r_test2)
           layer
Min.           1
1st Qu.    40505
Median     58967
3rd Qu.    75139
Max.       92917
NA's    17086764

> summary(r_test)
           layer
Min.           1
1st Qu.    25570
Median     51215
3rd Qu.    66486
Max.       95704
NA's    17980349
1

1 Answers

0
votes

What you are showing is that the first 13 cells are NA --- that is quite common. I suspect that one of your layers has only NA's. Can you inspect/show

summary(RegionalGridsBest)

It clearly has to do with NA values. From the example

library(usdm)
file <- system.file("external/spain.grd", package="usdm")
r <- stack(file)
v <- vifstep(r, th=10)

If one layer is all NA it fails

r[[1]][] <- NA
v <- vifstep(r, th=10)
#Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 
#  0 (non-NA) cases

But also where there is no cell that without NA's

r <- stack(file)
r[[1]][1:500] <- NA
r[[2]][450:870] <- NA
v <- vifstep(r, th=10)
#Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 
#  0 (non-NA) cases

You can check for that with

 tst <- any(is.na(RegionalGridsBest))

The cells should not all be 1 (TRUE), there should be some cells with 0 (FALSE) for vifstep to work.