如何获取ausplotsR包optim_species函数选中的站点标识?
样地优化选择中获取站点选中顺序与ID的解决方案
我需要通过优化方法选择样地以最大化各类多样性指数,例如从4个站点中选3个,想获取选中站点的先后顺序以及每一步的累积物种数。使用R语言ausplotsR包的optim_species函数时,仅能得到新增站点的累积物种数和最终共识站点,无法获取首个随机选中站点ID、各估计器对应的选中站点ID及完整选中顺序。
测试代码:
# 加载包 library(ausplotsR) # 测试用物种矩阵 species_matrix <- matrix( c(1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1), nrow = 4, ncol = 5, byrow = TRUE, dimnames = list(c("Site 1", "Site 2", "Site 3", "Site 4"), c("Species 1", "Species 2", "Species 3", "Species 4", "Species 5")) ) # 运行optim_species optim_species(species_matrix, n.plt = 3, random = TRUE, iterations = 10)
一、直接从optim_species获取选中信息的可能性
optim_species函数的设计目标是输出累积多样性曲线和共识站点集合,没有提供参数或输出项来返回各迭代的选中站点顺序及ID。查看函数源码可知,它在迭代过程中仅记录了累积多样性数值,并未保存选中站点的详细信息,因此无法直接从现有函数输出中提取所需内容。
二、替代方案
方案1:修改optim_species源码以保存选中站点信息
可以复制optim_species的源码,添加代码来记录每一步选中的站点ID,从而获取完整的选中顺序。步骤如下:
- 在R控制台输入
ausplotsR::optim_species,复制全部函数代码。 - 修改代码,添加记录选中站点的逻辑:
- 在迭代循环内,每次选中站点时,将站点ID存入一个列表。
- 最终返回结果时,新增包含各迭代选中顺序的元素。
修改后的核心代码片段:
optim_species_custom <- function(spp.matrix, n.plt = NULL, random = FALSE, iterations = 10, ...) { # 保留原函数的初始化、多样性计算等逻辑 # 新增存储选中站点的列表 selected_sites_list <- list() for (i in 1:iterations) { # 原函数的起始站点选择逻辑 if (random) { initial <- sample(1:nrow(spp.matrix), 1) } else { initial <- which.max(rowSums(spp.matrix)) } selected <- initial # 记录本次迭代的选中顺序 selected_order <- rownames(spp.matrix)[initial] for (j in 2:n.plt) { # 原函数计算每个未选中站点的多样性增益 remaining <- setdiff(1:nrow(spp.matrix), selected) gain <- sapply(remaining, function(x) { length(unique(c(colnames(spp.matrix)[spp.matrix[selected,]>0], colnames(spp.matrix)[spp.matrix[x,]>0]))) - length(colnames(spp.matrix)[spp.matrix[selected,]>0]) }) best <- remaining[which.max(gain)] selected <- c(selected, best) # 追加新增站点到顺序列表 selected_order <- c(selected_order, rownames(spp.matrix)[best]) } # 保存本次迭代的完整选中顺序 selected_sites_list[[i]] <- selected_order # 原函数的其他逻辑(如累积多样性计算) } # 整理结果,新增选中站点顺序项 result <- list( # 保留原函数的输出项(如cumulative_species、consensus等) selected_sites = selected_sites_list ) return(result) }
调用修改后的函数后,可通过$selected_sites获取每次迭代的完整选中站点顺序,包括首个随机站点。
方案2:手动实现贪婪优化算法
如果不想修改包源码,可以手动实现类似optim_species的贪婪选择逻辑,直接控制每一步的站点记录:
# 自定义贪婪选择函数,返回选中顺序和累积物种数 greedy_site_selection <- function(spp_matrix, n_plt, random_start = TRUE, iterations = 10) { site_names <- rownames(spp_matrix) n_sites <- nrow(spp_matrix) all_results <- list() for (iter in 1:iterations) { # 选择起始站点 start_idx <- if (random_start) sample(1:n_sites, 1) else which.max(rowSums(spp_matrix)) selected_idx <- c(start_idx) cumulative_species <- c(sum(spp_matrix[start_idx, ] > 0)) # 迭代选择剩余站点 for (step in 2:n_plt) { remaining_idx <- setdiff(1:n_sites, selected_idx) # 计算每个剩余站点的新增物种数 gains <- sapply(remaining_idx, function(idx) { current_species <- colSums(spp_matrix[selected_idx, ]) > 0 new_species <- colSums(spp_matrix[c(selected_idx, idx), ]) > 0 sum(new_species & !current_species) }) # 选择增益最大的站点(若有多个则随机选) best_idx <- remaining_idx[which.max(gains)] if (length(best_idx) > 1) best_idx <- sample(best_idx, 1) selected_idx <- c(selected_idx, best_idx) cumulative_species <- c(cumulative_species, sum(colSums(spp_matrix[selected_idx, ]) > 0)) } # 整理本次迭代结果 all_results[[iter]] <- list( selected_sites = site_names[selected_idx], cumulative_species = cumulative_species ) } return(all_results) } # 使用自定义函数 results <- greedy_site_selection(species_matrix, n_plt = 3, random_start = TRUE, iterations = 10) # 查看第一次迭代的选中顺序和累积物种数 results[[1]]$selected_sites results[[1]]$cumulative_species
该函数可灵活扩展以支持其他多样性指数(如Shannon-Wiener、Simpson指数),只需修改计算gains的逻辑即可。
内容的提问来源于stack exchange,提问作者Francois Brassard
相关产品推荐
相关产品推荐

