R/null.lg.fit.R

#--------------------------------------------
#' @title null.lg.fit
#' 
#' @description Gaussian fit for null similarity scores.
#' 
#' @details The gaussian distribution is a distribution which often gives 
#' a good fit to the null log similarity scores when these scores are on a scale of 0 to 1. Other than 
#' standardization, this function assumes that if the user wants the scores to be transformed, they've 
#' transformed them. This routine calls the \code{fitdistr} function from the MASS package.
#' 
#' @param score.null.vec  Vector of null (non-match) similarity scores or transformed similarity scores
#' @param standardizeQ    Whether or not to standardize the null scores
#' @param plotQ           Diagnostic plots?
#' 
#' @return  list with the fitted parameters, fit info and chi-square goodness of fit test results
#'
#' @references XXXX
#'
#' @examples
#' XXXX
#--------------------------------------------

null.lg.fit <- function(score.null.vec, standardizeQ=FALSE, plotQ=FALSE) {
  
  #Take the log:
  lgs <- score.null.vec
  
  if(standardizeQ==TRUE){
    lgs <- (lgs - mean(lgs))/sd(lgs)
  }
  
  lgs <- sort(lgs)
  
  normfit <- MASS::fitdistr(lgs,"normal")
  #If data was standardized, these should be pretty close to 0 and 1:
  mu <- normfit$estimate[1]
  sig <- normfit$estimate[2]
  
  dens <- dnorm(lgs, mean=mu, sd=sig)
  
  #Compute AIC and BIC for comparison to other fits
  llk <- sum(log(dens))
  N<-length(lgs)
  k<-2
  aic <- -2* llk + (2*N*k/(N-k-1))
  bic <- -2* llk + k*log(N)
  
  #This is needed for both plots and fit diagnostics:
  lgs.hist.info <- hist(lgs,plot=F)
  
  if(plotQ==TRUE){
    
    print("Rendering diagnostic plots...")
    
    split.screen( figs = c( 1, 2 ) )
    
    screen(1)
    
    ylim.max <- max(dens,lgs.hist.info$density)
    xlim.max <- max(lgs,lgs.hist.info$breaks)
    xlim.min <- min(lgs,lgs.hist.info$breaks)
    
    if(standardizeQ==TRUE){
      fittitle <- "Gaussian fit to standardized log(null)"
    } else {
      fittitle <- "Gaussian fit to log(null)"
    }
    
    plot(lgs, dens, xlim=c(xlim.min,xlim.max), ylim=c(0,ylim.max), typ="l", col="blue", lwd=3, xlab="", ylab="")
    par(new=T)
    hist(lgs, probability=T, xlim=c(xlim.min,xlim.max), ylim=c(0,ylim.max), xlab="STD log(KNM)", main=fittitle)
    
    #Q-Q Plot:
    #t-axis:
    tmax<-1000
    tax<-seq(1,tmax,1)/(tmax+1)
    cemp<-ecdf(lgs)(lgs)
    #Empirical quantile function (inverse CDF):
    qemp<-splinefun(cemp,lgs)
    #Empirical quantiles:
    Zt<-qemp(tax)
    #Quantiles from fit:
    Zt.hat<-qnorm(tax, mean=mu, sd=sig)
    
    if(standardizeQ==TRUE){
      QQtitle <- "Q-Q plot for Gaussian fit to standardized log(null) hist"
    } else {
      QQtitle <- "Q-Q plot for Gaussian fit to log(null) hist"
    }
    
    #Q-Q plot:
    screen(2)
    plot(Zt,Zt.hat, xlab="empirical quantiles", ylab="fit quantiles",main=QQtitle)
    abline(0,1)
    
    close.screen( all = TRUE )
    
  }
  
  freq.obs <- lgs.hist.info$counts
  freq.expec <- rep(-1,length(lgs.hist.info$mids))
  fit.probs <- rep(-1,length(lgs.hist.info$mids))
  
  print("Computing fit interquantile probabilities...")
  for(i in 1:(length(lgs.hist.info$breaks)-1)){
    
    upi <- lgs.hist.info$breaks[i+1]
    loi <- lgs.hist.info$breaks[i]
    
    #    print(paste("Bin:",i,
    #                "Upper:",upi,
    #                "Lower:",loi,
    #                "p-upper:",pssd(upi, ssdfit),
    #                "p-lower:",pssd(loi, ssdfit),
    #                "prob:", pssd(upi, ssdfit) - pssd(loi, ssdfit)
    #    )
    #    )
    
    fit.probs[i] <- pnorm(upi, mean=mu, sd=sig) - pnorm(loi, mean=mu, sd=sig)
    freq.expec[i] <- fit.probs[i] * length(score.null.vec)
  }
  
  plt <- pnorm(lgs.hist.info$breaks[1], mean=mu, sd=sig)
  prt <- 1-sum(c(plt,fit.probs))
  fit.probs <- c(plt,fit.probs,prt)
  
  fit.info <- cbind(fit.probs, c(plt*length(score.null.vec), freq.expec, prt*length(score.null.vec)), c(0,freq.obs,0))
  colnames(fit.info) <- c("interquant.probs", "interquant.exp.cts", "interquant.obs.cts")
  chisq.results <- chisq.test(c(0,freq.obs,0), p = fit.probs)
  
  fit.params <- c(mu,sig)
  names(fit.params) <- c("mu.est","sig.est")
  
  info.list <- list(fit.params, fit.info, chisq.results, normfit, aic, bic)
  names(info.list) <- c("parameters", "fit.info", "chi.square.test", "fit.obj", "AIC", "BIC")
  
  return(info.list)
  
}
npetraco/fdrID documentation built on May 23, 2019, 9:33 p.m.