| Title: | Benefit Applied Strategic Sampling |
|---|---|
| Description: | Sampling design for generating a spatially dispersed sample that is representative across multiple variables. |
| Authors: | David Hope |
| Maintainer: | David Hope <[email protected]> |
| License: | GPL (>= 3) |
| Version: | 0.3.0 |
| Built: | 2026-05-18 09:12:44 UTC |
| Source: | https://github.com/dhope/BASSr |
A simple features (package SF) object with all of Ontario's Study Areas and the associated Land Cover
all_study_areasall_study_areas
A data frame with 746 rows and 19 variables:
Unique Identifier for each study area
Total hectares in study area
Dominant land cover in study area
Hectares covered by landcover type (00-18)
...
SF geometry
Hexagons generated in R, landcover extracted from National Land Cover 2015 (https://open.canada.ca/data/en/dataset/4e615eae-b90c-420b-adee-2ca35896caf6).
Run speed bass on all hexagons and all samples
allhexes(hexes, samples, total, w, printDets = FALSE)allhexes(hexes, samples, total, w, printDets = FALSE)
hexes |
Matrix of hexagon land covers. Rows are hexagons, columns are land cover types |
samples |
Matrix of hexagon land covers from random sample. Rows are hexagons, columns are land cover types |
total |
Vector of total land cover. values are individual land cover types |
w |
A vector of weights for each land cover value |
printDets |
print details of function calculation. For debugging. |
The BASSr package implements the Benefit Applied Strategic Sampling
Useful links:
Report bugs at https://github.com/dhope/BASSr/issues
BASSr defunct functions
clean_forBass(...) run_full_BASS_w_selection()clean_forBass(...) run_full_BASS_w_selection()
... |
Original function arguments |
Deprecated These functions are no longer useful.
subsample_grts_and_calc_benefit() extract_habitat_cost() genraster() noGRTS_BASS_run() getresults_BASS()subsample_grts_and_calc_benefit() extract_habitat_cost() genraster() noGRTS_BASS_run() getresults_BASS()
Calculate the benefit of a hexagon from grts results.
calculate_benefit( land_hex, samples, hex_id = hex_id, stratum_id = NULL, non_random_set = NULL, land_cover_weights = NULL, crs = 4326, coords = c("lon", "lat"), quiet = FALSE )calculate_benefit( land_hex, samples, hex_id = hex_id, stratum_id = NULL, non_random_set = NULL, land_cover_weights = NULL, crs = 4326, coords = c("lon", "lat"), quiet = FALSE )
land_hex |
(Spatial) Data frame. Land Cover data by hexagon. If
non-spatial, will be converted to spatial sf data frame using the |
samples |
(Spatial) Data frame. Results from |
hex_id |
Column. Identifies hexagon IDs (e.g., default |
stratum_id |
Column. Identifies larger area (e.g., |
non_random_set |
Character vector. |
land_cover_weights |
Data frame. Proportional weights
( |
crs |
Numeric, character, or sf/sfc. Coordinate reference system. Must
be valid input to |
coords |
Character vector. Names of the columns containing X and Y
coordinates (default |
quiet |
Logical. Whether to suppress progress messages. |
Spatial data frame with benefits per hex
# Using example data psu_hexagons and psu_samples calculate_benefit( land_hex = psu_hexagons, samples = psu_samples, non_random_set = c("SA_09", "SA_22", "SA_47")) # Specify a non-random set calculate_benefit( land_hex = psu_hexagons, samples = psu_samples, non_random_set = c("SA_09", "SA_22", "SA_47")) # Without GRTS non_grts_samples <- draw_random_samples( land_hex = psu_hexagons, num_runs = 3, n_samples = 10, use_grts = FALSE) calculate_benefit( land_hex = psu_hexagons, samples = non_grts_samples)# Using example data psu_hexagons and psu_samples calculate_benefit( land_hex = psu_hexagons, samples = psu_samples, non_random_set = c("SA_09", "SA_22", "SA_47")) # Specify a non-random set calculate_benefit( land_hex = psu_hexagons, samples = psu_samples, non_random_set = c("SA_09", "SA_22", "SA_47")) # Without GRTS non_grts_samples <- draw_random_samples( land_hex = psu_hexagons, num_runs = 3, n_samples = 10, use_grts = FALSE) calculate_benefit( land_hex = psu_hexagons, samples = non_grts_samples)
Calculate the cost-benefits and inclusion probabilities.
calculate_inclusion_probs( benefits, costs, hex_id = hex_id, stratum_id = NULL, omit_flag = NULL, benefit_weight = 0.5 )calculate_inclusion_probs( benefits, costs, hex_id = hex_id, stratum_id = NULL, omit_flag = NULL, benefit_weight = 0.5 )
benefits |
Spatial Data frame. Benefits associated with each hexagon
(output of |
costs |
Data frame. Costs for each hexagon in a RawCost format. |
hex_id |
Column. Identifies hexagon IDs (e.g., default |
stratum_id |
Column. Identifies larger area (e.g., |
omit_flag |
Column identifying hexes to omit (e.g., water hexes). |
benefit_weight |
Numeric. Weight assigned to benefit in the selection probabilities. 0.5 is equal weighting of cost and benefits. 1.0 is zero weighting to cost. Default 0.5. |
A data frame with full inclusion probabilities for each hex.
b <- calculate_benefit(land_hex = psu_hexagons, samples = psu_samples) inc <- calculate_inclusion_probs( benefits = b, costs = psu_costs) # Omit water hexes (identified by column `water`) inc <- calculate_inclusion_probs( benefits = b, costs = psu_costs, omit_flag = water)b <- calculate_benefit(land_hex = psu_hexagons, samples = psu_samples) inc <- calculate_inclusion_probs( benefits = b, costs = psu_costs) # Omit water hexes (identified by column `water`) inc <- calculate_inclusion_probs( benefits = b, costs = psu_costs, omit_flag = water)
See for more information: https://www150.statcan.gc.ca/n1/en/pub/12-001-x/2011001/article/11450-eng.pdf?st=0oyBln55 or https://en.wikipedia.org/wiki/Probability-proportional-to-size_sampling
calculate_PPS_hab_inlc_pr( land_hex, hex_id = hex_id, stratum_id = NULL, quiet = FALSE )calculate_PPS_hab_inlc_pr( land_hex, hex_id = hex_id, stratum_id = NULL, quiet = FALSE )
land_hex |
(Spatial) Data frame. Land Cover data by hexagon. If
non-spatial, will be converted to spatial sf data frame using the |
hex_id |
Column. Identifies hexagon IDs (e.g., default |
stratum_id |
Column. Identifies larger area (e.g., |
quiet |
Logical. Whether to suppress progress messages. |
tibble with selection weights from PPS
calculate_PPS_hab_inlc_pr(land_hex = psu_hexagons)calculate_PPS_hab_inlc_pr(land_hex = psu_hexagons)
Calculate z-scores for each hexagon by sum of individual z scores
calculate_z_scores(land_hex, hex_id, stratum_id = NULL, quiet = FALSE)calculate_z_scores(land_hex, hex_id, stratum_id = NULL, quiet = FALSE)
land_hex |
(Spatial) Data frame. Land Cover data by hexagon. If
non-spatial, will be converted to spatial sf data frame using the |
hex_id |
Column. Identifies hexagon IDs (e.g., default |
stratum_id |
Column. Identifies larger area (e.g., |
quiet |
Logical. Whether to suppress progress messages. |
data frame
calculate_z_scores(psu_hexagons, hex_id)calculate_z_scores(psu_hexagons, hex_id)
This is a general function to clean land cover columns.
clean_land_cover(land_raw, pattern = "CLC15_", append = "", quiet = FALSE)clean_land_cover(land_raw, pattern = "CLC15_", append = "", quiet = FALSE)
land_raw |
(Spatial) data frame. Land Cover data to be cleaned. |
pattern |
Character. Pattern to match and replace with 'LC' |
append |
Character. Text to append to end of land cover code |
quiet |
Logical. Whether to suppress progress messages. |
(Spatial) Data frame with cleaned land cover column names.
psu_hex_clean <- clean_land_cover(psu_hex_dirty, pattern = "CLC0013_")psu_hex_clean <- clean_land_cover(psu_hex_dirty, pattern = "CLC0013_")
A dataset containing red, blue, green values associated with all the land cover types found in the 2015 Canadian National Land Cover Classification
clrfileclrfile
A data frame with 20 rows and 6 variables:
price, in US dollars
weight of the diamond, in carats
weight of the diamond, in carats
weight of the diamond, in carats
weight of the diamond, in carats
weight of the diamond, in carats
https://open.canada.ca/data/en/dataset/4e615eae-b90c-420b-adee-2ca35896caf6
Cost variables for estimate_cost_study_area
cost_varscost_vars
A list with 18 variables used to calculate the cost model for N Ontario:
Cost of truck use per day per crew, in dollars
Number of crews for truck surveys
number of arus deployed per crew per day
Cost of ATV use per day per crew, in dollars
Number of crews for ATV surveys
Number of arus deployed per crew per day
Cost of helicopter rental per hour, in dollars
Maximum range of helicopter from fuelling base, in kilometres
Cost of setting up base for helicopter use with distance from airport, in km
Helicopter fuel usage per hour, in litres
Helicopter crew size
Number of arus deployed per day per person
Speed of movement of helicopter when relocating, in km per hour
Cost of heli fuel from an airport, in dollars per litre
Cost of heli fuel from a basecamp, in dollars per litre
Cost of heli fuel from a a remote fuel cache, in dollars per litre
Number of hours a helicopter spends flying in the study area per day.
...
Advice from Rich Russell
Function that takes a landscape as an sf object and returns a hexagonal grid of a given size. Allowed inputs are hectres ('ha'), metres squared ('m2'), metres or kilometres ('m', or 'km') as the diameter of each hexagon
create_hexes( land, hex_size, units = NULL, hex_prefix = "SA_", linear_type = "short_diagonal" )create_hexes( land, hex_size, units = NULL, hex_prefix = "SA_", linear_type = "short_diagonal" )
land |
sf Spatial. Area over which to create hexagonal grid. Must be have a valid CRS. If lat/lon (i.e. CRS 4326) will be projected to 3347 to ensure proper units. |
hex_size |
Numeric or Units. Size of hexagon in area or diagonal
diameter. Can be a bear number (see |
units |
Character. Units of |
hex_prefix |
Character. Text to prefix to hexagon IDs. Default "SA_" results in hexagon ids of "SA_01", etc. |
linear_type |
Character. Type of diameter to use when specifying linear
hexagonal grid sizes. One of |
If hex_size is provided as a units object (i.e., units::set_units(100, km^2)) then the units can be any area or length unit recognized by the
units package and convertible to m or m2. Otherwise, if using a bare number in hex_size and
providing the units as a character in units, they must be one of "m",
"m2", "km", "km2", or "ha".
For example, hex_size = units::set_units(100, ft) will work, but hex_size = 100, units = "ft" will not.
Returns a sf polygon layer of hexagons with unique IDs
library(sf) library(ggplot2) plot <- st_polygon(list(cbind(c(-90,-90,-85,-85,-90), c(50,55,55,50,50)))) |> st_sfc(crs = 4326) |> st_transform(3347) ggplot() + geom_sf(data = plot, fill = "white") # Create grid by area - 1000km2 grid <- create_hexes(plot, hex_size = 1000, units = "km2") # Check the area st_area(grid[1,]) |> units::set_units("km2") # Check the visual ggplot() + geom_sf(data = plot, fill = "white") + geom_sf(data = grid, fill = NA) # Create grid by diameter - 33.98088 km from side to side grid2 <- create_hexes(plot, hex_size = 33.98088, units = "km") # Check the area - Hah! A hexagon with the diameter of 33.98088 km has an area of ~1,000km st_area(grid2[1,]) |> units::set_units("km2") # Check the visual - Identical ggplot() + geom_sf(data = plot, fill = "white") + geom_sf(data = grid2, fill = NA) # Diameter of a 1000 km2 hexagon is area_km2 <- 1000 (sqrt(2 * area_km2 / (3 * sqrt(3)))) * sqrt(3) # Create grid by hectare grid <- create_hexes(plot, hex_size = 40000, units = "ha") ggplot() + geom_sf(data = plot, fill = "white") + geom_sf(data = grid, fill = NA) # Create grid with pre-set units area <- units::set_units(1000, "km2", mode = "character") grid <- create_hexes(plot, hex_size = area) ggplot() + geom_sf(data = plot, fill = "white") + geom_sf(data = grid, fill = NA)library(sf) library(ggplot2) plot <- st_polygon(list(cbind(c(-90,-90,-85,-85,-90), c(50,55,55,50,50)))) |> st_sfc(crs = 4326) |> st_transform(3347) ggplot() + geom_sf(data = plot, fill = "white") # Create grid by area - 1000km2 grid <- create_hexes(plot, hex_size = 1000, units = "km2") # Check the area st_area(grid[1,]) |> units::set_units("km2") # Check the visual ggplot() + geom_sf(data = plot, fill = "white") + geom_sf(data = grid, fill = NA) # Create grid by diameter - 33.98088 km from side to side grid2 <- create_hexes(plot, hex_size = 33.98088, units = "km") # Check the area - Hah! A hexagon with the diameter of 33.98088 km has an area of ~1,000km st_area(grid2[1,]) |> units::set_units("km2") # Check the visual - Identical ggplot() + geom_sf(data = plot, fill = "white") + geom_sf(data = grid2, fill = NA) # Diameter of a 1000 km2 hexagon is area_km2 <- 1000 (sqrt(2 * area_km2 / (3 * sqrt(3)))) * sqrt(3) # Create grid by hectare grid <- create_hexes(plot, hex_size = 40000, units = "ha") ggplot() + geom_sf(data = plot, fill = "white") + geom_sf(data = grid, fill = NA) # Create grid with pre-set units area <- units::set_units(1000, "km2", mode = "character") grid <- create_hexes(plot, hex_size = area) ggplot() + geom_sf(data = plot, fill = "white") + geom_sf(data = grid, fill = NA)
Creates a grid of sites within the hexagon grid cells created by
create_hexes(). These sites can then be sampled with sample_sites().
create_sites(hexes, spacing = NULL, n = NULL, hex_id = hex_id)create_sites(hexes, spacing = NULL, n = NULL, hex_id = hex_id)
hexes |
Spatial Data frame. Hexagon grid across sampling region.
Requires column identifying the hexagon IDs (see |
spacing |
Numeric. Distance between sites. Units
are assumed to be those of |
n |
Numeric. Approximate number of sites to create within a hex grid. |
hex_id |
Column. Identifies hexagon IDs (e.g., default |
Spatial data frame of sites as points.
# Get sites by exact within-hex distances sites_sp <- create_sites(psu_hexagons, spacing = 5) # Get sites by approximate number of points (but equal spacing among hexes) sites_n <- create_sites(psu_hexagons, n = 61) # Same number of sites, but in slightly different spots, because creating by # n maximizes spacing, but creating by spacing using the exact spacing # specified. library(ggplot2) ggplot() + geom_sf(data = psu_hexagons) + geom_sf(data = sites_sp, size = 0.5, colour = "red") + geom_sf(data = sites_n, size = 0.5, colour = "blue")# Get sites by exact within-hex distances sites_sp <- create_sites(psu_hexagons, spacing = 5) # Get sites by approximate number of points (but equal spacing among hexes) sites_n <- create_sites(psu_hexagons, n = 61) # Same number of sites, but in slightly different spots, because creating by # n maximizes spacing, but creating by spacing using the exact spacing # specified. library(ggplot2) ggplot() + geom_sf(data = psu_hexagons) + geom_sf(data = sites_sp, size = 0.5, colour = "red") + geom_sf(data = sites_n, size = 0.5, colour = "blue")
Adjust selection weighting
downweight_selection_pr( sample_locs, scalingFactor, sigma_value, selection_column = NULL, fun = "cauchy", existing_sampling = NULL, dmat = NULL )downweight_selection_pr( sample_locs, scalingFactor, sigma_value, selection_column = NULL, fun = "cauchy", existing_sampling = NULL, dmat = NULL )
sample_locs |
sf object with the selection weighting column (polygons or points) |
scalingFactor |
scaling factor to downweight |
sigma_value |
sigma value of distribution effect for existing sampling. Larger value means sampling has wider effect |
selection_column |
Column with sampling weights to be adjusted. If null return only the weights. |
fun |
Type of decay function. Current options are 'cauchy', 'normal' or 'exp' |
existing_sampling |
exisint sampling to down weight around (points) |
dmat |
distance matrix from sample locations (rows) to existing sampling (columns) |
data.frame sample_locs with downweights and adjusted selection probabilities included. If selection column is null return a vector of the downweights alone.
# downweight_selection_pr(BASSr::all_study_areas[1:10,], BASSr::all_study_areas[20:30,], scalingFactor = 0.1, sigma_value = 1e5, fun = 'cauchy', selection_column = NULL)# downweight_selection_pr(BASSr::all_study_areas[1:10,], BASSr::all_study_areas[20:30,], scalingFactor = 0.1, sigma_value = 1e5, fun = 'cauchy', selection_column = NULL)
Draw random sample
draw_random_samples( land_hex, num_runs, n_samples, use_grts = TRUE, crs = 4326, coords = c("lon", "lat"), return_grts = FALSE, seed = NULL, quiet = FALSE, ... )draw_random_samples( land_hex, num_runs, n_samples, use_grts = TRUE, crs = 4326, coords = c("lon", "lat"), return_grts = FALSE, seed = NULL, quiet = FALSE, ... )
land_hex |
(Spatial) Data frame. Land Cover data by hexagon. If
non-spatial, will be converted to spatial sf data frame using the |
num_runs |
Numeric. Number of times to draw random samples. |
n_samples |
Numeric. Number of samples to draw in each run. |
use_grts |
Logical. Whether to use |
crs |
Numeric, character, or sf/sfc. Coordinate reference system. Must
be valid input to |
coords |
Character vector. Names of the columns containing X and Y
coordinates (default |
return_grts |
Logical. Return the |
seed |
Numeric. Random seed to use for random sampling. Seed only
applies to specific sampling events (does not change seed in the
environment). |
quiet |
Logical. Whether to suppress progress messages. |
... |
Extra named arguments passed on to |
Spatial data frame of samples
draw_random_samples(psu_hexagons, num_runs = 1, n_samples = 10)draw_random_samples(psu_hexagons, num_runs = 1, n_samples = 10)
Cost model estimate
estimate_cost_study_area( narus, StudyAreas, pr, sr, wr = 0, dist_base_sa, dist_airport_sa, dist2airport_base, AirportType, vars )estimate_cost_study_area( narus, StudyAreas, pr, sr, wr = 0, dist_base_sa, dist_airport_sa, dist2airport_base, AirportType, vars )
narus |
The number of ARUs to deploy in the study area |
StudyAreas |
Tibble with study area information and distances |
pr |
Column with primary road buffer proportion of study area |
sr |
Column with secondary Road proportion of study area ( should not include primary road area) |
wr |
Column with Winter Road proportion of study area ( should not include primary or secondary road areas) |
dist_base_sa |
Column with distance between basecamp and study area |
dist_airport_sa |
Column with distance between airport and study area |
dist2airport_base |
Column with distance between airport and base camp |
AirportType |
Column with nearest airport type |
vars |
List containing parameters for cost estimation |
data frame
Extract Habitat and Cost
number_iterations |
Number of iterations to draw samples from full GRTS |
n_samples_per_iter |
Number of samples to pull per iteration |
sample_hexes |
Sample hexagon file |
study_area_hexes |
Study area hexagon files |
id |
Id of the study area of interest |
id_col |
Column identifying study area |
hab_rast_location |
Location of raster for habitat |
shape_file_list |
named list of shapefiles used in cost calculation. The current iteration must include the following names:
|
return_all_ |
Logical to return a full results or just the inclusion probabilities |
nARUs |
number of ARUs to deploy |
hexid_col |
hexagon identification column |
calc_cost |
Logical - calculate cost |
calc_hab |
Logical - calculate habitat |
write_hexes |
Logical - write hexagons |
load_hexes |
Logical load hexagons |
rds.loc |
RDS location |
sa.rast.loc |
Study area location |
quick |
Run using cpp |
A full BASS run
full_BASS_run( land_hex, num_runs, n_samples, costs = NULL, hex_id = hex_id, stratum_id = NULL, omit_flag = NULL, non_random_set = NULL, benefit_weight = 0.5, land_cover_weights = NULL, return_grts = FALSE, crs = 4326, coords = c("lon", "lat"), seed = NULL, quiet = FALSE, ... )full_BASS_run( land_hex, num_runs, n_samples, costs = NULL, hex_id = hex_id, stratum_id = NULL, omit_flag = NULL, non_random_set = NULL, benefit_weight = 0.5, land_cover_weights = NULL, return_grts = FALSE, crs = 4326, coords = c("lon", "lat"), seed = NULL, quiet = FALSE, ... )
land_hex |
(Spatial) Data frame. Land Cover data by hexagon. If
non-spatial, will be converted to spatial sf data frame using the |
num_runs |
Numeric. Number of times to draw random samples. |
n_samples |
Numeric. Number of samples to draw in each run. |
costs |
Data frame. Costs for each hexagon in a RawCost format. |
hex_id |
Column. Identifies hexagon IDs (e.g., default |
stratum_id |
Column. Identifies larger area (e.g., |
omit_flag |
Column identifying hexes to omit (e.g., water hexes). |
non_random_set |
Character vector. |
benefit_weight |
Numeric. Weight assigned to benefit in the selection probabilities. 0.5 is equal weighting of cost and benefits. 1.0 is zero weighting to cost. Default 0.5. |
land_cover_weights |
Data frame. Proportional weights
( |
return_grts |
Logical. Return the |
crs |
Numeric, character, or sf/sfc. Coordinate reference system. Must
be valid input to |
coords |
Character vector. Names of the columns containing X and Y
coordinates (default |
seed |
Numeric. Random seed to use for random sampling. Seed only
applies to specific sampling events (does not change seed in the
environment). |
quiet |
Logical. Whether to suppress progress messages. |
... |
Extra named arguments passed on to |
Data frame of inclusion probabilities. Or, if return_grts = TRUE a
list including the data frame of inclusion probabilities as well as the
spsurvey grts sampling object.
Extra named arguments for spsurvey::grts() can also be passed on via ....
In particular, note that the default values for mindis (minimum distance
between sites) is NULL, and maxtry (maximum attempts to try to obtain the
minimum distance between sites) is 10.
# With example data psu_hexagons and psu_costs... d <- full_BASS_run( land_hex = psu_hexagons, num_runs = 10, n_samples = 3, costs = psu_costs) # Omit water hexes d <- full_BASS_run( land_hex = psu_hexagons, num_runs = 10, n_samples = 3, costs = psu_costs, omit_flag = water) # Keep grts objects d <- full_BASS_run( land_hex = psu_hexagons, num_runs = 10, n_samples = 3, costs = psu_costs, return_grts = TRUE) names(d) d[["inclusion_probs"]] d[["grts_output"]][[1]] # Change spsurvey::grts() arguments d <- full_BASS_run( land_hex = psu_hexagons, num_runs = 10, n_samples = 3, costs = psu_costs, mindis = 10, maxtry = 10) d# With example data psu_hexagons and psu_costs... d <- full_BASS_run( land_hex = psu_hexagons, num_runs = 10, n_samples = 3, costs = psu_costs) # Omit water hexes d <- full_BASS_run( land_hex = psu_hexagons, num_runs = 10, n_samples = 3, costs = psu_costs, omit_flag = water) # Keep grts objects d <- full_BASS_run( land_hex = psu_hexagons, num_runs = 10, n_samples = 3, costs = psu_costs, return_grts = TRUE) names(d) d[["inclusion_probs"]] d[["grts_output"]][[1]] # Change spsurvey::grts() arguments d <- full_BASS_run( land_hex = psu_hexagons, num_runs = 10, n_samples = 3, costs = psu_costs, mindis = 10, maxtry = 10) d
Generate Raster
id_col |
Column to use |
studyareas |
Study area list |
id |
Id of study area to extract |
hab_rast_location |
Location of habitat file |
writeout |
Logical - write output |
outpath |
Location of output |
Get the results from a BASS grts run
grts_output |
Hypothetical sample set |
study_area_results |
Study area results of BASS |
nARUs |
Number of ARUs to deploy |
Key for land cover codes and names
lcc2015_codeslcc2015_codes
A data frame with 19 rows and 2 variables:
Land cover code
Land cover name
...
https://open.canada.ca/data/en/dataset/4e615eae-b90c-420b-adee-2ca35896caf6
A calculate BASS from random samples
samples |
Hypothetical sample set |
num_runs |
The number of times to draw random samples from hexagons |
n_samples |
The number of samples to draw in each sample |
costs |
the cost table for each hexagon id |
A poloygon with the outline of Ontario
ontarioontario
A simple feature data frame with 1 rows and 2 variables:
Two letter code for Ontario
Name of Province
SF geometry
Map of Canadian Jurisdictions
Opposite signs True or false
oppositeSigns(x, y)oppositeSigns(x, y)
x |
a double value |
y |
an integer |
Propare hexagons for cost calculations
prepare_cost( truck_roads, atv_roads, winter_roads, all_roads, airports, basecamps, hexagons, idcol_, calc_roads = T, airport_cols = c("NAME", "AIRPORT_TY", "OGF_ID"), basecamp_cols = c("OFFICIAL_N", "OGF_ID", "CLASS_SUBT"), ... )prepare_cost( truck_roads, atv_roads, winter_roads, all_roads, airports, basecamps, hexagons, idcol_, calc_roads = T, airport_cols = c("NAME", "AIRPORT_TY", "OGF_ID"), basecamp_cols = c("OFFICIAL_N", "OGF_ID", "CLASS_SUBT"), ... )
truck_roads |
Primary roads (truck roads) buffer. Should be a polygon layer. Only used if calculating road estimates. |
atv_roads |
Secondary (atv roads) buffer. Should be a polygon layer. Only used if calculating road estimates. |
winter_roads |
Winter roads buffer. Should be a polygon layer. Not currently in use. |
all_roads |
Total roads buffer. Should be polygon. Not currently in use. |
airports |
Airport locations. Should be a polygon layer. |
basecamps |
Basecamp locations. Should be a polygon layer. |
hexagons |
Hexagon layer |
idcol_ |
Column with hexagon ids |
calc_roads |
Logical. Should you calculate roads or are they already included in hexagon layer |
airport_cols |
Columns to use extract airport info. See examples. Should be length of 3. |
basecamp_cols |
Columns to use extract basecamp info. See examples. Should be length of 3. |
... |
You can include multisession with the furrr package. Needs to include Multicor=T & Cores = int |
data frame
## Not run: prepare_cost( truck_roads = NA, atv_roads = NA, winter_roads = NA, all_roads = NA, airports = airports_official, basecamps = tourism, hexagons = study_area_hexagons_in_brandt %>% sf::left_join(road_info, by = c("StudyAreaID" = "StudyArea")), idcol_ = StudyAreaID, calc_roads = F, airport_cols = c("NAME", "AIRPORT_TY", "OGF_ID"), basecamp_cols = c("OFFICIAL_N", "OGF_ID", "CLASS_SUBT") ) ## End(Not run)## Not run: prepare_cost( truck_roads = NA, atv_roads = NA, winter_roads = NA, all_roads = NA, airports = airports_official, basecamps = tourism, hexagons = study_area_hexagons_in_brandt %>% sf::left_join(road_info, by = c("StudyAreaID" = "StudyArea")), idcol_ = StudyAreaID, calc_roads = F, airport_cols = c("NAME", "AIRPORT_TY", "OGF_ID"), basecamp_cols = c("OFFICIAL_N", "OGF_ID", "CLASS_SUBT") ) ## End(Not run)
Dummy costs data
psu_costspsu_costs
A data frame with 33 rows and 27 columns
ID of the hex
Province code for that hex
Whether that hex is in water or not
Area of the hex in m2
Specific costs for each hex (see ?estimate_cost_study_area)
Number of ARUs to be deployed
Total raw cost of sampling this hex
Data generated in data-raw/data_create_study_area.R
Dummy hex data to be cleaned
psu_hex_dirtypsu_hex_dirty
A spatial data frame with 33 features and 9 fields
ID of the hex
Province code for that hex
Whether that hex is in water or not
Land cover columns
Geometry
Data generated in data-raw/data_create_study_area.R
Dummy hex data
psu_hexagonspsu_hexagons
A spatial data frame with 33 features and 9 fields
ID of the hex
Province code for that hex
Whether that hex is in water or not
Land cover columns
Geometry
Data generated in data-raw/data_create_study_area.R
Dummy sampled hexes
psu_samplespsu_samples
A spatial data frame with 30 features and 21 fields
Sampling output (see spsurvey::grts())
ID of the hex
Province code for that hex
Whether that hex is in water or not
Land cover columns
Geometry
Run number
Total number of runs performed
Total number of samples drawn in a run
Data generated in data-raw/data_create_study_area.R
Running this function will run BASS on both study areas and sample units within those study areas. It will return a selection of sample units with associated costs and habitats.
study_areas_hab_cost |
A list of study area hexagons, costs, and habitat characteristics. See naming above. |
Number_of_Study_areas |
Number of Study Areas to select |
Number_of_sample_units |
Number of Sample Units to select |
Size_of_HSS |
Size of they Hypothetical Sample Set |
Number_of_HSS |
Number of iterations in the hypothetical sample set |
Weight_of_benfit |
Weight of benefit in selection probabilties. |
LandCoverType |
String with identifer for both the land cover hexagons and their code within that tibble |
RemovedLayers_ |
Layers to remove from benefit calculation must be in format of c(-var1, -var2, -var3) |
Area_of_interest |
Area of interest in tibble with StudyAreaID |
RandomSeed |
Random seet to use in GRTS |
calculate_benefits |
Should you calculate benefits or are they included in benefit_df |
only_calculate_benefits |
Do you only want to calculate benefits or complete the full run |
benefit_dfs |
If calculate_benefits is TRUE need some data.frames with benefits. |
returnALL |
do you return the GRTS object, as well as the selection probs. (Can be used later to calculate spatial balance) |
oversample |
Proportion of sites to oversample, used for both study areas and sample units |
weighted_benefits_df |
list of data frames with weight and land covers for benefit calculation |
Non |
random set of study areas and sample units in form of named list. |
List of sample units and a summary comparing land cover to the local area
Sample sites based on the cost/benefit probabilities calculated in previous steps. Sites can be sampled with or without stratification.
run_grts_on_BASS( probs, nARUs, os = NULL, num_runs = 1, hex_id = NULL, stratum_id = NULL, remove_hexes = NULL, selection_weighting = inclpr, seed = NULL, ... )run_grts_on_BASS( probs, nARUs, os = NULL, num_runs = 1, hex_id = NULL, stratum_id = NULL, remove_hexes = NULL, selection_weighting = inclpr, seed = NULL, ... )
probs |
Data frame. Output of |
nARUs |
Numeric, Data frame, Vector, or List. Number of base samples to
choose. For stratification, a named vector/list of samples per stratum, or
a data frame with columns |
os |
Numeric, Vector, or List. Over sample size (proportional) or named
vector/list of number of samples per stratum. Ignored if |
num_runs |
Numeric. Number of times to draw random samples. |
hex_id |
Column. Identifies hexagon IDs (e.g., default |
stratum_id |
Column. Identifies larger area (e.g., |
remove_hexes |
Character Vector. Ids of hexagons to remove prior to sampling. |
selection_weighting |
Column. Identifies selection weightings used by
the |
seed |
Numeric. Random seed to use for random sampling. Seed only
applies to specific sampling events (does not change seed in the
environment). |
... |
Extra named arguments passed on to |
If num_runs is 1, a single spsurvey object, otherwise a list of
spsurvey objects.
Extra named arguments for spsurvey::grts() can also be passed on via ....
In particular, note that the default values for mindis (minimum distance
between sites) is NULL, and maxtry (maximum attempts to try to obtain the
minimum distance between sites) is 10.
d <- full_BASS_run( land_hex = psu_hexagons, num_runs = 10, n_samples = 3, costs = psu_costs) # Simple selection sel <- run_grts_on_BASS( probs = d, nARUs = 5, os = 0.2) # Stratify d <- dplyr::mutate(d, Province = c(rep("ON", 16), rep("MB", 17))) # Add Strata # With lists... sel <- run_grts_on_BASS( probs = d, nARUs = list("ON" = 5, "MB" = 2), stratum_id = Province, os = 0.2) # With data frame... sel <- run_grts_on_BASS( probs = d, nARUs = data.frame(Province = c("ON", "MB"), n = c(5, 2), n_os = c(1, 1)), stratum_id = Province)d <- full_BASS_run( land_hex = psu_hexagons, num_runs = 10, n_samples = 3, costs = psu_costs) # Simple selection sel <- run_grts_on_BASS( probs = d, nARUs = 5, os = 0.2) # Stratify d <- dplyr::mutate(d, Province = c(rep("ON", 16), rep("MB", 17))) # Add Strata # With lists... sel <- run_grts_on_BASS( probs = d, nARUs = list("ON" = 5, "MB" = 2), stratum_id = Province, os = 0.2) # With data frame... sel <- run_grts_on_BASS( probs = d, nARUs = data.frame(Province = c("ON", "MB"), n = c(5, 2), n_os = c(1, 1)), stratum_id = Province)
Selection methods for processing site selection using GRTS, random sampling, clustering, or shortest path methods.
select_sites( sites, type, n_samples, min_dist, cluster_size = NULL, min_dist_cluster = NULL, os = NULL, hex_id = hex_id, site_id = site_id, ARUonly = FALSE, useGRTS = TRUE, progress = TRUE, seed = NULL )select_sites( sites, type, n_samples, min_dist, cluster_size = NULL, min_dist_cluster = NULL, os = NULL, hex_id = hex_id, site_id = site_id, ARUonly = FALSE, useGRTS = TRUE, progress = TRUE, seed = NULL )
sites |
Spatial Data frame. Site points created in |
type |
String. Method to select sites. Must be one of
|
n_samples |
Numeric. Number of samples to draw for each hex. |
min_dist |
Numeric. Minimum distance between points, or if Clusters, between cluster centres. |
cluster_size |
Integer. For Clusters, number of points per cluster. For Shortest Paths, number of points per path. Only applies to Clusters and Paths. |
min_dist_cluster |
Numeric. Minimum distance between ARU samples within clusters. Only applies to Clusters. |
os |
Numeric. Over sample size (proportional). Only applies to Clusters and Random. |
hex_id |
Column. Identifies hexagon IDs (e.g., default |
site_id |
Column. Identifies site IDs (default |
ARUonly |
Logical. Return only ARU locations. If |
useGRTS |
Logical. Should the program be run using GRTS? Only applies to Clusters or Random samples. |
progress |
Logical. Show progress bars if applicable. |
seed |
Numeric. Random seed to use for random sampling. Seed only
applies to specific sampling events (does not change seed in the
environment). |
If Clustered, returns a data frame of clustered points selected from sites.
If Random, returns a data frame of sampled points selected from sites.
If Shortest Path, returns a list of the points on the path and the original points selected to create the path.
library(dplyr) library(ggplot2) sites <- psu_hexagons |> slice_sample(n = 7) |> create_sites(spacing = 5) |> mutate(scaled_benefit = 1, benefit = 0.95) # Basic clusters s <- select_sites(sites = sites, hex_id = hex_id, site_id = site_id, type = "cluster", os = 0.75, n_samples = 7, cluster_size = 5, ARUonly = FALSE, seed = 1234, useGRTS = TRUE, min_dist = 25, min_dist_cluster = 9) ggplot() + geom_sf(data = psu_hexagons) + # Hex grid geom_sf(data = sites, alpha = 0.4) + # Sites on selected Hex grids geom_sf(data = s, aes(colour = aru)) + # Selected sites scale_colour_viridis_d() # Random samples s <- select_sites(sites = sites, hex_id = hex_id, site_id = site_id, type = "random", os = 1.0, n_samples = 2, ARUonly = FALSE, seed = 1234, min_dist = 10) ggplot() + geom_sf(data = psu_hexagons) + # Hex grid geom_sf(data = sites, alpha = 0.4) + # Sites on selected Hex grids geom_sf(data = s, aes(colour = siteuse)) + # Selected sites scale_colour_viridis_d() # Shortest Path s <- select_sites(sites = sites, hex_id = hex_id, site_id = site_id, type = "path", n_samples = 8, cluster_size = 4, ARUonly = FALSE, seed = 1234, useGRTS = TRUE, min_dist = 10, progress = FALSE) ggplot() + geom_sf(data = sites, alpha = 0.4) + # Sites on selected Hex grid geom_sf(data = s$routes, aes(colour = factor(route))) + # Selected sites scale_colour_viridis_d()library(dplyr) library(ggplot2) sites <- psu_hexagons |> slice_sample(n = 7) |> create_sites(spacing = 5) |> mutate(scaled_benefit = 1, benefit = 0.95) # Basic clusters s <- select_sites(sites = sites, hex_id = hex_id, site_id = site_id, type = "cluster", os = 0.75, n_samples = 7, cluster_size = 5, ARUonly = FALSE, seed = 1234, useGRTS = TRUE, min_dist = 25, min_dist_cluster = 9) ggplot() + geom_sf(data = psu_hexagons) + # Hex grid geom_sf(data = sites, alpha = 0.4) + # Sites on selected Hex grids geom_sf(data = s, aes(colour = aru)) + # Selected sites scale_colour_viridis_d() # Random samples s <- select_sites(sites = sites, hex_id = hex_id, site_id = site_id, type = "random", os = 1.0, n_samples = 2, ARUonly = FALSE, seed = 1234, min_dist = 10) ggplot() + geom_sf(data = psu_hexagons) + # Hex grid geom_sf(data = sites, alpha = 0.4) + # Sites on selected Hex grids geom_sf(data = s, aes(colour = siteuse)) + # Selected sites scale_colour_viridis_d() # Shortest Path s <- select_sites(sites = sites, hex_id = hex_id, site_id = site_id, type = "path", n_samples = 8, cluster_size = 4, ARUonly = FALSE, seed = 1234, useGRTS = TRUE, min_dist = 10, progress = FALSE) ggplot() + geom_sf(data = sites, alpha = 0.4) + # Sites on selected Hex grid geom_sf(data = s$routes, aes(colour = factor(route))) + # Selected sites scale_colour_viridis_d()
The internal BASSr benefit algorithm
speedbass(hex, w, sample, total, printDets = FALSE)speedbass(hex, w, sample, total, printDets = FALSE)
hex |
A vector of land cover values |
w |
A vector of weights for each land cover value |
sample |
a vector of land cover values from random sample |
total |
a vector of land cover values from total of study area |
printDets |
logical - should you print the details - messy for now. |
Dummy SSU land cover
ssu_land_coverssu_land_cover
A data frame with 3003 rows and 10 columns
ID of the hex
ID of subsampling unit (hex)
Area of the SSU hex
Land cover columns
Province code for that hex
Data generated in data-raw/data_create_study_area.R
Dummy SSU points
ssu_pointsssu_points
A spatial data frame with 3003 features and 3 fields
Geometry
ID of the hex
ID of subsampling unit (hex)
Province code for that hex
Data generated in data-raw/data_create_study_area.R
A list containing the cost, landcover, and id for an example study area
StudyArea_hexesStudyArea_hexes
A list of 2 dataframes and one character string:
cost estimates for each sample unit
SF polygon with land cover in percentages for each sample unit
Unique identifier for Study Area
BASSr analysis for N. Ontario
Subsample GRTS and calculate benefit
n_samples |
Number of Samples |
num_runs |
Number of iterations |
grts_file |
grts file to run |
land_hex |
Att frame |
quick |
run it using CPP quick calc |
Add a number to a sum of vector
sumH(x, h)sumH(x, h)
x |
A vector |
h |
a nuber to add |