cjcarlson / embarcadero

🌲🌉 Species distribution models with Bayesian additive regression trees
48 stars 11 forks source link

plot.mcmc does not work with masked raster layers #26

Open RS-eco opened 3 years ago

RS-eco commented 3 years ago

The plot.mcmc() function works fine for the example provided, but if the raster input dataset is not a simulated squared-raster layer but a masked raster layer (i.e. South America) the output of the plot.mcmc() function is meaningless. I probably has something to do with the matrix conversion in the beginning of the function. Here is a reproducible example of the problem:

library(dismo)
file <- paste0(system.file(package="dismo"), "/ex/bradypus.csv")
file
bradypus <- read.table(file,  header=TRUE,  sep=",") %>% select(-species)
bradypus$presence <- 1

files <- list.files(path=paste(system.file(package="dismo"), '/ex',
                               sep=''),  pattern='grd',  full.names=TRUE )
mask <- raster(files[1])
set.seed(1963)
bg <- randomPoints(mask, 500)
bg <- as.data.frame(bg)
colnames(bg) <- c("lon", "lat")
bg$presence <- 0

abspres <- bind_rows(bradypus, bg)

path <- file.path(system.file(package="dismo"), 'ex')

files <- list.files(path, pattern='grd$', full.names=TRUE )
files
predictors <- stack(files)
predictors

xnames <- names(predictors)

plot(predictors[[1]])
occ <- SpatialPoints(abspres[,c('lon','lat')])
occ.df <- cbind(abspres,
                raster::extract(predictors, occ))
occ.df <- occ.df[,-c(1:3)]

### The actual example 

sdm <- bart(y.train=occ.df[,'presence'],
            x.train=occ.df[,xnames],
            keeptrees = TRUE)

plot.mcmc(sdm, predictors, iter=50)
AMBarbosa commented 3 years ago

Hi,

I was about to report the same issue. Now that @RS-eco mentions the matrix conversion, I found that the function seems to work properly if we disable the line input.matrix <- input.matrix[complete.cases(input.matrix),], to keep all NAs in their places. But I'm not sure what it's really doing with the NA pixels, as they are not NA and have different values in the resulting raster maps.

Also, there seems to be a disagreement in the name of the function (plot_mcmc.R, but plot.mcmc <- function {...} inside).

Otherwise it seems to work, thanks Colin for another great function!

RS-eco commented 3 years ago

Thanks, yes disabling the specified line works. If you additionally mask the output raster before plotting, add r <- mask(r, inputstack[[1]]) after creating the object "r", there should also be no problem with the NAs in the resulting maps.

kendrag-csu commented 2 years ago

I'm also getting an issue with plot.mcmc both in the vignette and in my own project. I get the following error for all lines using it:

Error in plot.mcm(sdm.tiny, climate, iter = 100) : could not find function "plot.mcm"

I tried embarcadero::plot.mcmc(sdm, climate, iter=5, quiet = TRUE) as well, but that results in an error

Error: 'plot.mcmc' is not an exported object from 'namespace:embarcadero'

Was this function removed from the package?