我如何在每个向量的迭代中将函数应用于矩阵

时间:2019-04-12 01:53:49

标签: r loops matrix apply

我有以下数据

  • 100个概率向量(pred_prob)
  • 100个实际结果向量 (真相)
  • 阈值点的20个向量,用于ifelse分类 (prob_seq)

想要在数据帧中应用阈值点并生成混淆矩阵。我快到了,但必须对x> 0.5进行硬编码,但希望将其应用于prob_seq中的向量)

pred_prob = runif(100, min=0, max=1)
truth = sample(0:1, 100, replace=T)
prob_seq <- seq(0.5,1,by=0.05)

row_n = length(pred_prob)
col_n = length(prob_seq)

class_table <- as.matrix(replicate(col_n, pred_prob), nrow = row_n,ncol = col_n)
class_table <- apply(class_table, 2, function(x) {factor(ifelse(x > 0.5,"1","0"))})

cm_list <- list()
library('caret')
for (i in 1:col_n)
{
  cm_results <- confusionMatrix(table(class_table[,i], truth),positive = "1")
  cm_list[[i]] <- cm_results$byClass[1:4]
  names(cm_list)[i] <- prob_seq[i]
}
cm_list
data.frame(cm_list)

希望按阈值列查看不同的结果。下面的示例使用静态x> 0.5。需要帮助将每个x应用于prob_seq向量

                    X0.5     X0.55      X0.6     X0.65      X0.7     X0.75      X0.8     X0.85      X0.9     X0.95        X1
Sensitivity    0.4464286 0.4464286 0.4464286 0.4464286 0.4464286 0.4464286 0.4464286 0.4464286 0.4464286 0.4464286 0.4464286
Specificity    0.5454545 0.5454545 0.5454545 0.5454545 0.5454545 0.5454545 0.5454545 0.5454545 0.5454545 0.5454545 0.5454545
Pos Pred Value 0.5555556 0.5555556 0.5555556 0.5555556 0.5555556 0.5555556 0.5555556 0.5555556 0.5555556 0.5555556 0.5555556
Neg Pred Value 0.4363636 0.4363636 0.4363636 0.4363636 0.4363636 0.4363636 0.4363636 0.4363636 0.4363636 0.4363636 0.4363636

谢谢!

1 个答案:

答案 0 :(得分:0)

一直在摸索,这段代码对我有用。

感谢您的反馈,如果可以通过更简单的方式完成,请分享您的想法。

pred_prob = runif(100, min=0, max=1)
truth = sample(0:1, 100, replace=T)
prob_seq <- seq(0.5,0.95,by=0.05)

row_n = length(pred_prob)
col_n = length(prob_seq)

fac_func <- function(x,y) {factor(ifelse(x > y,"1","0"))}

class_table <- as.matrix(replicate(col_n, pred_prob), nrow = row_n,ncol = col_n)
class_table <- sapply(seq_len(ncol(class_table)), function(i) fac_func(class_table[,i], y = prob_seq[i]))

cm_list <- list()
cm_abs <- list()
library('caret')
for (i in 1:col_n)
{
  cm_results <- confusionMatrix(table(class_table[,i], truth),positive = "1")
  cm_abs[[i]] <- cm_results$table[2,1]
  cm_list[[i]] <- cm_results$byClass[1:4]
  names(cm_list)[i] <- prob_seq[i]
  names(cm_abs)[i] <- prob_seq[i]
}
cm_list <- as.data.frame(cm_list)
cm_abs <- as.data.frame(cm_abs) 
row.names(cm_abs) <- 'false_negative'
rbind(cm_list,cm_abs)

和结果

                     X0.5      X0.55       X0.6      X0.65       X0.7      X0.75      X0.8     X0.85      X0.9     X0.95
Sensitivity     0.5370370  0.5185185  0.4814815  0.4444444  0.3888889  0.3888889 0.3703704 0.2592593 0.2037037 0.1111111
Specificity     0.4347826  0.4565217  0.5434783  0.6521739  0.6956522  0.7826087 0.8260870 0.8478261 0.8913043 0.9347826
Pos Pred Value  0.5272727  0.5283019  0.5531915  0.6000000  0.6000000  0.6774194 0.7142857 0.6666667 0.6875000 0.6666667
Neg Pred Value  0.4444444  0.4468085  0.4716981  0.5000000  0.4923077  0.5217391 0.5277778 0.4936709 0.4880952 0.4725275
false_negative 26.0000000 25.0000000 21.0000000 16.0000000 14.0000000 10.0000000 8.0000000 7.0000000 5.0000000 3.0000000