# ANALYSIS OF OUTBREAK DATA # Load required packages library(bbmle) library(plotrix) ########## IMPORT THE DATA AS CSV FILES # Below is the code that reads in the data from the Excel spreadsheet and prepares it for the analysis # Each page of the Excel file is saved as a csv file which is then uploaded into R. # YOU WILL NEED TO EDIT THIS TO SPECIFY THE PATH TO YOUR CSV FILES StaphA<-read.csv("/Users/amyhurford/Desktop/Work/Students/Ugrad/Alice/StaphA.csv") Enteroc<-read.csv("/Users/amyhurford/Desktop/Work/Students/Ugrad/Alice/Enterococci.csv") Pseudo<-read.csv("/Users/amyhurford/Desktop/Work/Students/Ugrad/Alice/Pseudo.csv") Klebs<-read.csv("/Users/amyhurford/Desktop/Work/Students/Ugrad/Alice/Klebs.csv") Acin<-read.csv("/Users/amyhurford/Desktop/Work/Students/Ugrad/Alice/Acin.csv") Serr<-read.csv("/Users/amyhurford/Desktop/Work/Students/Ugrad/Alice/Serr.csv") Enterob<-read.csv("/Users/amyhurford/Desktop/Work/Students/Ugrad/Alice/Enterobacter.csv") HepB<-read.csv("/Users/amyhurford/Desktop/Work/Students/Ugrad/Alice/HepB.csv") HepC<-read.csv("/Users/amyhurford/Desktop/Work/Students/Ugrad/Alice/HepC.csv") Strep<-read.csv("/Users/amyhurford/Desktop/Work/Students/Ugrad/Alice/Strep.csv") Salmon<-read.csv("//Users/amyhurford/Desktop/Work/Students/Ugrad/Alice/Salmonella.csv") Legion<-read.csv("/Users/amyhurford/Desktop/Work/Students/Ugrad/Alice/Legionella.csv") Ecoli<-read.csv("/Users/amyhurford/Desktop/Work/Students/Ugrad/Alice/Ecoli.csv") Cdiff<-read.csv("/Users/amyhurford/Desktop/Work/Students/Ugrad/Alice/Cdiff.csv") ########## PRE-PROCESSING: # below is code that takes the information from the Excel spreadsheet and converts it into a nice dataframe that # can be used for the analysis. # Need to create matrix with columns: 1) species; 2) duration; 3) outbreak size; 4) Molecular evidence and 5) tranmission route. labs<-labels(StaphA)[[2]] j2<-which(labs=="Duration..days.") j3<-which(labs=="Cases..patient.") j4<-which(labs=="Molecular.confirmation") j5<-which(labs=="Transmission") Mx <- subset(StaphA[1:268,], select = c(j2, j3, j4, j5)) Mx$species <- rep("Staph",268) # Construct data matrix for Enterococci and add to the bottom of Staph A data matrix labs<-labels(Enteroc)[[2]] j2<-which(labs=="Duration..days.") j3<-which(labs=="Cases..patient.") j4<-which(labs=="Molecular.Evidence") j5<-which(labs=="Transmission") Mx2 <- subset(Enteroc[1:116,], select = c(j2, j3, j4, j5)) Mx2$species <- rep("Enteroc",116) names(Mx2)<-labels(Mx)[[2]] Mx<-rbind(Mx,Mx2) # Construct data matrix for Pseudomonas and add to the bottom of the existing matrix labs<-labels(Pseudo)[[2]] j2<-which(labs=="Duration..days.") j3<-which(labs=="Cases..patient.") j4<-which(labs=="Molecular.Evidence") j5<-which(labs=="Transmission") Mx2 <- subset(Pseudo[1:144,], select = c(j2, j3, j4, j5)) Mx2$species <- rep("Pseudo",144) names(Mx2)<-labels(Mx)[[2]] Mx<-rbind(Mx,Mx2) # Construct data matrix for Klebsilla and add labs<-labels(Klebs)[[2]] j2<-which(labs=="Duration..days.") j3<-which(labs=="Cases..patient.") j4<-which(labs=="Molecular.Evidence") j5<-which(labs=="Transmission") Mx2 <- subset(Klebs[1:79,], select = c(j2, j3, j4, j5)) Mx2$species<-rep("Klebs",79) names(Mx2)<-labels(Mx)[[2]] Mx<-rbind(Mx,Mx2) # Construct data matrix for Acinetobacter and add labs<-labels(Acin)[[2]] j2<-which(labs=="Duration..days.") j3<-which(labs=="Cases..patient.") j4<-which(labs=="Molecular.Evidence") j5<-which(labs=="Transmission") Mx2 <- subset(Acin[1:169,], select = c(j2, j3, j4, j5)) Mx2$Cspecies<-rep("Acin",169) names(Mx2)<-labels(Mx)[[2]] Mx<-rbind(Mx,Mx2) # Construct data matrix for Serratia and add labs<-labels(Serr)[[2]] j2<-which(labs=="Duration..days.") j3<-which(labs=="Cases..patient.") j4<-which(labs=="Molecular.Evidence") j5<-which(labs=="Transmission") Mx2 <- subset(Serr[1:116,], select = c(j2, j3, j4, j5)) Mx2$species<-rep("Serr",116) names(Mx2)<-labels(Mx)[[2]] Mx<-rbind(Mx,Mx2) # Construct data matrix for Enterobacter and add labs<-labels(Enterob)[[2]] j2<-which(labs=="Duration..days.") j3<-which(labs=="Cases..patient.") j4<-which(labs=="Molecular.Evidence") j5<-which(labs=="Transmission") Mx2 <- subset(Enterob[1:62,], select = c(j2, j3, j4, j5)) Mx2$species<-rep("Enterob",62) names(Mx2)<-labels(Mx)[[2]] Mx<-rbind(Mx,Mx2) # Construct data matrix for Hepatitis B Virus and add labs<-labels(HepB)[[2]] j2<-which(labs=="Duration..days.") j3<-which(labs=="Cases..patient.") j4<-which(labs=="Molecular.Evidence") j5<-which(labs=="Transmission") Mx2 <- subset(HepB[1:85,], select = c(j2, j3, j4, j5)) Mx2$species<-rep("Hep B",85) names(Mx2)<-labels(Mx)[[2]] Mx<-rbind(Mx,Mx2) # Construct data matrix for Hepatitis C Virus and add labs<-labels(HepC)[[2]] j2<-which(labs=="Duration..days.") j3<-which(labs=="Cases..patient.") j4<-which(labs=="Molecular.Evidence") j5<-which(labs=="Transmission") Mx2 <- subset(HepC[1:85,], select = c(j2, j3, j4, j5)) Mx2$species<-rep("Hep C",85) names(Mx2)<-labels(Mx)[[2]] Mx<-rbind(Mx,Mx2) # Construct data matrix for Streptococci and add labs<-labels(Strep)[[2]] j2<-which(labs=="Duration..days.") j3<-which(labs=="Cases..patient.") j4<-which(labs=="Molecular.Evidence") j5<-which(labs=="Transmission") Mx2 <- subset(Strep[1:87,], select = c(j2, j3, j4, j5)) Mx2$species<-rep("Strep",87) names(Mx2)<-labels(Mx)[[2]] Mx<-rbind(Mx,Mx2) # Construct data matrix for Salmonella and add labs<-labels(Salmon)[[2]] j2<-which(labs=="Duration..days.") j3<-which(labs=="Cases..patient.") j4<-which(labs=="Molecular.Evidence") j5<-which(labs=="Transmission") Mx2 <- subset(Salmon[1:78,], select = c(j2, j3, j4, j5)) Mx2$species<-rep("Salm",78) names(Mx2)<-labels(Mx)[[2]] Mx<-rbind(Mx,Mx2) # Construct data matrix for Legionella and add labs<-labels(Legion)[[2]] j2<-which(labs=="Duration..days.") j3<-which(labs=="Cases..patient.") j4<-which(labs=="Molecular.Evidence") j5<-which(labs=="Transmission") Mx2 <- subset(Legion[1:55,], select = c(j2, j3, j4, j5)) Mx2$species<-rep("Legion",55) names(Mx2)<-labels(Mx)[[2]] Mx<-rbind(Mx,Mx2) # Construct data matrix for Ecoli and add labs<-labels(Ecoli)[[2]] j2<-which(labs=="Duration..days.") j3<-which(labs=="Cases..patient.") j4<-which(labs=="Molecular.Evidence") j5<-which(labs=="Transmission") Mx2 <- subset(Ecoli[1:39,], select = c(j2, j3, j4, j5)) Mx2$species<-rep("E. coli",39) names(Mx2)<-labels(Mx)[[2]] Mx<-rbind(Mx,Mx2) # Construct data matrix for Clostrium difficule and add labs<-labels(Cdiff)[[2]] j2<-which(labs=="Duration..days.") j3<-which(labs=="Cases..patient.") j4<-which(labs=="Molecular.Evidence") j5<-which(labs=="Transmission") Mx2 <- subset(Cdiff[1:50,], select = c(j2, j3, j4, j5)) Mx2$species<-rep("C. diff",50) names(Mx2)<-labels(Mx)[[2]] Mx<-rbind(Mx,Mx2) # Remove entries that are NA # Omit entries that read in as NA Mx<-na.omit(Mx) # Remove detials on attributes attributes(Mx)$na.action <- NULL # Group like transmission routes: # (1) Multiple transmission routes are grouped together #add a new level levels(Mx$Transmission)<-c(levels(Mx$Transmission),"multi") # (2) Spelling mistakes are corrected # (3) Biologics and drugs are aggregated within equipment. Mx$Transmission[Mx$Transmission=="HCW+env"]<-"multi" Mx$Transmission[Mx$Transmission=="HCW+air"]<-"multi" Mx$Transmission[Mx$Transmission=="HCW+env+equip"]<-"multi" Mx$Transmission[Mx$Transmission=="env+equip"]<-"multi" Mx$Transmission[Mx$Transmission=="HCW+equip"]<-"multi" Mx$Transmission[Mx$Transmission=="equip+air"]<-"multi" Mx$Transmission[Mx$Transmission=="HCW+equip+env"]<-"multi" Mx$Transmission[Mx$Transmission=="drug"]<-"equip" Mx$Transmission[Mx$Transmission=="HCw+env"]<-"multi" Mx$Transmission[Mx$Transmission=="HCW+equip "]<-"multi" Mx$Transmission[Mx$Transmission=="equip+env"]<-"multi" Mx$Transmission[Mx$Transmission=="HCW "]<-"HCW" Mx$Transmission[Mx$Transmission=="HCW+PTP"]<-"multi" Mx$Transmission[Mx$Transmission=="equip+air"]<-"multi" Mx$Transmission[Mx$Transmission=="HCW+equip+env"]<-"multi" Mx$Transmission[Mx$Transmission=="env "]<-"env" Mx$Transmission[Mx$Transmission=="PTP+env"]<-"multi" Mx$Transmission[Mx$Transmission=="HCW+env+air"]<-"multi" Mx$Transmission[Mx$Transmission=="equip+food"]<-"multi" Mx$Transmission[Mx$Transmission=="HCW+drug"]<-"multi" Mx$Transmission[Mx$Transmission=="HCW+equip+food"]<-"multi" Mx$Transmission[Mx$Transmission=="PTP+equip"]<-"multi" Mx$Transmission[Mx$Transmission=="food+equip"]<-"multi" Mx$Transmission[Mx$Transmission=="food+HCW"]<-"multi" Mx$Transmission[Mx$Transmission=="HCW+food"]<-"multi" Mx$Transmission[Mx$Transmission=="unk "]<-"unk" Mx$Transmission[Mx$Transmission=="PTP+food"]<-"multi" Mx$Transmission[Mx$Transmission=="equip+drug"]<-"multi" Mx$Transmission[Mx$Transmission=="bio"]<-"equip" # Remove unused levels Mx$Transmission<-factor(Mx$Transmission) Mx$Molecular.confirmation<-factor(Mx$Molecular.confirmation) # The data matrix for all species MxTot<-Mx # Remove all the instances where the transmission routes was unknown from MxTot Mx<-MxTot[-1*which(MxTot$Transmission == "unk"),] Mx$Transmission<-factor(Mx$Transmission) # Now all the data is organized in the dataframe Mx. # Also we create seperate dataframe for each species. These are used to # make boxplots. MxAcin = Mx[Mx$species=="Acin",] MxAcin$Transmission<-factor(MxAcin$Transmission) MxCdiff = Mx[Mx$species=="C. diff",] MxCdiff$Transmission<-factor(MxCdiff$Transmission) MxEcoli = Mx[Mx$species=="E. coli",] MxEcoli$Transmission<-factor(MxEcoli$Transmission) MxEnterob = Mx[Mx$species=="Enterob",] MxEnterob$Transmission<-factor(MxEnterob$Transmission) MxEnteroc = Mx[Mx$species=="Enteroc",] MxEnteroc$Transmission<-factor(MxEnteroc$Transmission) MxHepB = Mx[Mx$species=="Hep B",] MxHepB$Transmission<-factor(MxHepB$Transmission) MxHepC = Mx[Mx$species=="Hep C",] MxHepC$Transmission<-factor(MxHepC$Transmission) MxKlebs = Mx[Mx$species=="Klebs",] MxKlebs$Transmission<-factor(MxKlebs$Transmission) MxLegion = Mx[Mx$species=="Legion",] MxLegion$Transmission<-factor(MxLegion$Transmission) MxPseudo = Mx[Mx$species=="Pseudo",] MxPseudo$Transmission<-factor(MxPseudo$Transmission) MxSalm = Mx[Mx$species=="Salm",] MxSalm$Transmission<-factor(MxSalm$Transmission) MxSerr = Mx[Mx$species=="Serr",] MxSerr$Transmission<-factor(MxSerr$Transmission) MxStaph = Mx[Mx$species=="Staph",] MxStaph$Transmission<-factor(MxStaph$Transmission) MxStrep = Mx[Mx$species=="Strep",] MxStrep$Transmission<-factor(MxStrep$Transmission) #################### DATA ANALYSIS # Response variable: log Cases # Model 1: Constant only # Model 2: Transmission route is the predictor variable # Model 3: Species is the predictor variable # Model 4: Transmission routes x species interaction is the predictor variable # Response variable: log Rate # Model 5: Constant only # Model 6: Transmission route is the predictor variable # Model 7: Species is the predictor variable # Model 8: Transmission routes x species interaction is the predictor variable # The response variable: logCases=log(Mx$Cases) data = logCases ##### Model 1: Constant only y1 = function(a){ mu = a mu[mu<0]=0 return(mu) } #The function for the negative log likelihood LL1 = function (a,y=data){ mu = y1(a=a) #get the total negative log likelihood loglike= -sum(dnorm(y,mu,log=TRUE)) return(loglike) } #The call to bbmle and results summary: Cases_Constant = mle2(minuslogl = LL1, start = list(a=2)) Cases_Constant_Summary = summary(Cases_Constant) ##### Model 2: Transmission route as the predictor variable # Build a matrix with columns: air, env, equip, HCW, insect, PTP, bio, food, and multi # and with columns equal to each of the outbreaks. An entry of 1 means that a particular # outbreak did occur from that transmission route. xMx = array(0,c(length(Mx$Transmission),length(levels(Mx$Transmission)))) xMx[,1]<-(Mx$Transmission=="air")*1 xMx[,2]<-(Mx$Transmission=="env")*1 xMx[,3]<-(Mx$Transmission=="equip")*1 xMx[,4]<-(Mx$Transmission=="food")*1 xMx[,5]<-(Mx$Transmission=="HCW")*1 xMx[,6]<-(Mx$Transmission=="insect")*1 xMx[,7]<-(Mx$Transmission=="PTP")*1 xMx[,8]<-(Mx$Transmission=="multi")*1 x1=xMx[,1] x2=xMx[,2] x3=xMx[,3] x4=xMx[,4] x5=xMx[,5] x6=xMx[,6] x7=xMx[,7] x8=xMx[,8] #function for model: the reference category is air. y2 = function(b1,b2,b3,b4,b5,b6,b7,b8){ mu = b1*x1+ b2*x2 + b3*x3 + b4*x4 + b5*x5 + b6*x6 + b7*x7 + b8*x8 mu[mu<0]=0 return(mu) } #The function for the negative log likelihood LL2 = function (b1,b2,b3,b4,b5,b6,b7,b8,y=data){ mu = y2(b1=b1,b2=b2,b3=b3,b4=b4,b5=b5,b6=b6,b7=b7,b8=b8) #get the total negative log likelihood loglike= -sum(dnorm(y,mu,log=TRUE)) return(loglike) } #The call to bbmle and results summary: Cases_TR = mle2(minuslogl = LL2, start = list(b1=2,b2=2,b3=2,b4=2,b5=2,b6=2,b7=2,b8=2), control = list(maxit = 5000)) Cases_TR_Summary = summary(Cases_TR) # The residuals resCase_TR = y2(coef(Cases_TR_Summary)[1,1], coef(Cases_TR_Summary)[2,1], coef(Cases_TR_Summary)[3,1], coef(Cases_TR_Summary)[4,1], coef(Cases_TR_Summary)[5,1],coef(Cases_TR_Summary)[6,1], coef(Cases_TR_Summary)[7,1], coef(Cases_TR_Summary)[8,1])-data ##### Model 3: Species as the predictor variable # Build a matrix with 14 columns for each different species # and with columns equal to each of the outbreaks. An entry of 1 means that a particular # outbreak did occur from that transmission route. zMx = array(0,c(length(Mx$species),14)) zMx[,1]<-(Mx$species=="Acin")*1 zMx[,2]<-(Mx$species=="C. diff")*1 zMx[,3]<-(Mx$species=="Enterob")*1 zMx[,4]<-(Mx$species=="Enteroc")*1 zMx[,5]<-(Mx$species=="E. coli")*1 zMx[,6]<-(Mx$species=="Hep B")*1 zMx[,7]<-(Mx$species=="Hep C")*1 zMx[,8]<-(Mx$species=="Klebs")*1 zMx[,9]<-(Mx$species=="Legion")*1 zMx[,10]<-(Mx$species=="Pseudo")*1 zMx[,11]<-(Mx$species=="Salm")*1 zMx[,12]<-(Mx$species=="Serr")*1 zMx[,13]<-(Mx$species=="Staph")*1 zMx[,14]<-(Mx$species=="Strep")*1 x1=zMx[,1] x2=zMx[,2] x3=zMx[,3] x4=zMx[,4] x5=zMx[,5] x6=zMx[,6] x7=zMx[,7] x8=zMx[,8] x9=zMx[,9] x10=zMx[,10] x11=zMx[,11] x12=zMx[,12] x13=zMx[,13] x14=zMx[,14] #function for model y3 = function(b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14){ mu = b1*x1 + b2*x2 + b3*x3 + b4*x4 + b5*x5 + b6*x6 + b7*x7 + b8*x8 + b9*x9+ b10*x10 + b11*x11 + b12*x12 + b13*x13 + b14*x14 mu[mu<0]=0 return(mu) } #The function for the negative log likelihood LL3 = function (b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,y=data){ mu = y3(b1=b1,b2=b2,b3=b3,b4=b4,b5=b5,b6=b6,b7=b7,b8=b8,b9=b9,b10=b10,b11=b11,b12=b12,b13=b13,b14=b14) #get the total negative log likelihood loglike= -sum(dnorm(y,mu,log=TRUE)) return(loglike) } #The call to bbmle and results summary: Cases_Species = mle2(minuslogl = LL3, start = list(b1=2,b2=2,b3=2,b4=2,b5=2,b6=2,b7=2,b8=2,b9=2,b10=2,b11=2,b12=2,b13=2,b14=2), control = list(maxit = 5000)) Cases_Species_Summary = summary(Cases_Species) #The residuals resCase_Species = y3(coef(Cases_Species_Summary)[1,1], coef(Cases_Species_Summary)[2,1], coef(Cases_Species_Summary)[3,1], coef(Cases_Species_Summary)[4,1], coef(Cases_Species_Summary)[5,1], coef(Cases_Species_Summary)[6,1], coef(Cases_Species_Summary)[7,1], coef(Cases_Species_Summary)[8,1], coef(Cases_Species_Summary)[9,1], coef(Cases_Species_Summary)[10,1], coef(Cases_Species_Summary)[11,1], coef(Cases_Species_Summary)[12,1], coef(Cases_Species_Summary)[13,1], coef(Cases_Species_Summary)[14,1])-data #Model 4: Interaction model. #First note that not all model interactions occur: levels(factor(Mx$Transmission[which(Mx$species=="Staph")])) #b11: Staph-air; b12: Staph-env; b13: Staph-equip; b14: Staph-HCW; b15: Staph-multi levels(factor(Mx$Transmission[which(Mx$species=="Enteroc")])) #b21: Enteroc-env; b22: Enteroc-equip; b23: Enteroc-HCW; b24: Enteroc-multi levels(factor(Mx$Transmission[which(Mx$species=="Pseudo")])) #b31: Pseudo-env; b32: Pseudo-equip; b33: Pseudo-HCW; b34: Pseudo-multi levels(factor(Mx$Transmission[which(Mx$species=="Klebs")])) # Klebs x : b41: env; b42: equip; b43: food; b44 HCW; b45 insect; b46 multi. levels(factor(Mx$Transmission[which(Mx$species=="Acin")])) # Acin x : b51 env; b52 equip b53 HCW b54 PTP b55 multi levels(factor(Mx$Transmission[which(Mx$species=="Serr")])) # Serr x : b61 air; b62 env; b63 equip; b64 HCW; b65 multi levels(factor(Mx$Transmission[which(Mx$species=="Enterob")])) # Enterob x : b71 env; b72 equip; b73 food; b74 HCW; b75 PTP; b76 multi levels(factor(Mx$Transmission[which(Mx$species=="Hep B")])) # Hep B x : b81 equip; b82 HCW; b83 PTP; b84 multi levels(factor(Mx$Transmission[which(Mx$species=="Hep C")])) # Hep C x : b91 env; b92 equip; b93 HCW; b94 PTP; b95 multi levels(factor(Mx$Transmission[which(Mx$species=="Strep")])) # Strep x : b101 air; b102 env; b103 equip; b104 food; b105 HCW; b106 PTP; b107 multi levels(factor(Mx$Transmission[which(Mx$species=="Salm")])) # Salm x : b111 env; b112 equip; b113 food; b114 HCW; b115 PTP; b116 multi levels(factor(Mx$Transmission[which(Mx$species=="Legion")])) # Legion x : b121 env; b122 multi levels(factor(Mx$Transmission[which(Mx$species=="E. coli")])) # E. coli x: b131 env; b132 HCW; b133 food; b134 PTP; b135 multi levels(factor(Mx$Transmission[which(Mx$species=="C. diff")])) # C. diff x: b141 env; b142 equip; b143 HCW; b144 PTP; b145 multi x11=(Mx$species=="Staph" & Mx$Transmission == "air")*1 x12=(Mx$species=="Staph" & Mx$Transmission == "env")*1 x13=(Mx$species=="Staph" & Mx$Transmission == "equip")*1 x14=(Mx$species=="Staph" & Mx$Transmission == "HCW")*1; x15=(Mx$species=="Staph" & Mx$Transmission == "multi")*1; x21=(Mx$species=="Enteroc" & Mx$Transmission == "env")*1; x22=(Mx$species=="Enteroc" & Mx$Transmission == "equip")*1; x23=(Mx$species=="Enteroc" & Mx$Transmission == "HCW")*1; x24=(Mx$species=="Enteroc" & Mx$Transmission == "multi")*1; x31=(Mx$species=="Pseudo" & Mx$Transmission == "env")*1; x32=(Mx$species=="Pseudo" & Mx$Transmission == "equip")*1; x33=(Mx$species=="Pseudo" & Mx$Transmission == "HCW")*1; x34=(Mx$species=="Pseudo" & Mx$Transmission == "multi")*1; x41=(Mx$species=="Klebs" & Mx$Transmission == "env")*1; x42=(Mx$species=="Klebs" & Mx$Transmission == "equip")*1; x43=(Mx$species=="Klebs" & Mx$Transmission == "food")*1; x44=(Mx$species=="Klebs" & Mx$Transmission == "HCW")*1; x45=(Mx$species=="Klebs" & Mx$Transmission == "insect")*1; x46=(Mx$species=="Klebs" & Mx$Transmission == "multi")*1; x51=(Mx$species=="Acin" & Mx$Transmission == "env")*1; x52=(Mx$species=="Acin" & Mx$Transmission == "equip")*1; x53=(Mx$species=="Acin" & Mx$Transmission == "HCW")*1; x54=(Mx$species=="Acin" & Mx$Transmission == "PTP")*1; x55=(Mx$species=="Acin" & Mx$Transmission == "multi")*1; x61=(Mx$species=="Serr" & Mx$Transmission == "air")*1; x62=(Mx$species=="Serr" & Mx$Transmission == "env")*1; x63=(Mx$species=="Serr" & Mx$Transmission == "equip")*1; x64=(Mx$species=="Serr" & Mx$Transmission == "HCW")*1; x65=(Mx$species=="Serr" & Mx$Transmission == "multi")*1; x71=(Mx$species=="Enterob" & Mx$Transmission == "env")*1; x72=(Mx$species=="Enterob" & Mx$Transmission == "equip")*1; x73=(Mx$species=="Enterob" & Mx$Transmission == "food")*1; x74=(Mx$species=="Enterob" & Mx$Transmission == "HCW")*1; x75=(Mx$species=="Enterob" & Mx$Transmission == "PTP")*1; x76=(Mx$species=="Enterob" & Mx$Transmission == "multi")*1; x81=(Mx$species=="Hep B" & Mx$Transmission == "equip")*1; x82=(Mx$species=="Hep B" & Mx$Transmission == "HCW")*1; x83=(Mx$species=="Hep B" & Mx$Transmission == "PTP")*1; x84=(Mx$species=="Hep B" & Mx$Transmission == "multi")*1; x91=(Mx$species=="Hep C" & Mx$Transmission == "env")*1; x92=(Mx$species=="Hep C" & Mx$Transmission == "equip")*1; x93=(Mx$species=="Hep C" & Mx$Transmission == "HCW")*1; x94=(Mx$species=="Hep C" & Mx$Transmission == "PTP")*1; x95=(Mx$species=="Hep C" & Mx$Transmission == "multi")*1; x101=(Mx$species=="Strep" & Mx$Transmission == "air")*1; x102=(Mx$species=="Strep" & Mx$Transmission == "env")*1; x103=(Mx$species=="Strep" & Mx$Transmission == "equip")*1; x104=(Mx$species=="Strep" & Mx$Transmission == "food")*1; x105=(Mx$species=="Strep" & Mx$Transmission == "HCW")*1; x106=(Mx$species=="Strep" & Mx$Transmission == "PTP")*1; x107=(Mx$species=="Strep" & Mx$Transmission == "multi")*1; x111=(Mx$species=="Salm" & Mx$Transmission == "env")*1; x112=(Mx$species=="Salm" & Mx$Transmission == "equip")*1; x113=(Mx$species=="Salm" & Mx$Transmission == "food")*1; x114=(Mx$species=="Salm" & Mx$Transmission == "HCW")*1; x115=(Mx$species=="Salm" & Mx$Transmission == "PTP")*1; x116=(Mx$species=="Salm" & Mx$Transmission == "multi")*1; x121=(Mx$species=="Legion" & Mx$Transmission == "env")*1; x122=(Mx$species=="Legion" & Mx$Transmission == "multi")*1; x131=(Mx$species=="E. coli" & Mx$Transmission == "env")*1; x132=(Mx$species=="E. coli" & Mx$Transmission == "food")*1; x133=(Mx$species=="E. coli" & Mx$Transmission == "HCW")*1; x134=(Mx$species=="E. coli" & Mx$Transmission == "PTP")*1; x135=(Mx$species=="E. coli" & Mx$Transmission == "multi")*1; x141=(Mx$species=="C. diff" & Mx$Transmission == "env")*1; x142=(Mx$species=="C. diff" & Mx$Transmission == "equip")*1; x143=(Mx$species=="C. diff" & Mx$Transmission == "HCW")*1; x144=(Mx$species=="C. diff" & Mx$Transmission == "PTP")*1; x145=(Mx$species=="C. diff" & Mx$Transmission == "multi")*1; y4 = function(b11, b12, b13, b14, b15, b21, b22, b23, b24, b31,b32,b33,b34,b41,b42,b43,b44,b45,b46,b51,b52,b53, b54, b55, b61, b62, b63, b64, b65, b71, b72, b73, b74, b75, b76, b81, b82, b83, b84, b91, b92, b93, b94, b95, b101, b102, b103, b104, b105, b106, b107, b111, b112, b113, b114, b115, b116, b121, b122, b131, b132, b133, b134, b135, b141, b142, b143, b144, b145){ mu = b11*x11+ b12*x12 + b13*x13 + b14*x14 + b15*x15+b21*x21 + b22*x22 + b23*x23 + b24*x24 + b31*x31 + b32*x32 + b33*x33 + b34*x34+ b41*x41 + b42*x42 + b43*x43 + b44*x44 + b45*x45 + b46*x46 + b51*x51 + b52*x52 + b53*x53 + b54*x54 + b55*x55+ b61*x61 + b62*x62 + b63*x63 + b64*x64 + b65*x65+ b71*x71 + b72*x72 + b73*x73 + b74*x74 + b75*x75 + b76*x76+ b81*x81 + b82*x82 + b83*x83 + b84*x84+ b91*x91 + b92*x92 + b93*x93 + b94*x94 + b95*x95+ b101*x101 + b102*x102 + b103*x103 + b104*x104 + b105*x105 + b106*x106 + b107*x107+ b111*x111 + b112*x112+ b113*x113 + b114*x114 + b115*x115 + b116*x116+ b121*x121 + b112*x122+ b131*x131 + b132*x132 + b133*x133 + b134*x134 + b135*x135+ b141*x141 + b142*x142 + b143*x143 + b144*x144+b145*x145 mu[mu<0]=0 return(mu) } #The function for the negative log likelihood LL4 = function (b11,b12,b13,b14,b15,b21,b22,b23,b24,b31,b32,b33,b34,b41,b42,b43,b44,b45,b46,b51,b52,b53,b54,b55,b61,b62,b63,b64,b65,b71,b72,b73,b74,b75,b76,b81,b82,b83,b84,b91,b92,b93,b94,b95,b101,b102,b103,b104,b105,b106,b107,b111,b112,b113,b114,b115,b116,b121,b122,b131,b132,b133,b134,b135,b141,b142,b143,b144,b145,y=data){ mu = y4(b11=b11,b12=b12,b13=b13,b14=b14,b15=b15,b21=b21,b22=b22,b23=b23,b24=b24,b31=b31,b32=b32,b33=b33,b34=b34,b41=b41,b42=b42,b43=b43,b44=b44,b45=b45,b46=b46,b51=b51,b52=b52,b53=b53,b54=b54,b55=b55,b61=b61,b62=b62,b63=b63,b64=b64,b65=b65,b71=b71,b72=b72,b73=b73,b74=b74,b75=b75,b76=b76,b81=b81,b82=b82,b83=b83,b84=b84,b91=b91,b92=b92,b93=b93,b94=b94,b95=b95,b101=b101,b102=b102,b103=b103,b104=b104,b105=b105,b106=b106,b107=b107,b111=b111,b112=b112,b113=b113,b114=b114,b115=b115,b116=b116,b121=b121,b122=b122,b131=b131,b132=b132,b133=b133,b134=b134,b135=b135,b141=b141,b142=b142,b143=b143,b144=b144,b145=b145) #get the total negative log likelihood loglike= -sum(dnorm(y,mu,log=TRUE)) return(loglike) } Cases_Int = mle2(minuslogl = LL4, start = list(b11=2,b12=2,b13=2,b14=2,b15=2,b21=2,b22=2,b23=2,b24=2,b31=2,b32=2,b33=2,b34=2,b41=2,b42=2,b43=2,b44=2,b45=2,b46=2,b51=2,b52=2,b53=2,b54=2,b55=2,b61=2,b62=2,b63=2,b64=2,b65=2,b71=2,b72=2,b73=2,b74=2,b75=2,b76=2,b81=2,b82=2,b83=2,b84=2,b91=2,b92=2,b93=2,b94=2,b95=2,b101=2,b102=2,b103=2,b104=2,b105=2,b106=2,b107=2,b111=2,b112=2,b113=2,b114=2,b115=2,b116=2,b121=2,b122=2,b131=2,b132=2,b133=2,b134=2,b135=2,b141=2,b142=2,b143=2,b144=2,b145=2), control = list(maxit = 5000)) Cases_Int_Summary = summary(Cases_Int) #The residuals resCase_Int = y4(coef(Cases_Int_Summary)[1,1], coef(Cases_Int_Summary)[2,1], coef(Cases_Int_Summary)[3,1], coef(Cases_Int_Summary)[4,1], coef(Cases_Int_Summary)[5,1], coef(Cases_Int_Summary)[6,1], coef(Cases_Int_Summary)[7,1], coef(Cases_Int_Summary)[8,1], coef(Cases_Int_Summary)[9,1], coef(Cases_Int_Summary)[10,1], coef(Cases_Int_Summary)[11,1], coef(Cases_Int_Summary)[12,1], coef(Cases_Int_Summary)[13,1], coef(Cases_Int_Summary)[14,1],coef(Cases_Int_Summary)[15,1], coef(Cases_Int_Summary)[16,1], coef(Cases_Int_Summary)[17,1], coef(Cases_Int_Summary)[18,1], coef(Cases_Int_Summary)[19,1], coef(Cases_Int_Summary)[20,1], coef(Cases_Int_Summary)[21,1], coef(Cases_Int_Summary)[22,1], coef(Cases_Int_Summary)[23,1], coef(Cases_Int_Summary)[24,1], coef(Cases_Int_Summary)[25,1], coef(Cases_Int_Summary)[26,1], coef(Cases_Int_Summary)[27,1], coef(Cases_Int_Summary)[28,1],coef(Cases_Int_Summary)[29,1], coef(Cases_Int_Summary)[30,1], coef(Cases_Int_Summary)[31,1], coef(Cases_Int_Summary)[32,1], coef(Cases_Int_Summary)[33,1], coef(Cases_Int_Summary)[34,1], coef(Cases_Int_Summary)[35,1], coef(Cases_Int_Summary)[36,1], coef(Cases_Int_Summary)[37,1], coef(Cases_Int_Summary)[38,1], coef(Cases_Int_Summary)[39,1], coef(Cases_Int_Summary)[40,1], coef(Cases_Int_Summary)[41,1], coef(Cases_Int_Summary)[42,1], coef(Cases_Int_Summary)[43,1], coef(Cases_Int_Summary)[44,1], coef(Cases_Int_Summary)[45,1], coef(Cases_Int_Summary)[46,1], coef(Cases_Int_Summary)[47,1], coef(Cases_Int_Summary)[48,1], coef(Cases_Int_Summary)[49,1], coef(Cases_Int_Summary)[50,1], coef(Cases_Int_Summary)[51,1], coef(Cases_Int_Summary)[52,1], coef(Cases_Int_Summary)[53,1], coef(Cases_Int_Summary)[54,1], coef(Cases_Int_Summary)[55,1], coef(Cases_Int_Summary)[56,1],coef(Cases_Int_Summary)[57,1], coef(Cases_Int_Summary)[58,1], coef(Cases_Int_Summary)[59,1], coef(Cases_Int_Summary)[60,1], coef(Cases_Int_Summary)[61,1], coef(Cases_Int_Summary)[62,1], coef(Cases_Int_Summary)[63,1], coef(Cases_Int_Summary)[64,1], coef(Cases_Int_Summary)[65,1], coef(Cases_Int_Summary)[66,1], coef(Cases_Int_Summary)[67,1], coef(Cases_Int_Summary)[68,1], coef(Cases_Int_Summary)[69,1])-data ########################### # Response variable: log Rate # Model 5: Constant only # Model 6: Transmission route is the predictor variable # Model 7: Species is the predictor variable # Model 8: Transmission routes x species interaction is the predictor variable # Change the response variable to the log case rate: logRate=log(Mx$Cases/Mx$Duration) data = logRate ###### Model 5: Constant only model y1 = function(a){ mu = a return(mu) } #The function for the negative log likelihood LL1 = function (a,y=data){ mu = y1(a=a) #get the total negative log likelihood loglike= -sum(dnorm(y,mu,log=TRUE)) return(loglike) } #The call to bbmle and results summary: Rate_Constant = mle2(minuslogl = LL1, start = list(a=-2), control = list(maxit = 5000)) Rate_Constant_Summary = summary(Rate_Constant) ##### Model 6: Transmission route as the predictor variable x1=xMx[,1] x2=xMx[,2] x3=xMx[,3] x4=xMx[,4] x5=xMx[,5] x6=xMx[,6] x7=xMx[,7] x8=xMx[,8] #function for model y2 = function(b1,b2,b3,b4,b5,b6,b7,b8){ mu = b1*x1 + b2*x2 + b3*x3 + b4*x4 + b5*x5 + b6*x6 + b7*x7 + b8*x8 return(mu) } #The function for the negative log likelihood LL2 = function (b1,b2,b3,b4,b5,b6,b7,b8,y=data){ mu = y2(b1=b1,b2=b2,b3=b3,b4=b4,b5=b5,b6=b6,b7=b7,b8=b8) #get the total negative log likelihood loglike= -sum(dnorm(y,mu,log=TRUE)) return(loglike) } #The call to bbmle and results summary: Rate_TR = mle2(minuslogl = LL2, start = list(b1=-2,b2=-2,b3=-2,b4=-2,b5=0,b6=0,b7=-2,b8=-2), control = list(maxit = 5000)) Rate_TR_Summary = summary(Rate_TR) # The residuals resRate_TR = y2(coef(Rate_TR_Summary)[1,1], coef(Rate_TR_Summary)[2,1], coef(Rate_TR_Summary)[3,1], coef(Rate_TR_Summary)[4,1], coef(Rate_TR_Summary)[5,1],coef(Rate_TR_Summary)[6,1], coef(Rate_TR_Summary)[7,1], coef(Rate_TR_Summary)[8,1])-data ##### Model 7: Species as the predictor variable # Vector of 1 0 1. x1=zMx[,1] x2=zMx[,2] x3=zMx[,3] x4=zMx[,4] x5=zMx[,5] x6=zMx[,6] x7=zMx[,7] x8=zMx[,8] x9=zMx[,9] x10=zMx[,10] x11=zMx[,11] x12=zMx[,12] x13=zMx[,13] x14=zMx[,14] #function for model y3 = function(b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14){ mu = b1*x1 + b2*x2 + b3*x3 + b4*x4 + b5*x5 + b6*x6 + b7*x7 + b8*x8 + b9*x9+ b10*x10 + b11*x11 + b12*x12 + b13*x13 + b14*x14 return(mu) } #The function for the negative log likelihood LL3 = function (b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,y=data){ mu = y3(b1=b1,b2=b2,b3=b3,b4=b4,b5=b5,b6=b6,b7=b7,b8=b8,b9=b9,b10=b10,b11=b11,b12=b12,b13=b13,b14=b14) #get the total negative log likelihood loglike= -sum(dnorm(y,mu,log=TRUE)) return(loglike) } #The call to bbmle and results summary: Rate_Species = mle2(minuslogl = LL3, start = list(b1=-2,b2=-2,b3=-2,b4=-2,b5=-2,b6=-2,b7=-2,b8=-2,b9=-2,b10=-2,b11=-2,b12=-2,b13=-2,b14=-2), control = list(maxit = 5000)) Rate_Species_Summary = summary(Rate_Species) #The residuals resRate_Species = y3(coef(Rate_Species_Summary)[1,1], coef(Rate_Species_Summary)[2,1], coef(Rate_Species_Summary)[3,1], coef(Rate_Species_Summary)[4,1], coef(Rate_Species_Summary)[5,1], coef(Rate_Species_Summary)[6,1], coef(Rate_Species_Summary)[7,1], coef(Rate_Species_Summary)[8,1], coef(Rate_Species_Summary)[9,1], coef(Rate_Species_Summary)[10,1], coef(Rate_Species_Summary)[11,1], coef(Rate_Species_Summary)[12,1], coef(Rate_Species_Summary)[13,1], coef(Rate_Species_Summary)[14,1])-data # Model 8: Interaction x11=(Mx$species=="Staph" & Mx$Transmission == "air")*1 x12=(Mx$species=="Staph" & Mx$Transmission == "env")*1 x13=(Mx$species=="Staph" & Mx$Transmission == "equip")*1 x14=(Mx$species=="Staph" & Mx$Transmission == "HCW")*1; x15=(Mx$species=="Staph" & Mx$Transmission == "multi")*1; x21=(Mx$species=="Enteroc" & Mx$Transmission == "env")*1; x22=(Mx$species=="Enteroc" & Mx$Transmission == "equip")*1; x23=(Mx$species=="Enteroc" & Mx$Transmission == "HCW")*1; x24=(Mx$species=="Enteroc" & Mx$Transmission == "multi")*1; x31=(Mx$species=="Pseudo" & Mx$Transmission == "env")*1; x32=(Mx$species=="Pseudo" & Mx$Transmission == "equip")*1; x33=(Mx$species=="Pseudo" & Mx$Transmission == "HCW")*1; x34=(Mx$species=="Pseudo" & Mx$Transmission == "multi")*1; x41=(Mx$species=="Klebs" & Mx$Transmission == "env")*1; x42=(Mx$species=="Klebs" & Mx$Transmission == "equip")*1; x43=(Mx$species=="Klebs" & Mx$Transmission == "food")*1; x44=(Mx$species=="Klebs" & Mx$Transmission == "HCW")*1; x45=(Mx$species=="Klebs" & Mx$Transmission == "insect")*1; x46=(Mx$species=="Klebs" & Mx$Transmission == "multi")*1; x51=(Mx$species=="Acin" & Mx$Transmission == "env")*1; x52=(Mx$species=="Acin" & Mx$Transmission == "equip")*1; x53=(Mx$species=="Acin" & Mx$Transmission == "HCW")*1; x54=(Mx$species=="Acin" & Mx$Transmission == "PTP")*1; x55=(Mx$species=="Acin" & Mx$Transmission == "multi")*1; x61=(Mx$species=="Serr" & Mx$Transmission == "air")*1; x62=(Mx$species=="Serr" & Mx$Transmission == "env")*1; x63=(Mx$species=="Serr" & Mx$Transmission == "equip")*1; x64=(Mx$species=="Serr" & Mx$Transmission == "HCW")*1; x65=(Mx$species=="Serr" & Mx$Transmission == "multi")*1; x71=(Mx$species=="Enterob" & Mx$Transmission == "env")*1; x72=(Mx$species=="Enterob" & Mx$Transmission == "equip")*1; x73=(Mx$species=="Enterob" & Mx$Transmission == "food")*1; x74=(Mx$species=="Enterob" & Mx$Transmission == "HCW")*1; x75=(Mx$species=="Enterob" & Mx$Transmission == "PTP")*1; x76=(Mx$species=="Enterob" & Mx$Transmission == "multi")*1; x81=(Mx$species=="Hep B" & Mx$Transmission == "equip")*1; x82=(Mx$species=="Hep B" & Mx$Transmission == "HCW")*1; x83=(Mx$species=="Hep B" & Mx$Transmission == "PTP")*1; x84=(Mx$species=="Hep B" & Mx$Transmission == "multi")*1; x91=(Mx$species=="Hep C" & Mx$Transmission == "env")*1; x92=(Mx$species=="Hep C" & Mx$Transmission == "equip")*1; x93=(Mx$species=="Hep C" & Mx$Transmission == "HCW")*1; x94=(Mx$species=="Hep C" & Mx$Transmission == "PTP")*1; x95=(Mx$species=="Hep C" & Mx$Transmission == "multi")*1; x101=(Mx$species=="Strep" & Mx$Transmission == "air")*1; x102=(Mx$species=="Strep" & Mx$Transmission == "env")*1; x103=(Mx$species=="Strep" & Mx$Transmission == "equip")*1; x104=(Mx$species=="Strep" & Mx$Transmission == "food")*1; x105=(Mx$species=="Strep" & Mx$Transmission == "HCW")*1; x106=(Mx$species=="Strep" & Mx$Transmission == "PTP")*1; x107=(Mx$species=="Strep" & Mx$Transmission == "multi")*1; x111=(Mx$species=="Salm" & Mx$Transmission == "env")*1; x112=(Mx$species=="Salm" & Mx$Transmission == "equip")*1; x113=(Mx$species=="Salm" & Mx$Transmission == "food")*1; x114=(Mx$species=="Salm" & Mx$Transmission == "HCW")*1; x115=(Mx$species=="Salm" & Mx$Transmission == "PTP")*1; x116=(Mx$species=="Salm" & Mx$Transmission == "multi")*1; x121=(Mx$species=="Legion" & Mx$Transmission == "env")*1; x122=(Mx$species=="Legion" & Mx$Transmission == "multi")*1; x131=(Mx$species=="E. coli" & Mx$Transmission == "env")*1; x132=(Mx$species=="E. coli" & Mx$Transmission == "food")*1; x133=(Mx$species=="E. coli" & Mx$Transmission == "HCW")*1; x134=(Mx$species=="E. coli" & Mx$Transmission == "PTP")*1; x135=(Mx$species=="E. coli" & Mx$Transmission == "multi")*1; x141=(Mx$species=="C. diff" & Mx$Transmission == "env")*1; x142=(Mx$species=="C. diff" & Mx$Transmission == "equip")*1; x143=(Mx$species=="C. diff" & Mx$Transmission == "HCW")*1; x144=(Mx$species=="C. diff" & Mx$Transmission == "PTP")*1; x145=(Mx$species=="C. diff" & Mx$Transmission == "multi")*1; y4 = function(b11, b12, b13, b14, b15, b21, b22, b23, b24, b31,b32,b33,b34,b41,b42,b43,b44,b45,b46,b51,b52,b53, b54, b55, b61, b62, b63, b64, b65, b71, b72, b73, b74, b75, b76, b81, b82, b83, b84, b91, b92, b93, b94, b95, b101, b102, b103, b104, b105, b106, b107, b111, b112, b113, b114, b115, b116, b121, b122, b131, b132, b133, b134, b135, b141, b142, b143, b144, b145){ mu = b11*x11+ b12*x12 + b13*x13 + b14*x14 + b15*x15+b21*x21 + b22*x22 + b23*x23 + b24*x24 + b31*x31 + b32*x32 + b33*x33 + b34*x34+ b41*x41 + b42*x42 + b43*x43 + b44*x44 + b45*x45 + b46*x46 + b51*x51 + b52*x52 + b53*x53 + b54*x54 + b55*x55+ b61*x61 + b62*x62 + b63*x63 + b64*x64 + b65*x65+ b71*x71 + b72*x72 + b73*x73 + b74*x74 + b75*x75 + b76*x76+ b81*x81 + b82*x82 + b83*x83 + b84*x84+ b91*x91 + b92*x92 + b93*x93 + b94*x94 + b95*x95+ b101*x101 + b102*x102 + b103*x103 + b104*x104 + b105*x105 + b106*x106 + b107*x107+ b111*x111 + b112*x112+ b113*x113 + b114*x114 + b115*x115 + b116*x116+ b121*x121 + b122*x122+ b131*x131 + b132*x132 + b133*x133 + b134*x134 + b135*x135+ b141*x141 + b142*x142 + b143*x143 + b144*x144 + b145*x145 return(mu) } #The function for the negative log likelihood LL4 = function (b11,b12,b13,b14,b15,b21,b22,b23,b24,b31,b32,b33,b34,b41,b42,b43,b44,b45,b46,b51,b52,b53,b54,b55,b61,b62,b63,b64,b65,b71,b72,b73,b74,b75,b76,b81,b82,b83,b84,b91,b92,b93,b94,b95,b101,b102,b103,b104,b105,b106,b107,b111,b112,b113,b114,b115,b116,b121,b122,b131,b132,b133,b134,b135,b141,b142,b143,b144,b145, y=data){ mu = y4(b11=b11,b12=b12,b13=b13,b14=b14,b15=b15,b21=b21,b22=b22,b23=b23,b24=b24,b31=b31,b32=b32,b33=b33,b34=b34,b41=b41,b42=b42,b43=b43,b44=b44,b45=b45,b46=b46,b51=b51,b52=b52,b53=b53,b54=b54,b55=b55,b61=b61,b62=b62,b63=b63,b64=b64,b65=b65,b71=b71,b72=b72,b73=b73,b74=b74,b75=b75,b76=b76,b81=b81,b82=b82,b83=b83,b84=b84,b91=b91,b92=b92,b93=b93,b94=b94,b95=b95,b101=b101,b102=b102,b103=b103,b104=b104,b105=b105,b106=b106,b107=b107,b111=b111,b112=b112,b113=b113,b114=b114,b115=b115,b116=b116,b121=b121,b122=b122,b131=b131,b132=b132,b133=b133,b134=b134,b135=b135,b141=b141,b142=b142,b143=b143,b144=b144,b145=b145) #get the total negative log likelihood loglike= -sum(dnorm(y,mu,log=TRUE)) return(loglike) } Rate_Int = mle2(minuslogl = LL4, start = list(b11=-2,b12=-2,b13=-2,b14=-2,b15=-2,b21=-2,b22=-2,b23=-2,b24=-2,b31=-2,b32=-2,b33=-2,b34=-2,b41=-2,b42=-2,b43=-2,b44=-2,b45=-2,b46=-2,b51=-2,b52=-2,b53=-2,b54=-2,b55=-2,b61=-2,b62=-2,b63=-2,b64=-2,b65=-2,b71=-2,b72=-2,b73=-2,b74=-2,b75=-2,b76=-2,b81=-2,b82=-2,b83=-2,b84=-2,b91=-2,b92=-2,b93=-2,b94=-2,b95=-2,b101=-2,b102=-2,b103=-2,b104=-2,b105=-2,b106=-2,b107=-2,b111=-2,b112=-2,b113=-2,b114=-2,b115=-2,b116=-2,b121=-2,b122=-2,b131=-2,b132=-2,b133=-2,b134=-2,b135=-2,b141=-2,b142=-2,b143=-2,b144=-2, b145=-2), control = list(maxit = 5000)) Rate_Int_Summary = summary(Rate_Int) # The residuals resRate_Int = y4(coef(Rate_Int_Summary)[1,1], coef(Rate_Int_Summary)[2,1], coef(Rate_Int_Summary)[3,1], coef(Rate_Int_Summary)[4,1], coef(Rate_Int_Summary)[5,1], coef(Rate_Int_Summary)[6,1], coef(Rate_Int_Summary)[7,1], coef(Rate_Int_Summary)[8,1], coef(Rate_Int_Summary)[9,1], coef(Rate_Int_Summary)[10,1], coef(Rate_Int_Summary)[11,1], coef(Rate_Int_Summary)[12,1], coef(Rate_Int_Summary)[13,1], coef(Rate_Int_Summary)[14,1],coef(Rate_Int_Summary)[15,1], coef(Rate_Int_Summary)[16,1], coef(Rate_Int_Summary)[17,1], coef(Rate_Int_Summary)[18,1], coef(Rate_Int_Summary)[19,1], coef(Rate_Int_Summary)[20,1], coef(Rate_Int_Summary)[21,1], coef(Rate_Int_Summary)[22,1], coef(Rate_Int_Summary)[23,1], coef(Rate_Int_Summary)[24,1], coef(Rate_Int_Summary)[25,1], coef(Rate_Int_Summary)[26,1], coef(Rate_Int_Summary)[27,1], coef(Rate_Int_Summary)[28,1],coef(Rate_Int_Summary)[29,1], coef(Rate_Int_Summary)[30,1], coef(Rate_Int_Summary)[31,1], coef(Rate_Int_Summary)[32,1], coef(Rate_Int_Summary)[33,1], coef(Rate_Int_Summary)[34,1], coef(Rate_Int_Summary)[35,1], coef(Rate_Int_Summary)[36,1], coef(Rate_Int_Summary)[37,1], coef(Rate_Int_Summary)[38,1], coef(Rate_Int_Summary)[39,1], coef(Rate_Int_Summary)[40,1], coef(Rate_Int_Summary)[41,1], coef(Rate_Int_Summary)[42,1], coef(Rate_Int_Summary)[43,1], coef(Rate_Int_Summary)[44,1], coef(Rate_Int_Summary)[45,1], coef(Rate_Int_Summary)[46,1], coef(Rate_Int_Summary)[47,1], coef(Rate_Int_Summary)[48,1], coef(Rate_Int_Summary)[49,1], coef(Rate_Int_Summary)[50,1], coef(Rate_Int_Summary)[51,1], coef(Rate_Int_Summary)[52,1], coef(Rate_Int_Summary)[53,1], coef(Rate_Int_Summary)[54,1], coef(Rate_Int_Summary)[55,1], coef(Rate_Int_Summary)[56,1],coef(Rate_Int_Summary)[57,1], coef(Rate_Int_Summary)[58,1], coef(Rate_Int_Summary)[59,1], coef(Rate_Int_Summary)[60,1], coef(Rate_Int_Summary)[61,1], coef(Rate_Int_Summary)[62,1], coef(Rate_Int_Summary)[63,1], coef(Rate_Int_Summary)[64,1], coef(Rate_Int_Summary)[65,1], coef(Rate_Int_Summary)[66,1], coef(Rate_Int_Summary)[67,1], coef(Rate_Int_Summary)[68,1], coef(Rate_Int_Summary)[69,1])-data ################# # Make an AIC table that compares the model AICs AICtab(Cases_Constant, Cases_TR, Cases_Species, Cases_Int,weights=TRUE) AICtab(Rate_Constant, Rate_TR, Rate_Species, Rate_Int,weights=TRUE) # Recover the estimated parameters and confidence intervals that can be used to make graphs of parameter # estimates # Note: estimating the confidence intervals, especially for the Interaction model can take # a while # The fitted coefficients coeffs2 = coef(Rate_Int_Summary) # # Estimating the confidence intervals CI2 = confint(Rate_Int) # # Making a vector with the mean, and lower and upper CIs. coef_vec2 = array(0,c(69,3)) coef_vec2[,1] = coeffs2[,1] coef_vec2[,2] = CI2[,1] coef_vec2[,3] = CI2[,2] # The labels coef_labs2 = c("Staph-air", "Staph-env", "Staph-equip", "Staph-HCW", "Staph-multi", "Enteroc - env", "Enteroc-equip", "Enteroc-HCW", "Enteroc-multi", "Pseudo-env","Pseudo-equip","Pseudo-HCW","Pseudo-multi", "Klebs-env","Klebs-equip","Klebs-food","Klebs-HCW","Klebs-insect","Klebs-multi","Acin-env","Acin-equip","Acin-HCW", "Acin-PTP", "Acin-multi", "Serr-air", "Serr-env", "Serr-equip", "Serr-HCW", "Serr-multi", "Enterob-env", "Enterob-equip", "Enterob-food", "Enterob-HCW", "Enterob-PTP", "Enterob-multi", "Hep. B-equip", "Hep. B-HCW", "Hep. B-PTP", "Hep. B-multi", "Hep. C-env", "Hep. C-equip", "Hep. C-HCW", "Hep. C-PTP", "Hep. C-multi", "Strep-air", "Strep-env", "Strep-equip", "Strep-food", "Strep-HCW", "Strep-PTP", "Strep-multi", "Salm-env", "Salm-equip", "Salm-food", "Salm-HCW", "Salm-PTP","Salm-multi", "Legion-env","Legion-multi", "E. coli-env", "E. coli-food","E. coli-HCW", "E. coli-PTP", "E. coli-multi", "C. diff-env", "C. diff- equip", "C. diff-HCW", "C. diff-PTP", "C. diff-multi") # Ordering by the estimated coefficients and making sure the labels are in the correct order xx = data.frame(cbind(coef_labs2,coef_vec2[,1])) xx2=xx[with(xx, order(coef_vec2[,1])), ] coef_labs2 = xx2[,1] sorted_coeffs2 = coef_vec2[order(coef_vec2[,1]),] # As above but now for the Species - Cases model. coeffs = exp(coef(Cases_Species_Summary)) CI = confint(Cases_Species) coef_vec = array(0,c(14,3)) coef_vec[,1] = coeffs[,1] coef_vec[,2] = CI[,1] coef_vec[,3] = CI[,2] sorted_coeffs = coef_vec[order(coef_vec[,1]),] coef_labs = c("Strep", "Legion", "Hep C", "Heb B", "Pseudo", "E. coli", "Enterob", "Staph", "Acin", "Serr", "C. diff", "Klebs", "Enteroc", "Salm") # Print a summary of all models print(Cases_Constant_Summary) print(Cases_TR_Summary) print(Cases_Species_Summary) print(Cases_Int_Summary) print(Rate_Constant_Summary) print(Rate_TR_Summary) print(Rate_Species_Summary) print(Rate_Int_Summary) # Print the number of data points for each predictor variable print(colSums(xMx)) print(colSums(zMx)) ######################## # CODE TO MAKE THE FIGURES THAT APPEAR IN THE MANUSCRIPT # Boxplots of the data setEPS() postscript("/Users/amyhurford/Desktop/Figure1.eps") bymed1 <- with(Mx, reorder(Transmission, Cases..patient., median)) bymed2 <- with(Mx, reorder(species, Cases..patient., median)) bymed3 <- with(Mx, reorder(Transmission, Cases..patient./Duration..days., median)) bymed4 <- with(Mx, reorder(species, Cases..patient./Duration..days., median)) par(mfrow = c(2,2), mar = c(5, 5, 3, 3), mgp = c(4, 1, 0)) boxplot(log(Mx$Cases) ~ bymed1, las=2, ylab="log CASES", xlab="Transmission route") boxplot(log(Mx$Cases) ~ bymed2, las=2, ylab="log CASES", xlab="Species") boxplot(log(Mx$Cases/Mx$Duration) ~ bymed3, las=2, ylab = "log RATE", xlab="Transmission route") boxplot(log(Mx$Cases/Mx$Duration) ~ bymed4, las=2, ylab="log RATE", xlab="Species") dev.off() #plot of coefficients of Cases - Species model setEPS() postscript("/Users/amyhurford/Desktop/Figure5a.eps",width=10,height=5) par(mgp=c(4,1,0),mar = c(4, 5, 3, 3), mfrow = c(1,1)) plotCI(1:14, sorted_coeffs[,1], ui=sorted_coeffs[,3], li=sorted_coeffs[,2], xaxt="n", ylab = "Mean number of cases", las=2) axis(1,1:14, labels = coef_labs, las=2) dev.off() # And plot of coefficients for the Rate - Interaction models setEPS() postscript("/Users/amyhurford/Desktop/Figure5b.eps",width=15,height=7) par(mgp=c(4,1,0),mar = c(8, 5, 3, 3), mfrow = c(1,1)) plotCI(1:69, sorted_coeffs2[1:69,1], ui=sorted_coeffs2[1:69,3], li=sorted_coeffs2[1:69,2], xaxt="n", ylab = "Log mean number of cases per day", las=2) axis(1,1:69, labels = coef_labs2, las=2) dev.off() # Checking the assumptions of normality for all of the models: setEPS() postscript("/Users/amyhurford/Desktop/Figure4.eps") par(mfrow = c(4,2),mar = c(2,4,2,2)) qqnorm(logCases, main="", ylab = "Constant - Cases", xlab = "Theoretical quantiles") qqline(logCases) qqnorm(log(Mx$Cases/Mx$Duration), main="", ylab = "Constant - Rate", xlab = "Theoretical quantiles") qqline(logRate) qqnorm(resCase_TR,main="", ylab = "TR - Cases", xlab = "Theoretical quantiles") qqline(resCase_TR) qqnorm(resRate_TR, main="", ylab = "TR - Rate", xlab = "Theoretical quantiles") qqline(resRate_TR) qqnorm(resCase_Species,main="", ylab = "Species - Cases", xlab = "Theoretical quantiles") qqline(resCase_Species) qqnorm(resRate_Species, main="", ylab = "Species - Rate", xlab = "Theoretical quantiles") qqline(resRate_Species) qqnorm(resCase_Int, main="", ylab = "Int - Cases", xlab = "Theoretical quantiles") qqline(resCase_Int) qqnorm(resRate_Int, main="", ylab = "Int - Rate", xlab = "Theoretical quantiles") qqline(resRate_Int) dev.off() # Code to make the interaction boxplot for Cases setEPS() postscript("/Users/amyhurford/Desktop/Figure2.eps") par(mfrow = c(4,4), mar = c(3.5, 2, 2, 2), mgp = c(2, 1, 0)) #bymed1 <- with(MxAcin, reorder(Transmission, Cases..patient., median)) boxplot(log(MxAcin$Cases)~MxAcin$Transmission, las=2, ylab = "log CASES", main = "Acinobacter") #bymed1 <- with(MxCdiff, reorder(Transmission, Cases..patient., median)) boxplot(log(MxCdiff$Cases) ~ MxCdiff$Transmission, las=2, main = "C. difficle") #bymed1 <- with(MxEcoli, reorder(Transmission, Cases..patient., median)) boxplot(log(MxEcoli$Cases) ~ MxEcoli$Transmission, las=2, main = "E. coli") #bymed1 <- with(MxEnterob, reorder(Transmission, Cases..patient., median)) boxplot(log(MxEnterob$Cases) ~ MxEnterob$Transmission, las=2, main = "Enterobacter") #bymed1 <- with(MxEnteroc, reorder(Transmission, Cases..patient., median)) boxplot(log(MxEnteroc$Cases) ~ MxEnteroc$Transmission, las=2, ylab = "log CASES", main = "Enterococci") #bymed1 <- with(MxHepB, reorder(Transmission, Cases..patient., median)) boxplot(log(MxHepB$Cases) ~ MxHepB$Transmission, las=2, main = "Hep. B") #bymed1 <- with(MxHepC, reorder(Transmission, Cases..patient., median)) boxplot(log(MxHepC$Cases) ~ MxHepC$Transmission, las=2, main = "Hep. C") #bymed1 <- with(MxKlebs, reorder(Transmission, Cases..patient., median)) boxplot(log(MxKlebs$Cases) ~ MxKlebs$Transmission, las=2, main = "Klebsiella") #bymed1 <- with(MxLegion, reorder(Transmission, Cases..patient., median)) boxplot(log(MxLegion$Cases) ~ MxLegion$Transmission, las=2, main = "Legionella",ylab = "log CASES") #bymed1 <- with(MxPseudo, reorder(Transmission, Cases..patient., median)) boxplot(log(MxPseudo$Cases) ~ MxPseudo$Transmission, las=2, main = "Pseudomonas") #bymed1 <- with(MxSalm, reorder(Transmission, Cases..patient., median)) boxplot(log(MxSalm$Cases) ~ MxSalm$Transmission, las=2, main = "Salmonella") #bymed1 <- with(MxSerr, reorder(Transmission, Cases..patient., median)) boxplot(log(MxSerr$Cases) ~ MxSerr$Transmission, las=2, main = "Serratia") #bymed1 <- with(MxStaph, reorder(Transmission, Cases..patient., median)) boxplot(log(MxStaph$Cases) ~ MxStaph$Transmission, las=2, main = "Staphylococcus", ylab = "log CASES") #bymed1 <- with(MxStrep, reorder(Transmission, Cases..patient., median)) boxplot(log(MxStrep$Cases) ~ MxStrep$Transmission, las=2, main = "Streptococcus") dev.off() # And code to make the interaction boxplot for Rate setEPS() postscript("/Users/amyhurford/Desktop/Figure3.eps") par(mfrow = c(4,4), mar = c(3.5, 2, 2, 2), mgp = c(2, 1, 0)) #bymed2 <- with(MxAcin, reorder(Transmission, Cases..patient./Duration..days., median)) boxplot(log(MxAcin$Cases/MxAcin$Duration) ~ MxAcin$Transmission, las=2, ylab = "log RATE", main = "Acinobacter") #bymed2 <- with(MxCdiff, reorder(Transmission, Cases..patient./Duration..days., median)) boxplot(log(MxCdiff$Cases/MxCdiff$Duration) ~ MxCdiff$Transmission, las=2, main = "C. difficle") #bymed2 <- with(MxEcoli, reorder(Transmission, Cases..patient./Duration..days., median)) boxplot(log(MxEcoli$Cases/MxEcoli$Duration) ~ MxEcoli$Transmission, las=2, main = "E. coli") #bymed2 <- with(MxEnterob, reorder(Transmission, Cases..patient./Duration..days., median)) boxplot(log(MxEnterob$Cases/MxEnterob$Duration) ~ MxEnterob$Transmission, las=2, main = "Enterobacter") #bymed2 <- with(MxEnteroc, reorder(Transmission, Cases..patient./Duration..days., median)) boxplot(log(MxEnteroc$Cases/MxEnteroc$Duration) ~ MxEnteroc$Transmission, las=2, ylab = "log RATE", main = "Enterococci") #bymed2 <- with(MxHepB, reorder(Transmission, Cases..patient./Duration..days., median)) boxplot(log(MxHepB$Cases/MxHepB$Duration) ~ MxHepB$Transmission, las=2, main = "Hep. B") #bymed2 <- with(MxHepC, reorder(Transmission, Cases..patient./Duration..days., median)) boxplot(log(MxHepC$Cases/MxHepC$Duration) ~ MxHepC$Transmission, las=2, main = "Hep. C") #bymed2 <- with(MxKlebs, reorder(Transmission, Cases..patient./Duration..days., median)) boxplot(log(MxKlebs$Cases/MxKlebs$Duration) ~ MxKlebs$Transmission, las=2, main = "Klebsiella") #bymed2 <- with(MxLegion, reorder(Transmission, Cases..patient./Duration..days., median)) boxplot(log(MxLegion$Cases/MxLegion$Duration) ~ MxLegion$Transmission, las=2, main = "Legionella",ylab = "log RATE") #bymed2 <- with(MxPseudo, reorder(Transmission, Cases..patient./Duration..days., median)) boxplot(log(MxPseudo$Cases/MxPseudo$Duration) ~ MxPseudo$Transmission, las=2, main = "Pseudomonas") #bymed2 <- with(MxSalm, reorder(Transmission, Cases..patient./Duration..days., median)) boxplot(log(MxSalm$Cases/MxSalm$Duration) ~ MxSalm$Transmission, las=2, main = "Salmonella") #bymed2 <- with(MxSerr, reorder(Transmission, Cases..patient./Duration..days., median)) boxplot(log(MxSerr$Cases/MxSerr$Duration) ~ MxSerr$Transmission, las=2, main = "Serratia") #bymed2 <- with(MxStaph, reorder(Transmission, Cases..patient./Duration..days., median)) boxplot(log(MxStaph$Cases/MxStaph$Duration) ~ MxStaph$Transmission, las=2, main = "Staphylococcus", ylab = "log RATE") #bymed2 <- with(MxStrep, reorder(Transmission, Cases..patient./Duration..days., median)) boxplot(log(MxStrep$Cases/MxStrep$Duration) ~ MxStrep$Transmission, las=2, main = "Streptococcus") dev.off() # Supplementary analysis - Appears in discussion # Frequency of outbreaks from different species #Species Species = c("Staph", "Pseudo", "Klebs", "Acin", "Enteroc", "Enterob", "Strep", "Serr", "Hep B", "Hep C", "Salm", "Legion") Hurford = c(268, 144, 79, 169, 116, 62, 87,116,85,85,78,55,39,15) Gastmeier = c(223, 129, 115, 105, 67, 66, 63,94,70,56,56,48) Chi_S2 = sum((Gastmeier - mean(Gastmeier))^2/mean(Gastmeier)) qchisq(0.001,11, lower.tail=FALSE) setEPS() postscript("/Users/amyhurford/Desktop/AdditionalFig.eps",width=15,height=7) par(mgp=c(4,1,0),mar = c(8, 5, 3, 3), mfrow = c(1,1)) plotCI(1:69, exp(sorted_coeffs2[1:69,1]), ui=exp(sorted_coeffs2[1:69,3]), li=exp(sorted_coeffs2[1:69,2]), xaxt="n", ylab = "Log mean number of cases per day", las=2) axis(1,1:69, labels = coef_labs2, las=2) dev.off()