I'm currently trying to apply the resampling method described in Herrmann et al. (2022) with an outer and an inner resampling loop :
My dataset is composed of 100 rows by 60 columns. Rows correspond to tries ID (one try per row) and columns to species (one species per column). Each cell represents the count of individuals in each species i for each try K = NiK
NiK could vary with resampling, but the overall amount of individuals in a given try all species confounded (called NK, corresponding to the row sums) need to be constant.
Let's generate a similar data frame :
df <- matrix(sample(0:15, 6000, replace = T), ncol = 60)
df
Here is the code I use for the outer loop :
#Outer loop
#Resampling among the 100 tries 1000 times
seed(1234)
outer = matrix(replicate(1000, sample(1:nrow(df), nrow(df), replace = T)), ncol = 100)
And now adding the inner loop, where i'm stuck. Inner loop resamples the number of individuals for a given species within each try, while outer loop resamples tries among the new dataset generated by the inner loop :
i = species number
r = outer resampling ID
inner <- NULL
complete <- NULL
for (r in 1:1000) {
for (i in 1:ncol(df)) {
inner = cbind(inner,sample(df[,i], nrow(df), replace = T))
}
complete = rbind(complete, inner[outer[r,],])
inner <- NULL
}
complete
The loop work fine to generate 1000 resampling of 100 tries through the inner and outer loops, but I have currently no idea of how to constrain each row to sum to NK, which could be calculated for a given try K :
rowSums(df[K,])
Does anyone know a function or a method that could permit to maintain NK constant while resampling in each column with the inner loop ?
Thanks by advance for your help, and hope my english and my explanations are understandable.