#we want to create a dynamic network with 10 nodes and no edges dyn <- as.dynamic(network.initialize(10)) #now we add an edge with a time interval 1 to 10, linking nodes 1 and 2 dyn<-add.edge.dynamic(dyn,c(1,10),1,2) #now we add a bunch more edges with various times dyn <- add.edge.dynamic(dyn,c(2,10),2,3) dyn <- add.edge.dynamic(dyn,c(3,10),3,4) dyn <- add.edge.dynamic(dyn,c(4,10),4,5) dyn <- add.edge.dynamic(dyn,c(5,10),5,6) dyn <- add.edge.dynamic(dyn,c(6,10),6,7) dyn <- add.edge.dynamic(dyn,c(6,10),7,8) dyn <- add.edge.dynamic(dyn,c(7,10),8,9) dyn <- add.edge.dynamic(dyn,c(8,10),9,1) dyn <- add.edge.dynamic(dyn,c(9,10),10,1) dyn <- add.edge.dynamic(dyn,c(9,20),10,1) dyn <- add.edge.dynamic(dyn,c(9,20),10,5) dyn <- add.edge.dynamic(dyn,c(9,20),10,3) dyn <- add.edge.dynamic(dyn,c(9,20),10,8) #now add some attributes #first give vertex ten the color blue at time 8 dyn <- set.dynamic.vertex.attribute(dyn,"color","blue",valid.time=8,v=10) dyn <- set.dynamic.vertex.attribute(dyn,"color","green",valid.time=9,v=10) dyn <- set.dynamic.vertex.attribute(dyn,"color","blue",valid.time=10,v=10) #now plot a series of slices through our example network #to show it at various times par(mfrow=c(3,3)) for(p in 5:13){ slice <-get.slice.network(dyn,p) plot(slice,vertex.col=get.vertex.attribute(slice,"color"),vertex.cex=5, main=p) } plot.intervals(dyn,pickSlice=TRUE) plot(dyn) ## Application to faux.mesa.high #load the faux.mesa.high data data(faux.mesa.high) mesa <- faux.mesa.high #check what attributes are present list.vertex.attributes(mesa) #define an exponential random graph model includes various attribute effects # no dd terms fauxmodel.01 <- ergm(mesa ~ edges +nodefactor("Race") +nodematch("Race",diff=T) +nodefactor("Grade") +nodematch("Grade",diff=T) +nodefactor("Sex") +nodematch("Sex")) #generate 20 networks using one of every 100 draws so there is enough #change between networks for us to see something. fauxsim <- simulate(fauxmodel.01,nsim=20,interval=100) #load the rSoNIA and dynamicnetwork libraries library(rSoNIA) #convert the simulation output to a dynamic network object fauxdyn <- as.dynamic(fauxsim$networks,check.renewal=FALSE) #launch sonia with the basic network just to test launchSonia(fauxdyn) #NOTE: above does not produce output #get the sex attribute, convert it to shapes, and save it back shapes <- get.vertex.attribute(fauxdyn,"Sex") shapes <- replace(shapes,shapes==1,"square") shapes <- replace(shapes,shapes==2,"circle") #now we set the shape property of all the vertices fauxdyn <- set.vertex.attribute(fauxdyn,"shapes",shapes) #set vertex colors using results of a function set.vertex.attribute(fauxdyn,"color", colorize(get.vertex.attribute(fauxdyn,"Race"))) # NOTE ˆ the output here is entertaining, but only for PH launchSonia(fauxdyn, vertex.col="color", usearrows=FALSE, displaylabels=FALSE, vertex.shape="shape", vertex.cex="Grade" ) ## USING SONIA IN BATCH MODE #need to specify some of the settings so it won‚t use defaults #set desired spacing between connected nodes soniaApplySettings$kk.opt.dist[2]<-20 #specify distance between un-connected nodes and components soniaApplySettings$kk.comp.connect[2] <- 10 #increase height of the movie to 500 pixels soniaGraphicsSettings$layout.height[2]<-500 #decrease number of interpolated frames to reduce file size soniaBrowseSettings$num.frames[2]<-10 #generate a flash movie in batch mode launchSonia(fauxDyn, max.iter=5000, interactive=FALSE, vertex.col="color", usearrows=FALSE, displaylabels=FALSE, vertex.shape="shape", vertex.cex="Grade", movie.file="myMovie", movie.type="swf") #same command, but change movie.type to "mov" instead of "swf" launchSonia(fauxDyn, max.iter=5000, interactive=FALSE, vertex.col="color", usearrows=FALSE, displaylabels=FALSE, vertex.shape="shape", vertex.cex="Grade", movie.file="myMovie", movie.type="mov") source("buildInfectNet.R") edgeTiming <- read.table("tel.conrsmons.txt", header=TRUE) edgeTiming[1:3,] infectStatus <- read.table("infect.conrsmons.b1.10comseed.05.txt", header=TRUE, fill=TRUE) names(infectStatus) infectNgh<- buildInfectNet(edgeTiming, infectStatus, neighbors=TRUE) plot(infectNgh,vertex.cex=0.3,edge.lwd=0.1, vertex.col=hiliteColor(infectStatus$serostatus[infectNgh%v%"vertex.names"])) seedNormal <- buildInfectNet(edgeTiming, infectStatus, neighbors=FALSE, keep.infected=FALSE, color.mode="seed") soniaApplySettings$kk.opt.dist[2] <- 5 soniaApplySettings$kk.comp.connect[2] <- 60 soniaGraphicsSettings$layout.height[2] <- 600 soniaGraphicsSettings$layout.width[2] <- 600 soniaGraphicsSettings$arc.width.fact[2] <- 2 soniaGraphicsSettings$node.trans[2] <- 1 soniaGraphicsSettings$node.trans[2] <- 1 soniaLayoutSettings$slice.duration[2] <- 60 soniaLayoutSettings$slice.delta[2] <- 30; soniaBrowseSettings$num.frames[2] <- 5 soniaBrowseSettings$render.dur[2] <- 60 launchSonia(seedNormal, vertex.col="color", arc.col="color", vertex.shape="shape", vertex.cex="size", usearrows=FALSE, displaylabels=FALSE, max.iter=5000, interactive=FALSE, movie.file=file.path(getwd(),"simSeedNormal")); soniaLayoutSettings$slice.duration[2] <- 3650 soniaLayoutSettings$slice.delta[2] <- 3650 soniaApplySettings$rescale.layout[2] <- "rescale layout to fit window" launchSonia(seedNormal, vertex.col="color", arc.col="color", vertex.shape="shape", vertex.cex="size", usearrows=FALSE, displaylabels=FALSE, max.iter=5000, interactive=TRUE) seedSustain <- buildInfectNet(edgeTiming, infectStatus, neighbors=FALSE, keep.infected=TRUE, color.mode="seed") soniaLayoutSettings$slice.duration[2] <- 60; soniaLayoutSettings$slice.delta[2] <- 30 soniaApplySettings$rescale.layout[2] <- "none" launchSonia(seedSustain, vertex.col="color", arc.col="color", vertex.shape="shape", vertex.cex="size", usearrows=FALSE, displaylabels=FALSE, max.iter=5000, interactive=FALSE, movie.file=file.path(getwd(),"simSeedSustain")) raceSustain <- buildInfectNet(edgeTiming, infectStatus, neighbors=FALSE, keep.infected=TRUE, color.mode="race"); launchSonia(raceSustain, vertex.col="color", arc.col="color", vertex.shape="shape",vertex.cex="size", usearrows=FALSE, displaylabels=FALSE, max.iter=5000, interactive=FALSE, movie.file=file.path(getwd(),"simRaceSustain")) ageSustain <- buildInfectNet(edgeTiming, infectStatus, neighbors=FALSE, keep.infected=TRUE, color.mode="age"); launchSonia(ageSustain, vertex.col="color", arc.col="color", vertex.shape="shape", vertex.cex="size", usearrows=FALSE, displaylabels=FALSE, max.iter=5000, interactive=FALSE, movie.file=file.path(getwd(),"simAgeSustain")) launchSonia(concurSustain, vertex.col="color", arc.col="color", vertex.shape="shape", vertex.cex="size", usearrows=FALSE, displaylabels=FALSE, max.iter=5000, interactive=FALSE, movie.file=file.path(getwd(),"simConcurSustain"))