Skip to content

get.checkerboard fails when occurrence/bg are in matrices #178

Description

@plantarum

Note that the documentation for get.checkerboard indicates occ and bg can be either a matrix or data.frame.

Steps to reproduce error:

Create data, as per help page:

     library(terra)
     
     set.seed(1)
     
     ### Create environmental extent (raster)
     envs <- rast(nrow = 25, ncol = 25, xmin = 0, xmax = 1, ymin = 0, ymax = 1)
     
     ### Create occurrence localities
     set.seed(1)
     nocc <- 25
     xocc <- rnorm(nocc, sd=0.25) + 0.5
     yocc <- runif(nocc, 0, 1)
     occs <- as.data.frame(cbind(xocc, yocc))
     
     ### Create background points
     nbg <- 500
     xbg <- runif(nbg, 0, 1)
     ybg <- runif(nbg, 0, 1)
     bg <- as.data.frame(cbind(xbg, ybg))

This works as designed for data.frames:

get.checkerboard(occs, envs, bg, aggregation.factor = 4)

Generating basic checkerboard partitions...
$occs.grp
[1] 2 2 1 2 1 1 2 2 1 2 2 2 2 1 2 1 2 2 1 1 2 2 1 2

$bg.grp
[1] 2 2 2 1 2 1 1 1 1 1 1 2 1 2 2 2 1 2 2 2 1 2 2 1 1 2 1 1 2 2 2 1 2 2 1 1 1 1 1 2 2 2 1 1 2 1 2 2 1 1 1 2 2 1
[55] 2 2 2 1 2 1 2 1 1 2 2 1 2 2 2 2 1 2 1 2 2 2 2 2 2 2 1 1 1 1 2 1 1 2 1 2 1 1 2 2 1 1 2 1 1 2 1 2 2 2 2 1 1 1
[109] 1 2 1 2 2 2 2 2 2 2 2 1 2 2 1 2 2 2 2 1 2 2 1 1 1 2 2 2 2 2 1 1 1 1 1 1 2 2 2 1 1 2 2 2 1 2 1 2 1 2 1 2 1 1
[163] 1 2 2 2 2 2 1 1 1 2 2 2 2 2 2 1 1 1 2 1 2 1 1 1 1 2 1 2 1 1 2 2 1 2 2 1 2 2 1 2 1 1 1 1 1 1 2 1 2 1 1 2 1 1
[217] 2 1 1 1 1 1 2 2 2 1 1 2 2 1 1 2 2 1 1 1 1 2 1 1 2 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 1 1 2 1 2 1 2 2 1 1 2 2 2
[271] 1 1 2 2 1 2 2 2 1 1 2 2 2 2 1 1 1 1 2 1 1 2 1 2 2 2 2 1 2 1 1 1 1 1 2 1 2 1 1 1 1 2 2 1 1 1 2 1 2 1 2 1 2 1
[325] 2 2 1 2 1 2 2 2 2 2 1 1 2 2 2 1 2 1 1 2 2 1 2 1 1 1 2 2 2 1 1 2 1 1 2 2 1 1 2 2 2 1 2 1 1 1 2 1 1 1 2 2 2 1
[379] 2 1 1 2 2 1 1 1 2 2 1 2 2 1 2 1 2 1 2 2 1 2 1 2 1 1 2 2 2 2 1 1 1 2 1 2 1 2 1 2 2 2 2 1 2 1 1 2 2 2 1 2 1 2
[433] 2 2 2 2 2 2 1 2 2 1 2 2 2 2 2 2 1 2 1 1 1 2 2 2 2 1 2 1 2 1 2 2 1 1 1 1 2 1 2 1 1 1 1 1 2 2 2 1 2 2 2 2 1 2
[487] 1 1 2 2 1 2 2 2 1 1 1 2 2 2

However, if we supply the occurrences and background as matrices instead:

get.checkerboard(as.matrix(occs), envs, as.matrix(bg), aggregation.factor = 4)

Generating basic checkerboard partitions...
Error in fix.by(by.y, y) : 'by' must specify uniquely valid columns

This is likely a very rare situation, I only encountered it when trying to debug some other issues. The documentation suggests occ and bg can be matrices or data.frames, and this is true when using ENMevaluate, as this function explicitly converts matrices to data.frames:

ENMeval/R/ENMevaluate.R

Lines 326 to 327 in bbfaf8d

occs <- as.data.frame(occs)
if(!is.null(bg)) bg <- as.data.frame(bg)

The problem is caused in the function get.checkerboard, which calls the names function on objects derived from occs and bg in a few places, e.g.:

names(occs.rn) <- c(names(occs.r)[1:2], "row")

names() refers to column names for data.frames, but not in matrices.

I think this could be fixed by switching the calls to names to colnames in get.checkerboard, or by explicitly coercing occs and bg to data.frames in get.checkerboard, as is already done in ENMevaluate.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions