我正在尝试使用 R 计算 Maxent 模型的真实技能统计 (TSS)。我正在遵循我在互联网上找到的脚本,但我收到了以下错误消息:
Error in `$<-.data.frame`(`*tmp*`, "variable", value = character(0)) : replacement has 0 rows, data has 11
有人可以帮我解决这个问题吗?谢谢 !我把我的脚本放在这里。
#### libraries ####
library("reshape2")
library("plyr")
library("reshape")
##### Directory ####
# Root directory #
dir <- c('C:\\Users\\johan\\Documents\\projects\\2021_Dryophytes_immaculatus_review\\range_study\\')
# output directory
all_out <- paste0(dir,'results_n147_12_variables_10replicates_20randoms_without_threshold\\')
#### tss function ####
calc_tss<-function( samples, background, threshold ){
sensitivity <- sum( samples$predict >= threshold ) / nrow( samples )
specificity <- sum( background$predict < threshold ) / nrow( background )
sensitivity + specificity - 1
}
#### Extract MaxSSS threshold values ####
# Create an empty vector
s_thresh_df <- list()
# For-loop
for(run in 1:4){
# Read CSV file of maxent results (generated by maxent earlier)
res_df <- read.csv(paste0("C:\\Users\\johan\\Documents\\projects\\2021_Dryophytes_immaculatus_review\\range_study\\results_n147_12_variables_10replicates_20randoms_without_threshold\\maxentResults.csv"), header=TRUE)
# Select only logistic threshold values and invert the table
thresh_df <- res_df[,c(grep("logistic.threshold",names(res_df)))]
thresh_df$id <- 1:nrow(thresh_df)
thresh_df <- melt(thresh_df, id.vars=c("id"))
# Add a column to indicate corresponding run within the table
thresh_df <- data.frame(run,thresh_df)
thresh_df$run <- run
# Clean up text within rows and rename column
thresh_df$variable <- gsub(".logistic.threshold","",
thresh_df$variable)
thresh_df <- rename(thresh_df,c("variable"="threshold"))
# Add to List
s_thresh_df[[run]] <- thresh_df
}