【发布时间】:2021-11-07 15:32:14
【问题描述】:
我有一个名为 rates 的简单矩阵代码,效果很好。
thresholds <- c(1e-8,1e-7,1e-6,1e-5,1e-4,1e-3,1e-2,1e-1,1)
rates = matrix(0,2,length(thresholds))
for (i in 1:length(thresholds)) {
rates[1,i] = 1
rates[2,i] = 1
但是,当我将相同的想法应用于更大的数据(矩阵)时,不知何故会出现错误。
thresholds <- c(1e-8,1e-7,1e-6,1e-5,1e-4,1e-3,1e-2,1e-1,1)
rates = matrix(0,2,length(thresholds))
for (i in 1:length(thresholds)) {
t = thresholds[i]
prob_matrix = solution_matrix(prob_matrix, t)
compare_true_est_sol = compare_solutions(synthetic_solution, prob_matrix)
rates[1,i] = compare_true_est_sol["TP"]/(compare_true_est_sol["TP"]+compare_true_est_sol["FN"])
rates[2,i] = compare_true_est_sol["FP"]/(compare_true_est_sol["FP"]+compare_true_est_sol["TN"])
}
return(rates)
我附上照片 以查看矩阵示例。 solution_matrix() 和 compare_solutions() 是您可以忽略的函数。错误是“
速率错误[2, i]
"。请任何人帮助为什么会发生错误以及如何解决它?
更新:我遵循@BrianMontgomery 的建议,它适用于 thresholds = c(1) is a vector with one element,但是当我将 thresholds 设置为具有多个元素的向量时发生错误,例如 thresholds = c(1, 2)。错误是“if (solution_matrix[i, j] == TRUE) { 中的错误: 需要 TRUE/FALSE 的缺失值”。solution_matrix 的代码如下。
solution_matrix <- function(probability_matrix, threshold){
probability_matrix = probability_matrix>=threshold
solution_matrix = probability_matrix
for (i in 1:nrow(solution_matrix)){
for (j in 1:ncol(solution_matrix)){
if (solution_matrix[i,j]== TRUE){
solution_matrix[i,j] = i-1
} else {
solution_matrix[i,j] = NA
}
}
}
for (j in 1:ncol(solution_matrix)){
solution_matrix[,j] = sort(as.numeric(solution_matrix[,j]),
na.last = TRUE)
}
solution_matrix = solution_matrix
colnames(solution_matrix) <- gsub("\\.", "-",
colnames(solution_matrix))
return(solution_matrix)
}
【问题讨论】:
-
我认为我们不能忽略这些函数,因为我在您提供的代码中看不到“结果”对象。哪一行给出了错误?我建议在这些函数上使用 debugonce()。
-
如果没有您要求我们忽略的功能,则无法复制错误。
-
我们需要能够在我们自己的系统上重现错误。除了你,没有人可以运行它。