Skip to contents

generate_weights() generates a matrix containing weight combinations for a set of variables such that each set of weights sums to 1. This can be supplied to calc_all_lscps to calculate fitness landscapes corresponding to a variety of possible sets of weights for weighting functional characteristics. The weights are generated by partitioning a weight of 1 across however many variables are requested in all possible ways.

Usage

generate_weights(step, n, data = NULL, nvar = NULL,
                 varnames = NULL, verbose = TRUE)

Arguments

step

numeric. The step size between weight partitions. Only one of step and n can be specified.

n

numeric. The number of weight partitions between 0-1. Only one of step and n can be specified.

data

an optional fnc_df (the output of as_fnc_df) or kriged_surfaces (the output krige_surf) object. The number of variabes and their names will be extracted from the data as the functional characteristics present in them.

nvar

the number of variables across which to allocate the weights. Ignored if data is not NULL. If nvar = NULL and varnames is supplied, the length of varnames will be used for nvar.

varnames

the names of the variables across which to allocate the weights. Ignored if data is not NULL. If varnames = NULL and nvar is supplied, the sequence from 1 to nar will be used for varnames.

verbose

whether to display a message noting the number of sets of weights created.

Details

generate_weights() works by fining all possible allocations of n objects into nvar bins. When step is supplied, n is computed as round(1/step), so the resulting weight partitions may not be exactly equal to step when its inverse is not an integer. The larger n is (or the smaller step) is, the more possible allocations will be produced (i.e., and the resulting object will have more rows). The output of generate_weights() can quickly become very large with increasing number of variables, and will make subsequent analyses slow. It is recommended to start with a large step size, or small n, and increment up.

Value

A grid_weights object, which is a matrix with a row for each each set of weights and a column for each variable over which the weights are allocated. The weights in each row will sum to 1.

Examples

# Allocating 10 partitions of .1 across 3 variables
wmat <- generate_weights(n = 10, nvar = 3)
#> 66 rows generated
head(wmat)
#>        1   2 3
#> [1,] 1.0 0.0 0
#> [2,] 0.9 0.1 0
#> [3,] 0.8 0.2 0
#> [4,] 0.7 0.3 0
#> [5,] 0.6 0.4 0
#> [6,] 0.5 0.5 0

# Allocating 5 partitions of .2 across the 4 functional
# characteristics in the warps dataset
data("warps")

warps_fnc <- as_fnc_df(warps)
wmat <- generate_weights(n = 5, data = warps_fnc)
#> 56 rows generated
head(wmat)
#>      hydro curve mech fea
#> [1,]   1.0   0.0    0   0
#> [2,]   0.8   0.2    0   0
#> [3,]   0.6   0.4    0   0
#> [4,]   0.4   0.6    0   0
#> [5,]   0.2   0.8    0   0
#> [6,]   0.0   1.0    0   0

# Using 'step' for the same result:
wmat <- generate_weights(step = .2, data = warps_fnc)
#> 56 rows generated
head(wmat)
#>      hydro curve mech fea
#> [1,]   1.0   0.0    0   0
#> [2,]   0.8   0.2    0   0
#> [3,]   0.6   0.4    0   0
#> [4,]   0.4   0.6    0   0
#> [5,]   0.2   0.8    0   0
#> [6,]   0.0   1.0    0   0