Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
RS-eco opened this issue Apr 6, 2021 · 3 comments
Open

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

RS-eco opened this issue Apr 6, 2021 · 3 comments

Comments

@RS-eco
Copy link

RS-eco commented Apr 6, 2021

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
Copy link

AMBarbosa commented Apr 6, 2021

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
Copy link
Author

RS-eco commented Apr 6, 2021

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
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants