library("network") set.seed(1) # Various ways to initialize networks in statnet net <- network.initialize(5) net nmat <- matrix(rbinom(25, 1, 0.5), nr = 5, nc = 5) net <- network(nmat, loops = TRUE) net summary(net) net <- as.network(nmat, loops = FALSE) all(nmat == net[,]) net <- as.network(nmat, loops = TRUE) all(nmat == net[,]) net <- network.initialize(5, loops = TRUE) net[nmat>0] <- 1 all(nmat == net[,]) net[,] <- 0 net[,] <- nmat all(nmat == net[,]) net[,] <- 0 network::add.edges(net, row(nmat)[nmat>0], col(nmat)[nmat>0]) all(nmat == net[,]) net[,] <- nmat[,] all(nmat == net[,]) #diplay edgelist as.matrix(net,matrix.type = "edgelist") #Adding nodes and edges net <- network.initialize(5) network::add.edge(net, 2, 3) net[,] network::add.edges(net, c(3, 5), c(4, 4)) net[,] net[,2] <- 1 net[,] network::delete.vertices(net, 4) net[,] network::add.vertices(net, 2) net[,] #each edge can be thought of as a list made up of a vector #of tail vertex IDs, a vector of head vertex IDs, and a vector of attributes. #The utility function get.edges retrieves edges in this form, #returning them as lists with elements #inl (tail), #outl (head) #atl (attributes). get.edges allows for edges to be retrieved by endpoint(s) ?get.edges network::get.edges(net, 1) net[,] network::get.edges(net, 2, neighborhood = "in") get.edges(net, 1, alter = 2) gplot(net) get.neighborhood(net, 1) get.neighborhood(net, 2, type = "in") get.edgeIDs(net, 3, neighborhood = "out") delete.edges(net, get.edgeIDs(net, 3, neighborhood = "out")) net[,] #Other functions to test network structure data("flo") nflo <- network(flo, directed = FALSE) nflo is.adjacent(nflo, 9, 1) is.adjacent(nflo, 9, 4) has.loops(nflo) is.bipartite(nflo) is.directed(nflo) is.hyper(nflo) is.multiplex(nflo) as.matrix(nflo, matrix.type = "edgelist") plot(nflo, displaylabels = TRUE, boxed.labels = FALSE) plot(nflo, displaylabels = TRUE, mode = "circle") # create an hypergraph hg_ex <- matrix(c(1,0,0, 1,0,1, 0,0,1, 0,0,1, 0,1,0, 0,1,1, 0,1,0), byrow = T, ncol=3) p.names <- paste("v", 1:7) rownames(hg_ex) <- p.names a.names <- paste("e", 1:3) colnames(hg_ex) <- a.names HG <- network(hg_ex, matrix = "incidence", hyper = TRUE, directed = FALSE, loops = TRUE) HG[,] network::get.edges(HG, 1) prova <- network.initialize(15, bipartite=10, directed=F) bip <-network.initialize(5,bipartite=3) bip[,1] <- 1 get.network.attribute(bip,'bipartite') # how many vertices in first mode? add.vertices(bip,3,last.mode=FALSE) get.network.attribute(bip,'bipartite') ?add.edge library(statnet) pp<- as.matrix(bip, matrix.type="bipartite") pp <- as.edgelist.sna(bip, force.bipartite=TRUE) pp data("faux.magnolia.high") fmh <- faux.magnolia.high faux.magnolia.high fmh %v% "Sex" gplot(fmh) #prova grafo slides 7 17 aprile x1=c(0, 1, 1, 0,0,0,0) x2=c(1,0,0,1, 0, 0, 0) x3=c(1,0,0,0,1,0,0) x4=c(0,1,0,0,1,0,0) x5=c(0,0,1, 1, 0, 1,1) x6=c(0,0,0,0,1,0,1) x7=c(0,0,0,0,1,1,0) mm = matrix(c(x1,x2,x3,x4,x5,x6,x7), nrow=7) gplot(mm) bt <- betweenness(mm, gmode="graph", cmode="undirected") ?betweenness star = network.initialize(10, directed=F) ?network.initialize star[1,] <- 1 gplot(star) btstar <- betweenness(star, gmode="graph", cmode="undirected") library(statnet) gtrans(netgot[,]) save(enemy)