simulate.RL {nem} | R Documentation |
The function generate random network considering the selected types of triads.
simulate.RL(ideal.net, random.net, terms = "forb", k = 100, custom.terms = NULL, cr.type = "sum")
ideal.net |
ideal network structure of class |
random.net |
random or initial network of class |
terms |
which types of triads has to be considered (allowed |
k |
the number of iterations |
custom.terms |
additional terms to be considered (if |
cr.type |
how to calculate CR value (allways use |
new.network |
generated network |
change.ratio.vec |
vector of CR values, each value is calculated after each iteration |
##---- Should be DIRECTLY executable !! ---- ##-- ==> Define data, use random, ##-- or do help(data=index) for the standard data sets. ## The function is currently defined as function (ideal.net, random.net, terms = "forb", k = 100, custom.terms = NULL, cr.type = "sum") { teoreticna.porazdelitev <- summary(ideal.net ~ triadcensus) if (terms == "forb") allowed.terms <- names(teoreticna.porazdelitev)[teoreticna.porazdelitev == 0] if (terms == "allow") allowed.terms <- names(teoreticna.porazdelitev)[teoreticna.porazdelitev != 0] if (terms == "all") allowed.terms <- names(teoreticna.porazdelitev) if (terms == "cust") allowed.terms <- names(teoreticna.porazdelitev)[custom.terms] change.ratio.vec <- NULL new.network <- random.net for (i in 1:k) { pov <- sample(which(random.net == 0)[is.element(which(random.net == 0), which(diag(1, nrow = n) == 1)) == F], size = 1) npov <- sample(which(random.net == 1), size = 1) new.network[pov] <- 1 new.network[npov] <- 0 s.rand <- summary(random.net ~ triadcensus) s.new <- summary(new.network ~ triadcensus) if (cr.type == "mean") { nova.teoreticna <- mean((s.new[allowed.terms] - teoreticna.porazdelitev[allowed.terms])) stara.teoreticna <- mean((s.rand[allowed.terms] - teoreticna.porazdelitev[allowed.terms])) } if (cr.type == "sum") { nova.teoreticna <- sum((s.new[allowed.terms] - teoreticna.porazdelitev[allowed.terms])^2) stara.teoreticna <- sum((s.rand[allowed.terms] - teoreticna.porazdelitev[allowed.terms])^2) } change.ratio.vec[i] <- change.ratio <- nova.teoreticna/stara.teoreticna if (is.nan(change.ratio) == FALSE) { if (change.ratio < 1) random.net <- new.network else new.network <- random.net } else new.network <- random.net } return(list(new.network, change.ratio.vec)) }