Generate a continuous raster map using moving windows. While window_gd is
built specifically for making moving window maps of genetic diversity from vcfs,
window_general can be used to make moving window maps from different data inputs.
See details for how to format data inputs for different statistics.
window_general(
x,
coords,
lyr,
stat,
wdim = 3,
fact = 0,
rarify = FALSE,
rarify_n = NULL,
rarify_nit = 5,
min_n = 2,
fun = mean,
L = "nvariants",
rarify_alleles = TRUE,
sig = 0.05,
crop_edges = FALSE,
...
)SpatRaster that includes a raster layer of genetic diversity and a raster layer of the number of samples within the window for each cell
data to be summarized by the moving window (note: order matters! coords should be in the same order, there are currently no checks for this). The class of x required depends on the statistic being calculated (see the stat argument and the function description for more details)
coordinates of samples as sf points, a two-column matrix, or a data.frame representing x and y coordinates (see Details for important information about projections)
SpatRaster or RasterLayer to slide the window across (see Details for important information about projections)
moving window statistic to calculate (can either be "pi" for nucleotide diversity (x must be a dosage matrix), "Ho" for average observed heterozygosity across all loci (x must be a heterozygosity matrix) , "allelic_richness" for average allelic richness across all loci (x must be a genind type object), "biallelic_richness" to get average allelic richness across all loci for a biallelic dataset (x must be a dosage matrix). stat can also be set to any function that will take xas input and return a single numeric value (for example, x can be a vector and stat can be set equal to a summary statistic like mean, sum, or sd)
dimensions (height x width) of window; if only one value is provided, a square window is created (defaults to 3 x 3 window)
aggregation factor to apply to lyr (defaults to 0; note: increasing this value reduces computational time)
if rarify = TRUE, rarefaction is performed (defaults to FALSE)
if rarify = TRUE, number of points to use for rarefaction (defaults to min_n)
if rarify = TRUE, number of iterations to use for rarefaction (defaults to 5). Can also be set to "all" to use all possible combinations of samples of size rarify_n within the window.
minimum number of samples to use in calculations (any focal cell with a window containing less than this number of samples will be assigned a value of NA; defaults to 2)
function to use to summarize rarefaction results (defaults to mean, must take na.rm = TRUE as an argument)
for calculating "pi", L argument in pi.dosage function. Return the average nucleotide diversity per nucleotide given the length L of the sequence. The wingen default is L = "nvariants" which sets L to the number of variants in the VCF. If L = NULL, returns the sum over SNPs of nucleotide diversity (note: L = NULL is the pi.dosage default which wingen does not use)
for calculating "biallelic_richness", whether to perform rarefaction of allele counts as in allelic.richness (defaults to TRUE)
for calculating "hwe", significance threshold (i.e., alpha level) to use for hardy-weinberg equilibrium tests (defaults to 0.05)
whether to remove cells on the edge of the raster where the window is incomplete (defaults to FALSE)
if a function is provided for stat, additional arguments to pass to the stat function (e.g. if stat = mean, users may want to set na.rm = TRUE)
To calculate genetic diversity statistics with the built in wingen functions, data must be formatted as such:
for "pi" or "biallelic_richness", x must be a dosage matrix with values of 0, 1, or 2
for "Ho", x must be a heterozygosity matrix where values of 0 = homozygosity and values of 1 = heterozygosity
for "allelic_richness" or "hwe, x must be a genind type object
for "basic_stats", x must be a hierfstat type object
Otherwise, stat can be any function that takes a matrix or data frame and outputs a
single numeric value (e.g., a function that produces a custom diversity index);
however, this should be attempted with caution since this functionality has
not have been tested extensively and may produce errors.