Last active
September 1, 2019 01:40
-
-
Save SachaEpskamp/f918285a12a8cd6e386aaf1012878f9a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
compareNetworks <- function(true,est, directed = FALSE){ | |
cor0 <- function(x,y,...){ | |
if (sum(!is.na(x)) < 2 || sum(!is.na(y)) < 2 || sd(x,na.rm=TRUE)==0 | sd(y,na.rm=TRUE) == 0){ | |
return(0) | |
} else { | |
return(cor(x,y,...)) | |
} | |
} | |
bias <- function(x,y) mean(abs(x-y),na.rm=TRUE) | |
if (is.matrix(true) & is.matrix(est)){ | |
if (directed){ | |
real <- c(true) | |
est <- c(est) | |
} else { | |
real <- true[upper.tri(true,diag=FALSE)] | |
est <- est[upper.tri(est,diag=FALSE)] | |
} | |
} else { | |
real <- true | |
} | |
# True positives: | |
TruePos <- sum(est != 0 & real != 0) | |
# False pos: | |
FalsePos <- sum(est != 0 & real == 0) | |
# True Neg: | |
TrueNeg <- sum(est == 0 & real == 0) | |
# False Neg: | |
FalseNeg <- sum(est == 0 & real != 0) | |
out <- list() | |
### Sensitivity: | |
out$sensitivity <- TruePos / (TruePos + FalseNeg) | |
# Specificity: | |
out$specificity <- TrueNeg / (TrueNeg + FalsePos) | |
# Correlation: | |
out$correlation <- cor0(est,real) | |
out$bias <- bias(est,real) | |
out$truePos <- TruePos | |
out$falsePos <- FalsePos | |
out$trueNeg <- TrueNeg | |
out$falseNeg <- FalseNeg | |
return(out) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment