Creates partition schemes in 3D for model training and testing. Can create block or kfold partitions, with output as a vector or list

partition_3D(
  maxent_df,
  coord_df,
  which_partition = "k.fold",
  kfolds = NULL,
  orientation = "lon_lat",
  return_format = "vector",
  ensure_all_folds = TRUE,
  max_attempts = 100,
  na_strategy = "NA"
)

Arguments

maxent_df

Data frame with column 'p' (1=presence, 0=absence).

coord_df

Data frame with 'longitude','latitude','depth' aligned with maxent_df.

which_partition

"k.fold" (default) or "block".

kfolds

Integer >= 2 for k.fold.

orientation

For "block": "lon_lat" (default) or "lat_lon".

return_format

"vector" (default) or "list".

ensure_all_folds

(k.fold) Ensure all folds appear among presences with known depth (default TRUE).

max_attempts

(k.fold) Retry cap when ensure_all_folds=TRUE (default 100).

na_strategy

(k.fold) How to handle NA depth rows: "NA" (default) leaves them NA; "random" assigns a random fold.

Value

If return_format="vector": integer vector (1..k or 1..4), length == nrow(maxent_df). Unassignable rows get NA. If "list": list(occ_partitions, bg_partitions).

Details

maxent_df and coord_df should be the same dataframes to be provided to maxent_3D() for model production. The spatial block partition, similarly to a traditional 2D partition, will separate the occurrences into 4 groups of equal (or about equal) size, with two groups making up two blocks on the upper half of the depth distribution and two groups on the lower half. Background points are assigned to spatial groups according to how they fall into the spatial partitions delimited by the occurrences. Note that this means the number of background points in each partition may not be as close to equal as the occurrences.

Examples

# \donttest{
# create test dataframe
occ <- rep(1, times = 10)
bg <- rep(0, times = 1000)
env1 <- sample(c(1:100), size = 1010, replace = TRUE)
env2 <- sample(c(1:1000), size = 1010, replace = TRUE)
p <- c(occ, bg)
testdf <- data.frame(p, env1, env2)

# create test coord data
r <- terra::rast(ncol = 100, nrow = 100)
set.seed(0)
longitude <- sample(terra::ext(r)[1]:terra::ext(r)[2], size = 1010, replace = TRUE)
set.seed(0)
latitude <- sample(terra::ext(r)[3]:terra::ext(r)[4], size = 1010, replace = TRUE)
depth <- sample(c(0, 5, 10, 15, 20, 25, 30, 35, 40, 45), size = 1010, replace = TRUE)
test_coords <- data.frame(longitude, latitude, depth)

# Here's the function
result_kfold <- partition_3D(maxent_df = testdf, coord_df = test_coords,
which_partition = 'k.fold', kfolds = 3)

result_block <- partition_3D(maxent_df = testdf, coord_df = test_coords,
which_partition = 'block', orientation = 'lat_lon')
# }