Choc_waffles
Choc_waffles

Reputation: 537

How do i apply function into matrix in iteration for each vector

I've got the following data

Would like to apply threshold point and generate confusion matrix in a dataframe. I am almost there, but had to hardcode the x > 0.5 but would like that to be applied with vectors from prob_seq instead)

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)

Would like to see varying results by threshold columns. Example below uses the static x > 0.5. Need help applying each x to the prob_seq vector

                    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

Thank you !

Upvotes: 0

Views: 41

Answers (1)

Choc_waffles
Choc_waffles

Reputation: 537

Was fumbling around, and this code did the trick for me.

Appreciate your feedback, and if this can be done in a simpler way, please share your thoughts.

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)

and the outcome

                     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

Upvotes: 0

Related Questions