simulate.RL {nem}R Documentation

The Relocating Links algorithm

Description

The function generate random network considering the selected types of triads.

Usage

simulate.RL(ideal.net, random.net, terms = "forb", k = 100, custom.terms = NULL, cr.type = "sum")

Arguments

ideal.net

ideal network structure of class matrix

random.net

random or initial network of class matrix

terms

which types of triads has to be considered (allowed allow, forbidden forb, all all or custom cust)

k

the number of iterations

custom.terms

additional terms to be considered (if terms = "cust")

cr.type

how to calculate CR value (allways use sum)

Value

new.network

generated network

change.ratio.vec

vector of CR values, each value is calculated after each iteration

Examples

##---- 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))
  }

[Package nem version 0.12.33 Index]