| Title: | Efficient Neighborhood Basal Area Metrics for Trees |
|---|---|
| Description: | Fast 'C++'-backed tools for computing conspecific and total neighborhood basal area in mapped forest plots. Includes unweighted and distance-weighted neighborhoods, multiple radii, decay kernels, and basic edge correction. Outputs are model-ready covariates for forest competition, growth, and survival models, following neighborhood modeling workflows commonly used in spatial ecology (e.g., Hülsmann et al. 2024 <doi:10.1038/s41586-024-07118-4>). |
| Authors: | Masatoshi Katabuchi [aut, cre] |
| Maintainer: | Masatoshi Katabuchi <[email protected]> |
| License: | GPL-3 |
| Version: | 0.1.2.900 |
| Built: | 2026-05-18 07:31:36 UTC |
| Source: | https://github.com/mattocci27/calba |
This function calculates the basal area across a given set of trees, applying a decay effect based on distance and species identity for each tree within a given radius.
ba_decay( mu_values, sp, gx, gy, ba, r, exponential_normal = FALSE, edge_correction = c("none", "safe"), bounds = NULL )ba_decay( mu_values, sp, gx, gy, ba, r, exponential_normal = FALSE, edge_correction = c("none", "safe"), bounds = NULL )
mu_values |
A numeric vector of decay parameters. Each value in 'mu_values' represents a decay factor that modifies how the basal area contribution diminishes with distance. |
sp |
A character vector containing species names for each tree. |
gx |
A numeric vector of x-coordinates for the trees. |
gy |
A numeric vector of y-coordinates for the trees. |
ba |
A numeric vector of basal area values for the trees. |
r |
A numeric scalar representing the radius to consider for neighboring trees. |
exponential_normal |
A logical value. If 'FALSE' (default), use exponential decay. If 'TRUE', use exponential-normal decay. |
edge_correction |
Character. See 'ba_simple()' for the '"safe"' behavior that skips edge trees. |
bounds |
Optional numeric vector 'c(xmin, xmax, ymin, ymax)' giving the plot extent. When 'NULL', the range of 'gx'/'gy' is used; supply bounds if your data do not span the full plot. |
The function applies an exponential decay model where the basal area contribution diminishes with distance from the focal tree:
where 'mu' is the decay parameter, 'ba' is the basal area, and 'dist' is the Euclidean distance between trees.
A list with two matrices:
A numeric matrix of basal areas with decay applied for conspecific (same species) trees.
A numeric matrix of basal areas with decay applied for all trees (conspecific + heterospecific).
# Generate a sample dataset set.seed(42) sample_data <- data.frame( latin = sample(letters[1:4], 100, replace = TRUE), gx = runif(100, 0, 10), gy = runif(100, 0, 10), ba = runif(100, 10, 30) ) mu_values <- c(1, 3, 5, 7) ba_decay( mu_values = mu_values, sp = sample_data$latin, gx = sample_data$gx, gy = sample_data$gy, ba = sample_data$ba, r = 3, exponential_normal = FALSE )# Generate a sample dataset set.seed(42) sample_data <- data.frame( latin = sample(letters[1:4], 100, replace = TRUE), gx = runif(100, 0, 10), gy = runif(100, 0, 10), ba = runif(100, 10, 30) ) mu_values <- c(1, 3, 5, 7) ba_decay( mu_values = mu_values, sp = sample_data$latin, gx = sample_data$gx, gy = sample_data$gy, ba = sample_data$ba, r = 3, exponential_normal = FALSE )
Transform the matrices returned by 'ba_decay' into a tidy table that can be joined with other data or mapped with 'ggplot2'.
ba_decay_long( mu_values, sp, gx, gy, ba, r, exponential_normal = FALSE, edge_correction = c("none", "safe"), bounds = NULL )ba_decay_long( mu_values, sp, gx, gy, ba, r, exponential_normal = FALSE, edge_correction = c("none", "safe"), bounds = NULL )
mu_values |
Numeric vector of decay parameters. |
sp |
Character/factor species vector. |
gx |
Numeric x-coordinates. |
gy |
Numeric y-coordinates. |
ba |
Numeric basal area. |
r |
Positive radius threshold. |
exponential_normal |
Logical passed to 'ba_decay'. |
edge_correction |
Character; see 'ba_simple()' for the '"safe"' option. |
bounds |
Optional numeric vector 'c(xmin, xmax, ymin, ymax)' giving the plot extent. When 'NULL', the range of 'gx'/'gy' is used; supply bounds if your data do not span the full plot. |
A data frame with 'tree_id', 'species', 'mu', 'con_ba', and 'total_ba'.
This function calculates the total basal area (conspecific + heterospecific) for a given set of tree data, focusing on the number of focal trees and a radius parameter. The basal area represents the cumulative cross-sectional area of tree trunks, either unadjusted or adjusted for a simple distance-weighted decay model.
ba_simple( sp, gx, gy, ba, r, dist_weighted = FALSE, edge_correction = c("none", "safe"), bounds = NULL )ba_simple( sp, gx, gy, ba, r, dist_weighted = FALSE, edge_correction = c("none", "safe"), bounds = NULL )
sp |
A character vector containing species names for each tree. |
gx |
A numeric vector of x-coordinates for the trees (e.g., 0 to 10 for realistic spatial data). |
gy |
A numeric vector of y-coordinates for the trees (e.g., 0 to 10 for realistic spatial data). |
ba |
A numeric vector of basal area values for the trees (e.g., cross-sectional area at breast height). |
r |
A numeric scalar for the radius parameter. This parameter represents the maximum distance to consider when summing basal areas of neighboring trees (e.g., 1–5 units for spatially distributed data). |
dist_weighted |
Logical. If 'TRUE', use the distance-weighted approach ('ba / dist'); if 'FALSE', use the unadjusted 'ba'. |
edge_correction |
Character. Use '"none"' (default) to return all focal trees or '"safe"' to skip neighborhood calculations for trees within 'r' of the plot edges and mark their output as 'NA'. |
bounds |
Optional numeric vector 'c(xmin, xmax, ymin, ymax)' giving the plot extent. When 'NULL', the range of 'gx'/'gy' is used; supply bounds if your data do not span the full plot. |
The function either: - Calculates the unadjusted basal area if 'dist_weighted = FALSE'. - Applies a simple distance-weighted approach ('ba / dist') if 'dist_weighted = TRUE'.
A list with two numeric vectors:
A numeric vector representing the cumulative basal area of conspecific trees within the radius.
A numeric vector representing the cumulative basal area of all trees (conspecific + heterospecific) within the radius.
# Generate a sample dataset set.seed(42) # For reproducibility sample_data <- data.frame( latin = sample(letters[1:4], 100, replace = TRUE), gx = runif(100, 0, 10), # Spatial coordinates between 0 and 10 gy = runif(100, 0, 10), ba = runif(100, 10, 30) # Basal area between 10 and 30 ) # Calculate with distance weighting ba_simple( sp = sample_data$latin, gx = sample_data$gx, gy = sample_data$gy, ba = sample_data$ba, r = 3, # Radius within the spatial scale dist_weighted = TRUE ) # Calculate without distance weighting ba_simple( sp = sample_data$latin, gx = sample_data$gx, gy = sample_data$gy, ba = sample_data$ba, r = 3, dist_weighted = FALSE )# Generate a sample dataset set.seed(42) # For reproducibility sample_data <- data.frame( latin = sample(letters[1:4], 100, replace = TRUE), gx = runif(100, 0, 10), # Spatial coordinates between 0 and 10 gy = runif(100, 0, 10), ba = runif(100, 10, 30) # Basal area between 10 and 30 ) # Calculate with distance weighting ba_simple( sp = sample_data$latin, gx = sample_data$gx, gy = sample_data$gy, ba = sample_data$ba, r = 3, # Radius within the spatial scale dist_weighted = TRUE ) # Calculate without distance weighting ba_simple( sp = sample_data$latin, gx = sample_data$gx, gy = sample_data$gy, ba = sample_data$ba, r = 3, dist_weighted = FALSE )
This function counts the number of conspecific trees within a given radius for each focal tree.
count_con(sp, gx, gy, r, edge_correction = c("none", "safe"), bounds = NULL)count_con(sp, gx, gy, r, edge_correction = c("none", "safe"), bounds = NULL)
sp |
A character vector of species names. |
gx |
A numeric vector of x-coordinates for the trees. |
gy |
A numeric vector of y-coordinates for the trees. |
r |
A numeric scalar for the radius parameter. |
edge_correction |
Character; see 'ba_simple()' for the '"safe"' option that skips focal trees near the boundary. |
bounds |
Optional numeric vector 'c(xmin, xmax, ymin, ymax)' giving the plot extent. When 'NULL', the range of 'gx'/'gy' is used; supply bounds if your data do not span the full plot. |
A numeric vector containing the count of conspecific trees within the radius for each focal tree.
sample_data <- data.frame( latin = sample(letters[1:4], 100, replace = TRUE), gx = runif(100, 0, 10), gy = runif(100, 0, 10) ) count_con( sp = sample_data$latin, gx = sample_data$gx, gy = sample_data$gy, r = 3 )sample_data <- data.frame( latin = sample(letters[1:4], 100, replace = TRUE), gx = runif(100, 0, 10), gy = runif(100, 0, 10) ) count_con( sp = sample_data$latin, gx = sample_data$gx, gy = sample_data$gy, r = 3 )
This function counts the total number of trees within a given radius for each focal tree.
count_total(gx, gy, r, edge_correction = c("none", "safe"), bounds = NULL)count_total(gx, gy, r, edge_correction = c("none", "safe"), bounds = NULL)
gx |
A numeric vector of x-coordinates for the trees. |
gy |
A numeric vector of y-coordinates for the trees. |
r |
A numeric scalar for the radius parameter. |
edge_correction |
Character; see 'ba_simple()' for the '"safe"' option that skips focal trees close to the edges. |
bounds |
Optional numeric vector 'c(xmin, xmax, ymin, ymax)' giving the plot extent. When 'NULL', the range of 'gx'/'gy' is used; supply bounds if your data do not span the full plot. |
A numeric vector containing the count of all trees within the radius for each focal tree.
sample_data <- data.frame( gx = runif(100, 0, 10), gy = runif(100, 0, 10) ) count_total( gx = sample_data$gx, gy = sample_data$gy, r = 3 )sample_data <- data.frame( gx = runif(100, 0, 10), gy = runif(100, 0, 10) ) count_total( gx = sample_data$gx, gy = sample_data$gy, r = 3 )
Provide a tidy summary of pairwise neighborhood basal area and counts, optionally including decay results for a vector of decay parameters.
neigh_ba( sp, gx, gy, ba, r, mu_values = NULL, dist_weighted = FALSE, exponential_normal = FALSE, edge_correction = c("none", "safe"), bounds = NULL )neigh_ba( sp, gx, gy, ba, r, mu_values = NULL, dist_weighted = FALSE, exponential_normal = FALSE, edge_correction = c("none", "safe"), bounds = NULL )
sp |
A character or factor vector of species names. |
gx |
Numeric x-coordinates of each tree. |
gy |
Numeric y-coordinates of each tree. |
ba |
Numeric basal area of each tree. |
r |
Positive numeric radius within which neighbors are considered. |
mu_values |
Optional numeric vector of decay parameters. When 'NULL', the decay table is omitted. |
dist_weighted |
Logical flag passed to 'ba_simple' to use a simple 'ba / dist' weighting when 'TRUE'. |
exponential_normal |
Logical passed to 'ba_decay' to select the exponential-normal kernel. |
edge_correction |
Character; see 'ba_simple()' for the '"safe"' option that skips edge focal trees. |
bounds |
Optional numeric vector 'c(xmin, xmax, ymin, ymax)' giving the plot extent. When 'NULL', the range of 'gx'/'gy' is used; supply bounds if your data do not span the full plot. |
A list with * 'summary': data frame with 'tree_id', 'species', 'con_ba', 'total_ba', 'con_count', 'total_count'. * 'decay': ('NULL' or) long data frame with 'tree_id', 'species', 'mu', 'con_ba', 'total_ba'.
The 'summary' component also includes derived columns: 'prop_con_ba', 'het_ba', 'het_count', and 'competition_index'.
sample_data <- data.frame( latin = sample(letters[1:4], 10, replace = TRUE), gx = runif(10, 0, 10), gy = runif(10, 0, 10), ba = runif(10, 10, 30) ) neigh_ba( sp = sample_data$latin, gx = sample_data$gx, gy = sample_data$gy, ba = sample_data$ba, r = 3, mu_values = c(1, 3) )sample_data <- data.frame( latin = sample(letters[1:4], 10, replace = TRUE), gx = runif(10, 0, 10), gy = runif(10, 0, 10), ba = runif(10, 10, 30) ) neigh_ba( sp = sample_data$latin, gx = sample_data$gx, gy = sample_data$gy, ba = sample_data$ba, r = 3, mu_values = c(1, 3) )
Compute basal area sums and counts for each tree across multiple radii in one pass.
neigh_multi_r( sp, gx, gy, ba, r_values, dist_weighted = FALSE, edge_correction = c("none", "safe"), bounds = NULL )neigh_multi_r( sp, gx, gy, ba, r_values, dist_weighted = FALSE, edge_correction = c("none", "safe"), bounds = NULL )
sp |
A character or factor vector of species names. |
gx |
Numeric x-coordinates of the trees. |
gy |
Numeric y-coordinates of the trees. |
ba |
Numeric basal area for each tree. |
r_values |
Numeric vector of radii to evaluate. |
dist_weighted |
Logical flag to use 'ba / dist' weighting within each radius. |
edge_correction |
Character; see 'ba_simple()' for the '"safe"' option. |
bounds |
Optional numeric vector 'c(xmin, xmax, ymin, ymax)' giving the plot extent. When 'NULL', the range of 'gx'/'gy' is used; supply bounds if your data do not span the full plot. |
A tidy tibble with 'tree_id', 'species', 'radius', 'con_ba', 'total_ba', 'con_count', 'total_count', 'prop_con_ba', 'het_ba', 'het_count', and 'competition_index'.
sample_data <- data.frame( latin = sample(letters[1:4], 10, replace = TRUE), gx = runif(10, 0, 10), gy = runif(10, 0, 10), ba = runif(10, 10, 30) ) neigh_multi_r( sp = sample_data$latin, gx = sample_data$gx, gy = sample_data$gy, ba = sample_data$ba, r_values = c(3, 5) )sample_data <- data.frame( latin = sample(letters[1:4], 10, replace = TRUE), gx = runif(10, 0, 10), gy = runif(10, 0, 10), ba = runif(10, 10, 30) ) neigh_multi_r( sp = sample_data$latin, gx = sample_data$gx, gy = sample_data$gy, ba = sample_data$ba, r_values = c(3, 5) )