# NOT RUN {
## For these examples, first generate a scale free network using preferential attachment:
# Number of nodes:
n <- 100
# Empty vector with Degrees:
Degs <- rep(0, n)
# Empty Edgelist:
E <- matrix(NA, n - 1, 2)
# Add and connect nodes 1 and 2:
E[1, ] <- 1:2
Degs[1:2] <- 1
# For each node, add it with probability proportional to degree:
for (i in 2:(n - 1))
{
E[i, 2] <- i + 1
con <- sample(1:i, 1, prob = Degs[1:i]/sum(Degs[1:i]),i)
Degs[c(con,i+1)] <- Degs[c(con,i+1)] + 1
E[i, 1] <- con
}
# Because this is an edgelist we need a function to convert this to an adjacency matrix:
E2adj <- function(E,n)
{
adj <- matrix(0,n,n)
for (i in 1:nrow(E))
{
adj[E[i,1],E[i,2]] <- 1
}
adj <- adj + t(adj)
return(adj)
}
### EXAMPLE 1: Animation of construction algorithm: ###
adjs <- lapply(1:nrow(E),function(i) E2adj(E[1:i,,drop=FALSE],n))
qgraph.animate(adjs,color="black",labels=FALSE,sleep=0.1, smooth = FALSE)
rm(adjs)
### EXAMPLE 2: Add nodes by final degree: ###
adj <- E2adj(E,n)
qgraph.animate(E2adj(E,n),color="black",labels=FALSE,constraint=100,sleep=0.1)
### EXAMPLE 3: Changing edge weights: ###
adjW <- adj*rnorm(n^2)
adjW <- (adjW + t(adjW))/2
adjs <- list(adjW)
for (i in 2:100)
{
adjW <- adj*rnorm(n^2)
adjW <- (adjW + t(adjW))/2
adjs[[i]] <- adjs[[i-1]] + adjW
}
qgraph.animate(adjs,color="black",labels=FALSE,constraint=100,sleep=0.1)
# }
Run the code above in your browser using DataLab