1

我无法在 R 的“permute”包中准确定义我的排列设计/层次结构。

给定一组假设的地块,其中我记录了物种的出现,我想在地块内重新排列物种,同时保持每个地块中的物种数量,并保持整个物种库中单个物种的总体丰度。

最终,我试图建立一个在地块级别(每个地块有 n 个物种)以及整个物种库级别(每个物种的总观察值)受限的零分布。

# build dataset representing the presence/absence of 10 species (columns)
# in 100 plots (rows)

set.seed(123)

dat = matrix(
    sample(c(0,1), size = 100*10, replace = T, prob = c(0.75, 0.25)),
    nrow = 100, 
    ncol = 10) # let this matrix represent the observed data

rowSums(dat) # represents the number of species present in each plot
colSums(dat) # represents the overall number of observations of each species

relative_abund = colSums(dat) / sum(dat) 
    # proportion of occurrences of each species in the entire species pool

# use "permute" package to shuffle species in plots
# while maintaining the total number of species in each plot
# and the relative abundance of all species in the species pool

library(permute)

# single permutation of "plot # 1" maintaining number of species per plot
dat[1, shuffle(dat[1,])] 

# single permutation maintaining total observations of "species # 1"
dat[shuffle(dat[,1]), 1 ] 


# use permutation design/control to shuffle data, such that 

rowSums(permuted_dat) == rowSums(dat)
colSums(permuted_dat) == colSums(dat) # at least approximately
4

1 回答 1

1

谢谢 - 加文辛普森。素食主义者套餐对此非常有效。

对于希望在生成的空模型中同时保留行频和列频的人来说,这是一个示例。“vegan”中的 ?commsim 帮助文件详细介绍了用于指定空模型的不同算法。

# load library
library(vegan)

# build hypothetical dataset of presence/absence data,
# where rows are plots and columns are species

dat = matrix(
    sample(c(0,1), size = 100*10, replace = T, prob = c(0.75, 0.25)),
    nrow = 100, 
    ncol = 10) 

# build null models by reshuffling the dataset
nm_qs = nullmodel(dat, "quasiswap")
nm_qs$data # show presence/absence if original data were abundances

# 100 iterations/reshuffles
# using the "quasiswap" non-sequential algorithm for binary data
sm_qs = simulate(nm_qs, nsim = 100)

# confirm reshuffling preserved row frequencies (i.e., # of species per plot)
rowSums(nm_qs$data) == rowSums(sm_qs[1:100, 1:10, 1]) # first reshuffle
rowSums(nm_qs$data) == rowSums(sm_qs[1:100, 1:10, 2]) # second reshuffle

# confirm reshuffling preserved column frequencies (i.e., overall species abundances)
colSums(nm_qs$data) == colSums(sm_qs[1:100, 1:10, 1]) # first reshuffle
colSums(nm_qs$data) == colSums(sm_qs[1:100, 1:10, 1]) # second reshuffle

# view single reshuffled community
sm_qs[1:100, 1:10, 1]

# build distribution from null models as needed...
于 2019-07-18T19:22:57.913 回答