Skip to content
antagomir edited this page Apr 5, 2015 · 10 revisions

ROC analysis

A basic example of ROC/AUC analysis with simulated random data.

# Get example data
library(microbiome, quietly = TRUE)
## Loading required package: permute
## Loading required package: lattice
## This is vegan 2.2-1
## Loading required package: DBI
## Loading required package: AnnotationDbi
## Loading required package: stats4
## Loading required package: BiocGenerics
## Loading required package: parallel
## 
## Attaching package: 'BiocGenerics'
## 
## The following objects are masked from 'package:parallel':
## 
##     clusterApply, clusterApplyLB, clusterCall, clusterEvalQ,
##     clusterExport, clusterMap, parApply, parCapply, parLapply,
##     parLapplyLB, parRapply, parSapply, parSapplyLB
## 
## The following object is masked from 'package:stats':
## 
##     xtabs
## 
## The following objects are masked from 'package:base':
## 
##     anyDuplicated, append, as.data.frame, as.vector, cbind,
##     colnames, do.call, duplicated, eval, evalq, Filter, Find, get,
##     intersect, is.unsorted, lapply, Map, mapply, match, mget,
##     order, paste, pmax, pmax.int, pmin, pmin.int, Position, rank,
##     rbind, Reduce, rep.int, rownames, sapply, setdiff, sort,
##     table, tapply, union, unique, unlist, unsplit
## 
## Loading required package: Biobase
## Welcome to Bioconductor
## 
##     Vignettes contain introductory material; view with
##     'browseVignettes()'. To cite Bioconductor, see
##     'citation("Biobase")', and for packages 'citation("pkgname")'.
## 
## Loading required package: GenomeInfoDb
## Loading required package: S4Vectors
## 
## Attaching package: 'S4Vectors'
## 
## The following object is masked from 'package:reshape':
## 
##     rename
## 
## Loading required package: IRanges
## 
## Attaching package: 'IRanges'
## 
## The following object is masked from 'package:reshape':
## 
##     expand
## 
## 
## Attaching package: 'AnnotationDbi'
## 
## The following object is masked from 'package:GenomeInfoDb':
## 
##     species
## 
## 
## 
## microbiome R package (microbiome.github.com)
##           
## 
## 
##  Copyright (C) 2011-2015
##           Leo Lahti and Jarkko Salojarvi 
## 
##         
##           <microbiome-admin@googlegroups.com>
## 
## 
## Attaching package: 'microbiome'
## 
## The following object is masked from 'package:lattice':
## 
##     densityplot
## 
## The following object is masked from 'package:e1071':
## 
##     impute
data.directory <- system.file("extdata", package = "microbiome")
l2.log.simulated <- read.profiling(level = "L2", 
                             data.dir = data.directory, log10 = TRUE)
## Reading /home/lei/R/x86_64-pc-linux-gnu-library/3.1/microbiome/extdata/L2-frpa.tab
## Logarithmizing the data
hitchip.matrix <- l2.log.simulated  

# Define two random groups for demonstration purpose
g1 <- sample(colnames(hitchip.matrix), 10)
g2 <- setdiff(colnames(hitchip.matrix), g1)

# Compare the two groups with t-test
pvalues <- c()
for (tax in rownames(hitchip.matrix)) {
  pvalues[[tax]] <- t.test(hitchip.matrix[tax, g1], hitchip.matrix[tax, g2])$p.value
}

# Order the taxa based on the p-values
ordered.results <- names(sort(pvalues))

# Assume there are some known true positives (here just randomly picked for demonstration)
true.positives <- sample(rownames(hitchip.matrix), 10)

# Overall ROC analysis (this will give the cumulative TPR and FPR along the ordered list)
res <- roc(ordered.results, true.positives)

# Calculate ROC/AUC value
auc <- roc.auc(ordered.results, true.positives)
print(auc)
## [1] 0.5533333
# Plot ROC curve
roc.plot(ordered.results, true.positives, line = TRUE)

plot of chunk roc-example

Clone this wiki locally