################################################################ ############ CODE DISEASE SPREAD vs VACCINATION ################ ################################################################ # Author: Sebastian Napp (CReSA-IRTA) # date: 2018 # Install different packages that we will need install.packages("maps") install.packages("mapdata") install.packages("maptools") install.packages("sp") install.packages("rgdal") install.packages("rgeos") install.packages("dplyr") # load those packages library(maps) library(mapdata) library(maptools) library(sp) library(rgdal) library(rgeos) library(dplyr) # set your working directory # setwd("your wd") # load a map (shapefiles) with a given administrative division from the working directory. # in our case, we used France (administrative division 2, i.e. Departments) obtained from: https://gadm.org/download_country_v3.html FRANCE2=readOGR("FRA_adm2.shp") # Reproject France shapefiles to UTM 28N FRANCE2_28N=spTransform(FRANCE2, CRS("+init=epsg:2192")) ################################################################ # MAKE THE JOIN OF SHAPEFILE AND DATABASE # load the csv file with the data on the number of herds and animals for each of your geographical units # for the sake of the exercise, we set a fixed 500 herds and 5000 cattle per Department census=read.csv("CENSUSDATA.csv", sep=";",header=T) # transform ID2 in shapefile to integer for joining with vaccination data FRANCE2_28N@data$ID_2 = as.numeric(as.character(FRANCE2_28N@data$ID_2)) # Join data from cvs file (with census data) to the shapefile using the code for the administrative divisions FRANCE2_28N@data = left_join(FRANCE2_28N@data, census, by=c("ID_2" = "MAPNUM")) ################################################################ # SIMULATE THE RANDOM DISTRIBUTION OF ALL CATTLE HERDS IN FRANCE # create a list to store the (randomly distributted) farms in the different departments farms=list() # get the number of geographical units in your shapefile ndep=length(FRANCE2_28N$NAME_2) # create matrices to store size and average size of cattle herds in each Department Z1=matrix(ncol=ndep,nrow=1,byrow=T) Z2=matrix(ncol=ndep,nrow=1,byrow=T) # loop to simulate random distribution of herds for(i in 1: ndep){ # generate random farms within a given department (i) farms[[i]]=spsample(FRANCE2_28N[i,],FRANCE2_28N$CATHERDS[i],type="random") # number of herds in department i Z1[i]=FRANCE2_28N$CATHERDS[i] # average number of bovines per farm in department i Z2[i]=FRANCE2_28N$CATHEADS[i]/FRANCE2_28N$CATHERDS[i] # vector with average census for a given department bov=rep(Z2[i], Z1[i]) # vector with department to which the herds belong depto=as.character(FRANCE2_28N$Dep[i]) # generate elements of SpatialPointsDataFrame: df with coordinates and df1 with dataframe df=data.frame(x = farms[[i]]$x, y=farms[[i]]$y) df1=data.frame(bov,depto) mat=as.matrix(df) # generate elements of SpatialPoints sp1=SpatialPoints(coords = mat) # generate elements of SpatialPointsDataFrame farms[[i]]=SpatialPointsDataFrame(sp1, data = df1) } # check cattle herds (in one of the 96 departments) i=43 plot(FRANCE2_28N[i,]) plot(farms[[i]],col="red",cex=0.5,pch=16,add=T) # create a SpatialPointsDataFrame object with all the farms in France by combining all the randomly distributted farms in the different departments FARMS=do.call("rbind", farms) # check their distribution plot(FRANCE2_28N) plot(FARMS,col="red",cex=0.01,add=T) ################################################################ # LOOP TO SIMULATE DISEASE SPREAD # get number of farms in France nfarms=length(FARMS) # select weekly speed of spread of disease, in our case 7.3 Km per week speed=7.3 # list in which each element is a dataframe with all the farms (within a corresponding radius) and their census, given an initial herd and a time for disease spread farmlist=list() # list in which each element is a SpatialPointsDataFrame with all the farms (within a corresponding radius), given an initial herd and a time for disease spread farmlist2=list() # timesteps in disease spread (columns in dataframe => i) # trials or number of farms randomly sampled (rows in dataframe => j) timesteps=5 # i trials=500 # j # matrices of j (trials or number of farms randomly sampled, i.e. rows in dataframe) by i (timesteps in disease spread, i.e. columns in dataframe) dimensions for storing a) number of farms and b) number of animals for the different buffers, respectively. matrix.farms=matrix(ncol=timesteps,nrow=trials,byrow=T) matrix.animals=matrix(ncol=timesteps,nrow=trials,byrow=T) # matrix to store randomly selected farm plus the Department to which it belongs matrix.herd=matrix(ncol=2,nrow=trials,byrow=T) # random farms selected rndF=matrix(ncol=trials,nrow=1,byrow=T) # loop for(j in 1: trials){ # select 1 herd randomly rndF[j]=sample(1:nfarms, 1, replace=T) # store value of random herd matrix.herd[j,1]=rndF[j] matrix.herd[j,2]=as.character(FARMS$depto[rndF[j]]) for(i in 1: timesteps){ # generate buffer around random farm (rndF[i]) for time i BUFF1=gBuffer(FARMS[rndF[j],],width=speed*i*1000) # get a list of the farms within buffers k=((j-1)*timesteps)+i farmlist[[k]]=over(BUFF1,FARMS,returnList=T) farmlist2[[k]]=FARMS[!is.na(over(FARMS,BUFF1)),] # number of farms z1=length(farmlist[[k]]$buffer[ ,1]) matrix.farms[j,i]=z1 # mean number of animals z2=(farmlist[[k]]$buffer[1,1]) z3=z1*z2 matrix.animals[j,i]=round(z3) } } # plot results of a random simulation (e.g. 44) with all the farms within a given radius x=44 plot(FRANCE2_28N) plot(farmlist2[[x]],col="red",cex=0.05,add=T) # get a dataframe with all the herds from that simulation farmlist[[x]] # transform matrix.herd from matrix to dataframe and name columns matrix.herd=as.data.frame(matrix.herd) colnames(matrix.herd)[1]="Initial herd" colnames(matrix.herd)[2]="Initial Department" # save results for herds and for animals RES.HERDS=data.frame(matrix.herd,matrix.farms) RES.ANIMALS=data.frame(matrix.herd,matrix.animals) ################################################################