network_swap {asnipe}  R Documentation 
Performs one (or more) random swap on the data and recalculates network, returning both the new network and the data stream
network_swap(association_data, data_format = "GBI", swaps = 1, association_index = "SRI", association_matrix = NULL, identities = NULL, which_identities = NULL, times = NULL, occurrences = NULL, locations = NULL, which_locations = NULL, start_time = NULL, end_time = NULL, classes = NULL, which_classes = NULL, days = NULL, within_day = FALSE, within_location = FALSE, within_class = FALSE, symmetric=TRUE, trialSwap=TRUE)
association_data 
a 
data_format 

swaps 
number of swaps (default = 1000) 
association_index 

association_matrix 
provide a starting association matrix (see details) 
identities 
N vector of identifiers for each individual (column) in the group by individual matrix 
which_identities 
vector of identities to include in the network (subset of identities) 
times 
K vector of times defining the middle of each group/event 
occurrences 
N x S matrix with the occurrence of each individual in each sampling period (see details) containing only 0s and 1s 
locations 
K vector of locations defining the location of each group/event 
which_locations 
vector of locations to include in the network (subset of locations) 
start_time 
element describing the starting time for inclusion in the network (useful for temporal analysis) 
end_time 
element describing the ending time for inclusion in the network (useful for temporal analysis) 
classes 
N vector of types or class of each individual (column) in the group by individual matrix (for subsetting) 
which_classes 
vector of class(es)/type(s) to include in the network (subset of classes) 
days 
K vector of day stamp for each event (can be integer or string representing any period of time) 
within_day 
if 
within_location 
if 
within_class 
if 
symmetric 
Boolean to ensure that permutations maintain symmetry within sampling periods if using 
trialSwap 
Boolean to include trial swaps (if TRUE, then every attempted permutation is returned) 
Performs one or more permutation swaps on the group by individual matrix as given by Whitehead (2008). In order to save on memory use, this function computers the number of swaps and returns the association matrix and the data stream resulting from these, thus not needing to create a large stack of networks to store each permutation. This can then be implemented in a loop as shown in the example below. Note that this method is quite a bit slower than the network_permutation function.
This implementation allows permutations (swaps) to be restricted to within any of three classes. Though each class is labelled, the function is flexible. Hence, days can represent any time period (months, hours, etc.). However, unlike the network_permutation, the subsetting of the data must be done outside of this function (for reasons that might be obvious)  see the example below.
Trial swaps are implemented following Miklos and Podani (2004). Every permutation, a candidate swap is selected. If the selected portions of the data satisfy the baseline rules (e.g. the checkerboard pattern), then either the selection is attempted again trialSwap = FALSE
or not trialSwap = TRUE
. This should be set to TRUE, but the option for FALSE is provided for legacy analyses.
Returns a list containing an N x N
matrix with the dyadic association rates of each pair of individuals after performing the swaps, and the N x N
data stream postswap, as two list elements.
Damien R. Farine
Whitehead (2008) Analyzing Animal Societies
# load data data("group_by_individual") data("times") # subset GBI (to reduce run time of the example) gbi < gbi[,1:80] # calculate network for data based on morning associations network < get_network(gbi, association_index="SRI", times=times, start_time=0, end_time=max(times)/2) # perform 100 permutations and calculate the coefficient # of variance after each permutation. # note that the subsetting is done outside of the function library(raster) cvs < rep(NA,100) network_perm = list(network,gbi[which(times <= max(times)/2),]) hours < floor(times/3600)[which(times <= max(times)/2)] for (i in 1:100) { network_perm < network_swap(network_perm[[2]], swaps=1, association_matrix=network_perm[[1]], days=hours, within_day=TRUE) cvs[i] < cv(network_perm[[1]]) } # plot the results with the original network as a red dot plot(cvs,pch=20,cex=0.5) points(0,cv(network),cex=1,pch=20,col="red")