JCra
JCra

Reputation: 51

Confusion matrix R

I am struggling with this confusion matrix application:

lvs <- c("normal", "abnormal")
truth <- factor(rep(lvs, times = c(86, 258)),
levels = rev(lvs))
pred <- factor(
c(
rep(lvs, times = c(54, 32)),
rep(lvs, times = c(27, 231))),
levels = rev(lvs))
xtab <- table(pred, truth)

Basically, I am confused with line number 2, 6 and 7. Below you can see the result after running the code. But I am still confused what I have to put into the brackets. Of course, I know the values were just an example but in the tutorial it wasn't explained what these values mean.

https://artax.karlin.mff.cuni.cz/r-help/library/caret/html/confusionMatrix.html

truth
pred   abnormal  normal
abnormal   231       32
normal     27        54

Thank you for your help!

Upvotes: 1

Views: 2548

Answers (1)

Rafael Toledo
Rafael Toledo

Reputation: 1064

I think you're more confused about how they create the dataset than the confusionMatrix itself. So i will try to explain step by step these lines.

Here, he just creates levels name to be used later in data generation.

lvs <- c("normal", "abnormal")

Now, he generates the vector to use as true labels of a test set, he replicates the levels name with rep, for each level he sets a specific number of times in the function argument. Afterwards, he transforms the vector as factor and assigns levels in reverse order of the levels name. The reverse order is to organize the order that functions as table will output the data.

truth <- factor(rep(lvs, times = c(86, 258)),levels = rev(lvs))

Here, it's the same process as above, by he joins two rep functions just to messy the data and to simulate responses from a predictor.

pred <- factor( c( rep(lvs, times = c(54, 32)), 
                   rep(lvs, times = c(27, 231))),
                   levels = rev(lvs) )

At last, you use the table function to create a confusion matrix, and with this table you can analyze many metrics as overall accuracy, sensibility, specificity. These metrics already come computed with caret::confusionMatrix.

xtab <- table(pred, truth)
caret::confusionMatrix(pred, truth)

The class assigned as Positive class is the first name of the levels vector in the factor instance.

Upvotes: 1

Related Questions