Kamil
Kamil

Reputation: 23

Calculating a function and comparing one column with multiple columns in a table

I have a big problem with a simple task. I have a dataset consisting of true values (0 and 1) and several predicted values from different models. I'm using package 'Metrics' and I would like to evaluate each prediction using AUC (function "auc" in Metrics). Here is a reproducible example. I have a table:

evaluate <- "true prediction_1 prediction_2 prediction_3
1 0.9 0.5 0.8
1 0.9 0.4 0.7
1 0.8 0.6 0.75
0 0.1 0.3 0.2
0 0.05 0.4 0.1
0 0.15 0.45 0.15"
Table <- read.table(text=evaluate, header = TRUE)

Let's get AUC for the 1st prediction:

require(Metrics)
auc(Table[,1], Table[,2])
[1] 1

similarly, we can get AUC for the 2nd prediction:

auc(Table[,1], Table[,3])
[1] 0.8333333

But how to get AUC simultaneously for all columns? (Not summing them but iterate the function over each pair) I would like to type something like:

auc(Table[,1], Table[,2:4])

and get a table where each prediction is assigned with its AUC value, but this doesn't work...

[1] 13.16667
Warning message:
In auc(Table[, 1], Table[, 2:4]) :
  longer object length is not a multiple of shorter object length

I suppose it is very simple but I'm struggling to find an answer so if anybody could help me or point me to the right path I will appreciate it!

Upvotes: 1

Views: 57

Answers (2)

akrun
akrun

Reputation: 887128

We can also use sapply

sapply(Table[-1], function(x) auc(Table[,1], x))
#  prediction_1 prediction_2 prediction_3 
#  1.0000000    0.8333333    1.0000000 

Upvotes: 1

jay.sf
jay.sf

Reputation: 72838

Using mapply.

mapply(auc, list(Table[,1]), Table[-1])
# [1] 1.0000000 0.8333333 1.0000000

mapply evaluates auc element-wise over the lists given in it's arguments. Because Table[,1] gets coerced to a vector we need to list it again. We could also use Table[,1, drop=FALSE], but the output is nicer using list.

Upvotes: 2

Related Questions