#read in a dynamic edgelist edges <- read.csv(file.choose(),stringsAsFactors = F) edges$V1 <- as.character(edges$V1) edges$V2 <- as.character(edges$V2) Net.model <- function(seed.val=round(runif(1,1,10000)),beta=.5,inc=3){ set.seed(seed.val) #t <- seq.Date(from=min(e$Date),to=max(e$Date),by=1) t <- seq(from=min(edges$Date),max(edges$Date),by=1) t <- t[1:56] allindivs <- unique(c(edges$V1,edges$V2)) herds <- data.frame(name=as.character(allindivs),state=0) herds$state <- sample(c(rep(0,nrow(herds)-1),1),nrow(herds)) herds$time.I <- NA herds$time.I <- ifelse(herds$state==1,0,NA) #for recording time infected. NA for all except initially infected farm #13-Create a dataframe called track that will keep track of the time that herds are infected #file for keeping track of S and I numbers track <- data.frame(time=min(t),S=c(nrow(herds)-1),I=c(1)) #This (above) is making a data frame (matrix) that records the time and number of susceptible and infected herds by time. S <- nrow(herds)-1 #object to keep track of how numbers are changing each timestep I <- 1 #object to keep track of how numbers are changing each timestep-THIS NEED TO BE CHANGED TO INCORPORATE THE IDENTIFIED (Ic) #AND UNIDENTIFIED INDIVIDUALS (Is) rbinom.v <- function(x){rbinom(1,1,x)}#This function vectorizes the rbinom function so that you can run it on columns of data. a= # of draws (can be set to one), b= probability. Returns the sum number of 1s drawn in sample of size a #rbinom(observations, size, probability); x=prob that farm becomes infected; observations= ;size= timestep <- length(t) for(i in 1:timestep){ #create new row for next timestep with same values as previous (will be updated later if transmission occurs) row <- data.frame(time=t[i],S=S,I=I) track <- rbind(track,row) print(i) ###MOVEMENTS M.o <- nrow(edges[edges$Date==t[i],]) #observed number of movements on time t if(M.o>0){ #skips to below if no movements occur edges.t <- subset(edges,Date==t[i]) #get list of movements for this timestep #bring in state of senders and receivers; State 1 is origin and State 2 is destination edges.t$state1 <- herds$state[match(edges.t$V1,herds$name)]#a column named state 1 that matches origin and farm name edges.t$state2 <- herds$state[match(edges.t$V2,herds$name)] # a column that matched the destination of edge list with farm name edges.t$time.I1 <- herds$time.I[match(edges.t$V1,herds$name)]#a column named state 1 that matches origin and farm name edges.t$time.I2 <- herds$time.I[match(edges.t$V2,herds$name)] # a column that matched the destination of edge list with farm name edges.t <- subset(edges.t, state1 != state2) #now, reorganize so that infected herd is always listed first if(nrow(edges.t)>0){ for(j in 1:nrow(edges.t)){ if(edges.t$state1[j]==0){ row <- data.frame(edges.t$V2[j],edges.t$V1[j],edges.t$Date[j],edges.t$state2[j],edges.t$state1[j],edges.t$time.I2[j],edges.t$time.I1[j]) names(row)<-names(edges.t) edges.t$V1[j] <- as.character(row$V1) edges.t$V2[j] <- as.character(row$V2) edges.t[j,4:7]<- row[4:7] } } edges.t <- subset(edges.t , time.I1 < i - inc) } if(sum(edges.t$state1,na.rm=T)>0){ #skip to below if no infected herds moved any animals #filter edgelist so only includes movements from I --> S herds edges.f <- subset(edges.t,state1==1 & state2==0) if(nrow(edges.f)>0){ #skip to below if no I-->S movements #calculate state transitions edges.f$PROB.I <- 1-beta trans <- data.frame(PROB.I.cum =tapply(edges.f$PROB.I,as.character(edges.f$V2),prod))#multiply all prob together. Probability that none of animals from any of the connected herds are I trans$name <- row.names(trans) trans$m <- 1-trans$PROB.I.cum #Probability that at least one E or I was moved #get transition trans$trans <- sapply(trans$m,rbinom.v) #if transmission occurs, update state in herds trans.names <- trans$name[trans$trans==1] #newly infected IDs herds$state <- ifelse(herds$name %in% trans.names,1,herds$state) herds$time.I <- ifelse(herds$name %in% trans.names,i,herds$time.I) if(nrow(subset(trans,trans>0))>0){#update farm counts S <- S - sum(trans$trans) #decrease S count by one farm I <- I + sum(trans$trans) #increase I count by one farm } }} } #Update counts of tracker track$S[i+1] <- S track$I[i+1] <- I } return(list(herds,track)) }