Difference between revisions of "Plotfun.R"

From Organic Design wiki
(Jun 18 2005)
 
(June 5 2005)
Line 34: Line 34:
 
      
 
      
 
     if(what=="U" || what=="Spec" || what=="UT" || what=="FNDR"){
 
     if(what=="U" || what=="Spec" || what=="UT" || what=="FNDR"){
        U[[deparse(as.numeric(i))]] <- m0[i]-data$V[[i]]
+
      U[[deparse(as.numeric(i))]] <- m0[i]-data$V[[i]]
        if(what=="Spec"){
+
      if(what=="Spec"){
 
             tmp <- U[[i]]/m0[i]
 
             tmp <- U[[i]]/m0[i]
 
             tmp[is.nan(tmp)] <- 0
 
             tmp[is.nan(tmp)] <- 0
Line 50: Line 50:
 
     }
 
     }
 
     if(what=="S" || what=="Sens" || what=="R" || what=="FDR"  || what=="PrFDReq1" || what=="PrRgt0" || what=="Power"){
 
     if(what=="S" || what=="Sens" || what=="R" || what=="FDR"  || what=="PrFDReq1" || what=="PrRgt0" || what=="Power"){
        S[[deparse(as.numeric(i))]] <- m1[i]-data$T[[i]]
+
    S[[deparse(as.numeric(i))]] <- m1[i]-data$T[[i]]
        if(what=="R" || what =="Sens" || what=="FDR" || what=="PrFDReq1" || what=="PrRgt0" || what=="Power"){
+
      if(what=="R" || what =="Sens" || what=="FDR" || what=="PrFDReq1" || what=="PrRgt0" || what=="Power"){
 
         R[[deparse(as.numeric(i))]] <- (S[[i]]+data$V[[i]])
 
         R[[deparse(as.numeric(i))]] <- (S[[i]]+data$V[[i]])
 
         if(what=="Sens"){
 
         if(what=="Sens"){
Line 88: Line 88:
 
#extractstats(F331, what="PrFDReq1")
 
#extractstats(F331, what="PrFDReq1")
  
plotfun <- function(FDlist, what="FDR", probs=c(0.025,0.975), xlim, ylim, add=F, col="black", lty=1, xpos, ypos=0.1, psfile=F,closefile=F,method="", main, CIs = T, hexbinning=F,binNo=200, xlab = expression(paste(pi[0], " proportion of Genes that do not change")),...){
+
plotfun <- function(FDlist, what="FDR", probs=c(0.025,0.975), xlim, ylim, add=F, col="black", lty=1, xpos, ypos=0.1, psfile=F,closefile=F,method="", main, CIs = T, hexbinning=F,binNo=200, ...){
 
#Problems fixed... main was missing, method has no default if options not found...
 
#Problems fixed... main was missing, method has no default if options not found...
 
if(is.character(psfile)){
 
if(is.character(psfile)){
Line 137: Line 137:
 
               "Power" = "P(R>0) - False Discovery Rate",
 
               "Power" = "P(R>0) - False Discovery Rate",
 
               "PrFDReq1" = "P(FDR=1)",
 
               "PrFDReq1" = "P(FDR=1)",
               "Spec" = "Specificity, U/m0",
+
               "Spec" = "Specificity, # rejected over m1",
               "Sens" = "Sensitivity, S/m1",
+
               "Sens" = "Sensitivity, # not rejected over m0",
 
               "p0" = "Proportion of genes not changing",               
 
               "p0" = "Proportion of genes not changing",               
 
               "Alphap0" = "alpha* . pi[0]"                 
 
               "Alphap0" = "alpha* . pi[0]"                 
Line 183: Line 183:
 
   }
 
   }
 
}else{
 
}else{
   plot(p, uCI, type="n", xlim=xlim, ylim=ylim, xlab=xlab, ylab=ylab, main=main)
+
   plot(p, uCI, type="n", xlim=xlim, ylim=ylim, xlab=expression(paste(pi[0], " known")), ylab=ylab, main=main)
 
   if(hexbinning){
 
   if(hexbinning){
 
     xbin <- rep(p, each=length(data[[1]]))
 
     xbin <- rep(p, each=length(data[[1]]))

Revision as of 02:21, 25 September 2006

extractstats <- function(data, what="FDR", alpha=0.05){

  1. Options for what: U, V, T, S, R, UT, FDR, FNDR, TaddV, PrRgt0, Power, PrReq1, p0, p0NW, Alphap0
  2. UT equals "m-R"
 U <- S <- R <- UT <- FDR <- FNDR <- TaddV<- PrRgt0 <- p0 <- p0NW <- Alphap0 <- list()
 Power <- PrFDReq1 <- Sens <- Spec <- list()
 Pchar <- names(data$V)
 p <- as.numeric(Pchar)
 m0 <- round(p * data$m)
 names(m0) <- Pchar
 m1 <- round((1-p)*data$m)
 names(m1) <- Pchar
 if(what=="V"){
   return(data$V)
 }
 if(what=="T"){
   return(data$T)
 }
 if(what=="p0"){
     return(data$p0)
 }	
 if(what=="p0NW"){
     return(data$p0NW)
 }	
 for(i in Pchar){
   if(what=="Alphap0"){
     Alphap0deparse(as.numeric(i)) <-  (alpha * as.numeric(i)) / data$p0i
   }
   if(what=="TaddV"){
     TaddVdeparse(as.numeric(i)) <- (data$Ti+data$Vi)
   }
   
   if(what=="U" || what=="Spec" || what=="UT" || what=="FNDR"){
     Udeparse(as.numeric(i)) <- m0[i]-data$Vi
     if(what=="Spec"){
           tmp <- Ui/m0[i]
           tmp[is.nan(tmp)] <- 0
           Specdeparse(as.numeric(i)) <- tmp
     }
     if(what=="UT" || what=="FNDR"){
       UTdeparse(as.numeric(i)) <- (Ui+data$Ti)
       if(what=="FNDR"){
         tmp <-  data$Ti/(UTi)
         tmp[is.nan(tmp)] <- 0
         FNDRdeparse(as.numeric(i)) <- tmp
       }
     }
   }
   if(what=="S" || what=="Sens" || what=="R" || what=="FDR"  || what=="PrFDReq1" || what=="PrRgt0" || what=="Power"){
   Sdeparse(as.numeric(i)) <- m1[i]-data$Ti
     if(what=="R" || what =="Sens" || what=="FDR" || what=="PrFDReq1" || what=="PrRgt0" || what=="Power"){
       Rdeparse(as.numeric(i)) <- (Si+data$Vi)
       if(what=="Sens"){

tmp <- Si/m1[i] tmp[is.nan(tmp)] <- 0 Sensdeparse(as.numeric(i)) <- tmp

       } 
       
       if(what=="FDR" || what=="PrFDReq1" || what=="Power"){

         tmp <-  data$Vi/(Ri)
         tmp[is.nan(tmp)] <- 0
         FDRdeparse(as.numeric(i)) <- tmp
       }
      
       if(what=="PrFDReq1"){
         tmp <- FDRi==1
         PrFDReq1deparse(as.numeric(i)) <- tmp
       }
       
       if(what=="PrRgt0" || what=="Power"){
         tmp <- Ri>0
         PrRgt0deparse(as.numeric(i)) <- tmp
       }
       if(what == "Power"){
         tmp <- PrRgt0i - FDRi
         Powerdeparse(as.numeric(i)) <- tmp
       }
     }
   }
 }

return(get(what)) }

  1. extractstats2(F331, what="PrRgt0")
  2. extractstats(F331, what="PrFDReq1")

plotfun <- function(FDlist, what="FDR", probs=c(0.025,0.975), xlim, ylim, add=F, col="black", lty=1, xpos, ypos=0.1, psfile=F,closefile=F,method="", main, CIs = T, hexbinning=F,binNo=200, ...){

  1. Problems fixed... main was missing, method has no default if options not found...

if(is.character(psfile)){

 postscript(psfile,horizontal=F, height=297/25.4, width=210/25.4)
 parcalls <- list(...)
 par(pty="s", mai=rep(1,4))
 for(i in names(parcalls)){
   par(i=parcallsi)
 }

} if(hexbinning){

 library(hexbin)

}

sim <- deparse(substitute(FDlist))

if(charmatch("EBarrays",sim, nomatch=0)){

 method <- "Empirical Bayes"

} if(charmatch("Multtest",sim, nomatch=0)){

 method <- "Within Gene t-tests"

} if(missing(main)){

 main <- "Model Unknown"

} if(length(grep("GG", sim))){

 main <- "GG-B model"

} if(length(grep("LNN",sim))){

 main <- "LNN-B model"

} if(length(grep("GU", sim))){

 main <- "GU-B model"

} data <- extractstats(FDlist, what)

ylab <- switch(what,

             "FDR"  = "False Discovery Rate",
             "FNDR" = "False Non-Discovery Rate",
             "TaddV"= "# Type I and II errors",
             "U"    = "# not rejected given EE",
             "V"    = "# rejected given EE",
             "T"    = "# not rejected given DE",
             "S"    = "# rejected given DE",
             "UT"   = "# not rejected",
             "R"    = "# rejected",
             "PrRgt0" = "P(R>0)",
             "Power" = "P(R>0) - False Discovery Rate",
             "PrFDReq1" = "P(FDR=1)",
             "Spec" = "Specificity, # rejected over m1",
             "Sens" = "Sensitivity, # not rejected over m0",
             "p0" = "Proportion of genes not changing",               
             "Alphap0" = "alpha* . pi[0]"                 	 
             )

p <- as.numeric(names(data)) alpha <- FDlist$alpha

if(missing(ylim)){

 ylim <- c(0,0.4)

} if(missing(xlim)){

 xlim <- c(0,1)

} if(missing(xpos)){

 xpos <- 0.1

}

lprob <- probs[1] uprob <- probs[2]

xbar <- sapply(data, mean) lCI <- sapply(data, quantile, probs=lprob) uCI <- sapply(data, quantile, probs=uprob)

  1. CIfudge <- c(seq(1,195,length=98),196:201)

if(add){

 if(hexbinning){
   xbin <- rep(p, each=length(data1))
   ybin <- unlist(data)
   bins <- hexbin(xbin, ybin, xbins=200)
   hexagons(bins)
 } 
   
 lines(p, xbar, col=col, lty=lty)
 if(CIs){
   lines(p, lCI, col=col,lty=lty)
   lines(p, uCI, col=col,lty=lty)
  1. lines(p, lCI, col="darkgrey",lty=lty)
  2. lines(p, uCI, col="darkgrey",lty=lty)
  1. lines(p[CIfudge], lCI[CIfudge], col="darkgrey",lty=lty)
  2. lines(p[CIfudge], uCI[CIfudge], col="darkgrey",lty=lty)
 }

}else{

 plot(p, uCI, type="n", xlim=xlim, ylim=ylim, xlab=expression(paste(pi[0], " known")), ylab=ylab, main=main)
 if(hexbinning){
   xbin <- rep(p, each=length(data1))
   ybin <- unlist(data)
   bins <- hexbin(xbin, ybin, xbins=binNo)
   hexagons(bins)
 } 
   lines(p, xbar, col=col, lty=lty)
   if(CIs){
     lines(p, lCI, col=col,lty=lty)
     lines(p, uCI, col=col,lty=lty)
   }
 if(what=="FDR") {
   lines(p, rep(alpha, length(p)), lty=3)
   lines(p, p*rep(alpha, length(p)), lty=3)
 }

} if(closefile){

 dev.off()

} return() }

 save.image()
 
 
  1. Little function to find the maximum quickly for any statistic of interest

maxquantile<- function(dset, what, prob, subsetpi0=NULL){ if(!is.null(subsetpi0)){ tmp <- sapply(extractstats(dset, what)[subsetpi0], quantile, prob) }else{ tmp <- sapply(extractstats(dset, what), quantile, prob) } maxquantile <- max(tmp) tmp[tmp==maxquantile] }

minquantile<- function(dset, what, prob, subsetpi0=NULL){ if(!is.null(subsetpi0)){ tmp <- sapply(extractstats(dset, what)[subsetpi0], quantile, prob) }else{ tmp <- sapply(extractstats(dset, what), quantile, prob) } maxquantile <- min(tmp) tmp[tmp==maxquantile] }

  1. Little function to find the maximum quickly for any statistic of interest

maxmean<- function(dset, what, subsetpi0=NULL){ if(!is.null(subsetpi0)){ tmp <- sapply(extractstats(dset, what)[subsetpi0], mean) }else{ tmp <- sapply(extractstats(dset, what), mean) }

maxmean <- max(tmp) tmp[tmp==maxmean] }