R/fit.structure.R

Defines functions fit.structure

Documented in fit.structure

#' Calculate structural fitness of networks
#'
#' The function is to used to calcuate the scale-free or modularity fitness of networks. The scale-free fitness is calcuated as
#' \eqn{-R^2\times slope} of the fit index of the model. Please refer to the package \code{\link[WGCNA]{WGCNA}} [1,2] for details.The fitness of modularity is computed
#' using \code{\link[igraph]{igraph}} package [3].
#' @param edgelist a data frame of network edge list, with regulators in first column, target genes in second column and edge weights in third column.
#' @param method a character string used to calculate the structural fitness. Options are "scale-free" and "modularity".
#' @return \code{fit.structure} returns a numeric value measures the structural fitness.
#' @references
#' [1] Langfelder P, Horvath S: WGCNA: an R package for weighted correlation network analysis. BMC Bioinformatics 2008, 9:559.
#'
#' [2] Zhao W, Langfelder P, Fuller T, Dong J, Li A, Hovarth S: Weighted gene coexpression network analysis: state of the art. J Biopharm Stat 2010, 20(2):281-300.
#'
#' [3] Csardi G, Nepusz T: The igraph software package for complex network research. InterJournal 2006, Complex Systems:1695.
#'
#' @export
#' @examples
#'  library(RLowPC)
#'  data(gnwdata)
#'  edgelist<-gnwdata$size500$net1
#'  edgelist[,3]<-1
#'  fit.structure(edgelist,method='scale-free')
#'  fit.structure(edgelist,method='modularity')
#'
fit.structure<-function(edgelist,method='scale-free'){
  degree<-node.degree(edgelist,directed = T)
  if('scale-free' %in% method){
    fitness<-WGCNA::scaleFreeFitIndex(degree)
    fitness<-as.numeric(-sign(fitness$slope.SFT)*fitness$Rsquared.SFT)
  }
  if('modularity' %in% method){
    g<-igraph::graph.data.frame(edgelist,directed = T)
    wtc <- igraph::walktrap.community(g)
    fitness<-igraph::modularity(wtc)
  }
  return(fitness)
}
wyguo/RLowPCor documentation built on May 4, 2019, 12:04 p.m.