Download R source file
setwd("your working directory")
library(igraph)
size=50
g = graph.tree(size, children = 2, mode="undirected")
plot(g)
seeds_num = 1
set.seed(2016); diffusers = sample(1:vcount(g),seeds_num); diffusers
diffusers
g[[diffusers,]]
nearest_neighbors<-unlist(g[[diffusers,]])
p=0.4
sample(c(1,0),length(nearest_neighbors),replace=T, prob=c(p,1-p))
is.infected<-sample(c(1,0),length(nearest_neighbors),replace=T, prob=c(p,1-p))
is.infected
new.infected<-nearest_neighbors[which(is.infected==1)]
diffusers<-c(diffusers,new.infected)
diffusers
V(g)$color = "white"
V(g)$color[V(g)%in%diffusers] = "red"
plot(g)
update_diffusers = function(diffusers){
nearest_neighbors = unlist(ego(g, 1, diffusers))
nearest_neighbors = setdiff(nearest_neighbors, diffusers)
n=length(nearest_neighbors)
is.infected=sample(c(1,0),n,replace=T, prob=c(p,1-p))
new_infected = nearest_neighbors[which(is.infected==1)]
diffusers = unique(c(diffusers, new_infected))
return(diffusers)
}
infected=list()
infected[[1]] = sample(1:vcount(g),seeds_num)
i = 1
while(length(infected[[i]]) < size){
infected[[i+1]] = update_diffusers(infected[[i]]) %>% sort()
print(paste("Tick:",i,"Infected:",length(infected[[i]])))
i = i + 1
}
num_cum = lapply(1:i, function(x) length(infected[[x]])) %>% unlist()
p_cum = num_cum/max(num_cum)
time = 1:i
plot(main="Proportion infected",p_cum~time, type = "b")
l<-layout.fruchterman.reingold(g)
par(mfrow=c(2,2), mar=c(1,1,1,1))
plot(g, main="Day 1", vertex.color=ifelse(V(g)%in%infected[[1]],"tomato","cadetblue3"), layout=l, vertex.label=NA)
plot(g, main="Day 8", vertex.color=ifelse(V(g)%in%infected[[8]],"tomato","cadetblue3"), layout=l, vertex.label=NA)
plot(g, main="Day 16", vertex.color=ifelse(V(g)%in%infected[[16]],"tomato","cadetblue3"), layout=l, vertex.label=NA)
plot(g, main=paste("Day",i), vertex.color=ifelse(V(g)%in%infected[[i]],"tomato","cadetblue3"), layout=l, vertex.label=NA)
dev.off()
install.packages("animation")
library(animation)
saveGIF({
m = 1
while(m <= length(infected)){
V(g)$color = "cadetblue3"
V(g)$color[V(g)%in%infected[[m]]] = "tomato"
day<-paste("Day",m)
plot(g, edge.color="gray85", vertex.frame.color="white", vertex.size=10, edge.width=3, layout =l)
title(day, cex.main=2.5, col.main="dodgerblue4")
m = m + 1}
},
interval = .8, ani.width = 640, ani.height = 640, movie.name="endemic_animation.gif")
saveGIF({
m = 1
while(m <= length(infected)){
par(mfrow=c(1,2))
V(g)$color = "cadetblue3"
V(g)$color[V(g)%in%infected[[m]]] = "tomato"
day<-paste("Day",m)
plot(g, edge.color="gray85", vertex.frame.color="white", vertex.size=10, edge.width=3, layout =l)
title(day, cex.main=1, col.main="dodgerblue4")
num_cum = unlist(lapply(1:m, function(x) length(infected[[x]]) ))
p_cum = num_cum/size
time = 1:m
plot(p_cum~time, type = "b", ylab = "Proportion Infected", xlab = "Time",
xlim = c(0,i), ylim =c(0,1), frame.plot = FALSE)
m = m + 1}
},
interval = .5, ani.width = 800, ani.height = 500, movie.name="netlogo_animation.gif")
library(ggplot2)
comm1<-read.graph("comm1.net", format="pajek") %>% as.undirected() %>% simplify()
comm1<-delete.vertices(comm1, which(degree(comm1)==0))
plot(comm1)
hist(degree(comm1))
mean(degree(comm1))
ran.net<-sample_degseq(degree(comm1), method="simple.no.multiple")
?sample_degseq
plot(ran.net)
graph.density(comm1)
graph.density(ran.net)
degree(comm1)==degree(ran.net)
Net<-list(comm1)
Net[[2]]<-sample_degseq(degree(comm1), method="simple.no.multiple")
Net[[3]]<-sample_degseq(degree(comm1), method="simple.no.multiple")
Net[[4]]<-sample_degseq(degree(comm1), method="simple.no.multiple")
Net[[5]]<-sample_degseq(degree(comm1), method="simple.no.multiple")
Net[[6]]<-sample_degseq(degree(comm1), method="simple.no.multiple")
size<-vcount(comm1)
update_diffusers = function(diffusers){
nearest_neighbors = unlist(ego(g, 1, diffusers))
nearest_neighbors = setdiff(nearest_neighbors, diffusers)
n=length(nearest_neighbors)
is.infected=sample(c(1,0),n,replace=T, prob=c(p,1-p))
new_infected = nearest_neighbors[which(is.infected==1)]
diffusers = unique(c(diffusers, new_infected))
return(diffusers)
}
record<-data.frame()
seeds<-sample(1:size, 50)
p = 0.2
g<-Net[[1]]
for(t in 1:50){
infected=list()
infected[[1]] = seeds[t]
for(i in 1:40){
if(length(infected[[i]])<size){ infected[[i+1]]="update_diffusers(infected[[i]])" %="">% sort()
p_cum = length(infected[[i]])/size
record<-rbind(record, c(1, t, i, p_cum))
}
else{
infected[[i+1]]<-infected[[i]]
record<-rbind(record, c(1, t, i, 1))
}
}
print(paste("trial:",t))
}
head(record)
for(n in 2:length(Net)){
g<-Net[[n]]
k=n-1
seed<-seeds[(10*k-9):(10*k)]
for(t in 1:10){
infected=list()
infected[[1]] = seed[t]
for(i in 1:40){
if(length(infected[[i]])<size){ infected[[i+1]]="update_diffusers(infected[[i]])" %="">% sort()
p_cum = length(infected[[i]])/size
record<-rbind(record, c(n, t, i, p_cum))
}
else{
infected[[i+1]]<-infected[[i]]
record<-rbind(record, c(n, t, i, 1))
}
}
print(paste("Network:",k,"Trial:",t))
}
}
colnames(record)<-c("net","trial","tick","cum.infected")
sch.rec<-record[which(record$net==1),]
sch.rec<-aggregate(sch.rec$cum.infected,list(sch.rec$tick),mean)
sch.rec$type<-"School Net"
ran.rec<-record[which(record$net!=1),]
ran.rec<-aggregate(ran.rec$cum.infected,list(ran.rec$tick),mean)
ran.rec$type<-"Random Net"
all.rec<-rbind(sch.rec,ran.rec)
ggplot(all.rec, aes(Group.1,x,colour=type))+geom_line()+xlab("Time")+ylab("Proportion Infected (Mean)")
install.packages("EpiModel",dependencies = TRUE)
install.packages("ndtv")
library("EpiModel")
library("ndtv")
N=50
nw <- network.initialize(n = N, directed = FALSE)
formation <- ~edges+concurrent
mean.edge<-1
mean.con<-.22
target.stats<-c(mean.edge*(N/2),mean.con*N)
coef.diss <- dissolution_coefs(dissolution = ~offset(edges), duration = 10)
est <- netest(nw, formation, target.stats, coef.diss, verbose = FALSE)
param <- param.net(inf.prob = 0.8, act.rate = 2, rec.rate=0.01)
init <- init.net(i.num = 2)
control <- control.net(type = "SIS", nsteps = 30, nsims = 1, verbose = FALSE)
sim <- netsim(est, param, init, control)
nw <- get_network(sim)
nw <- color_tea(nw, verbose = FALSE)
slice.par <- list(start = 1, end = 30, interval = 1,
aggregate.dur = 1, rule = "any")
render.par <- list(tween.frames = 10, show.time = FALSE)
plot.par <- list(mar = c(0, 0, 0, 0))
compute.animation(nw, slice.par = slice.par)
render.d3movie(
nw,
render.par = render.par,
plot.par = plot.par,
vertex.cex = 1.5,
vertex.col = "ndtvcol",
edge.col = "darkgrey",
vertex.border = "lightgrey",
displaylabels = FALSE,
)
N=500
nw <- network.initialize(n = N, directed = FALSE)
formation <- ~edges
mean.edge<-0.7
target.stats<-mean.edge*(N/2)
coef.diss <- dissolution_coefs(dissolution = ~offset(edges), duration = 100)
est <- netest(nw, formation, target.stats, coef.diss, verbose = FALSE)
param <- param.net(inf.prob = 0.5, act.rate = 2, rec.rate=0.01)
init <- init.net(i.num = 10)
control <- control.net(type = "SIS", nsteps = 500, nsims = 5, verbose = FALSE)
sim <- netsim(est, param, init, control)
formation.concur <- ~edges+concurrent
mean.con<-.2
target.stats<-c(mean.edge*(N/2),mean.con*N)
est <- netest(nw, formation.concur, target.stats, coef.diss, verbose = FALSE)
sim.concur.1 <- netsim(est, param, init, control)
mean.con<-.25
target.stats<-c(mean.edge*(N/2),mean.con*N)
est <- netest(nw, formation.concur, target.stats, coef.diss, verbose = FALSE)
sim.concur.2 <- netsim(est, param, init, control)
par(mfrow=c(1,3))
plot(sim)
plot(sim.concur.1)
plot(sim.concur.2)
</size){></size){>