"calc.graph.pars" <- function(sp.mat,names,rank,sub,maxWgt=1,skipSP=FALSE,verbose=FALSE) { CC <- function(mat,maxWgt) apply(mat,1,function(x,mat){x%*%mat%*%x},mat=mat)/(apply(mat,1,sum)^2-apply(mat^2,1,sum))/maxWgt N <- length(rank) DE <- rank<=sub graph.all <- sparseM2Graph(sp.mat,names,"undirected") graph.A <- sparseM2Graph(sp.mat[DE,DE],names[DE],"undirected") graph.B <- sparseM2Graph(sp.mat[!DE,!DE],names[!DE],"undirected") deg <- sapply(edgeWeights(graph.all),sum) Eall <- mean(deg) EA <- mean(deg[DE]) EB <- mean(deg[!DE]) beta <- EA/EB pA <- sum(sapply(edgeWeights(graph.A),sum))/2 / (choose(sub,2)*maxWgt) pB <- sum(sapply(edgeWeights(graph.B),sum))/2 / (choose(N-sub,2)*maxWgt) pAB <- (EA/maxWgt-(sub-1)*pA)/(N-sub) relcon <- pA/pAB mat.temp <- as.matrix(sp.mat) coef.all <- CC(mat.temp,maxWgt) mat.temp <- as.matrix(sp.mat[DE,DE]) coef.A <- CC(mat.temp,maxWgt) mat.temp <- as.matrix(sp.mat[!DE,!DE]) coef.B <- CC(mat.temp,maxWgt) mean.coef.all <- mean(coef.all,na.rm=TRUE) mean.coef.A <- mean(coef.A,na.rm=TRUE) mean.coef.B <- mean(coef.B,na.rm=TRUE) nan.all <- sum(is.nan(coef.all)) nan.A <- sum(is.nan(coef.A)) nan.B <- sum(is.nan(coef.B)) if(skipSP) mean.sp.all <- mean.sp.A <- mean.sp.B <- inf.all <- inf.A <- inf.B <- NA else { sp.temp <- johnson.all.pairs.sp(graph.all) mean.sp.all <- mean(sp.temp[sp.temp != Inf]) inf.all <- sum(sp.temp == Inf) sp.temp <- johnson.all.pairs.sp(graph.A) mean.sp.A <- mean(sp.temp[sp.temp != Inf]) inf.A <- sum(sp.temp == Inf) sp.temp <- johnson.all.pairs.sp(graph.B) mean.sp.B <- mean(sp.temp[sp.temp != Inf]) inf.B <- sum(sp.temp == Inf) } if(verbose) { cat(paste("E_deg(all) = ",Eall,"\n",sep="")) cat(paste("E_deg(A) = ",EA,"\n",sep="")) cat(paste("E_deg(B) = ",EB,"\n",sep="")) cat(paste("beta = ",beta,"\n",sep="")) cat(paste("p_A = ",pA,"\n",sep="")) cat(paste("p_B = ",pB,"\n",sep="")) cat(paste("p_AB = ",pAB,"\n",sep="")) cat(paste("Rel. conn. = ",relcon,"\n",sep="")) cat(paste("Mean clust. coef. overall / NAN = ",mean.coef.all,"\t",nan.all,"\n",sep="")) cat(paste("Mean clust. coef. A / NAN = ",mean.coef.A,"\t",nan.A,"\n",sep="")) cat(paste("Mean clust. coef. B / NAN = ",mean.coef.B,"\t",nan.B,"\n",sep="")) cat(paste("Mean shortest path overall / INF = ",mean.sp.all,"\t",inf.all,"\n",sep="")) cat(paste("Mean shortest path A / INF = ",mean.sp.A,"\t",inf.A,"\n",sep="")) cat(paste("Mean shortest path B / INF = ",mean.sp.B,"\t",inf.B,"\n",sep="")) } result <- list(Eall=Eall,EA=EA,EB=EB,beta=beta,pA=pA,pB=pB,pAB=pAB,rconn=relcon,mcoefALL=mean.coef.all,mcoefA=mean.coef.A,mcoefB=mean.coef.B,mspALL=mean.sp.all,mspA=mean.sp.A,mspB=mean.sp.B) return(result) }