0

I am doing species distribution modeling (ecological niche modeling) where I project a model to either current or future climate rasters (Bioclim variables).

When I predict to the current rasters (object=bio_stack), everything works, using this code:

#predict species current distribution using bio_stack
current_dist<-predict(object=bio_stack, model=mod_gam,filename="whitebark_current_dist_17April2014", 
           na.rm=TRUE, type="response", format="GTiff", overwrite=TRUE,
           progress="text")

However, when I predict to the future rasters (object=future_bio_stack), this code does not produce the results:

#predict species future distribution using future_bio_stack
future_dist<-predict(object = future_bio_stack, model=mod_gam,filename="whitebark_future_dist_17April2014", 
           fun=predict, na.rm=TRUE, type="response", format="GTiff", overwrite=TRUE,
           progress="text") 

Instead, I get this warning message:

In addition: Warning message:
In predict.gam(model, blockvals, ...) :
  not all required variables have been supplied in  newdata!

The future biostack layers to which I am projecting look ok in an image, with normal min and max values (I can not post the image without a reputation score).

However, the min and max values of the future_bio_stack look strange in the summary, especially compared to the bio_stack. Here is a summary of each of the stacks, with the min and max values provided:

> future_bio_stack

class       : RasterStack 
dimensions  : 4800, 5880, 28224000, 5  (nrow, ncol, ncell, nlayers)
resolution  : 0.008333333, 0.008333333  (x, y)
extent      : -152, -103, 30, 70  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +ellps=WGS84 +no_defs 
names       : WC05future_clipped, WC06future_clipped, WC08future_clipped,     WC13future_clipped, WC15future_clipped 
min values  :             -32768,             -32768,                 -32768,             -32768,             -32768 
max values  :              32767,              32767,                  32767,              32767,              32767 

> bio_stack
class       : RasterStack 
dimensions  : 4800, 5880, 28224000, 5  (nrow, ncol, ncell, nlayers)
resolution  : 0.008333333, 0.008333333  (x, y)
extent      : -152, -103, 30, 70  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
names       : WC05_clipped, WC06_clipped, WC08_clipped, WC13_clipped, WC15_clipped 
min values  :          -56,         -429,         -145,            7,            5 
max values  :          457,          100,          332,          767,          129 

Any ideas why bio_stack is working with the predict function but future_bio_stack is not?

horseoftheyear
  • 917
  • 11
  • 23
user3545679
  • 181
  • 1
  • 12

1 Answers1

0

I am answering my own question here, as I just figured this out.

In order to project the trained model (which used bio_stack) to another set of rasters (to eg. future_bio_stack), the column names of each of the stacks have to be the same.

I renamed the files AND re-stacked the rasters of the future_bio_stack, making the names the same as those in bio_stack:

> bio5future<-raster("WC05_clipped.tif") 
> bio6future<-raster("WC06_clipped.tif")
> bio8future<-raster("WC08_clipped.tif")
> bio13future<-raster("WC13_clipped.tif")
> bio15future<-raster("WC15_clipped.tif")
> 
> future_bio_stack<-stack(bio5future, bio6future, bio8future,
> bio13future, bio15future)

I then re-ran the predict function, using future_bio_stack as my object, and it worked.

user3545679
  • 181
  • 1
  • 12