###INSTRUCTIONS#### #If not already installed, install WinBUGS from http://www.mrc-bsu.cam.ac.uk/software/bugs/the-bugs-project-winbugs/ #If not already installed, install R from http://cran.r-project.org/ #Within R, install the package "R2WinBUGS". #Adjust the following, as needed path <- "C:/" #Give the path to your working directory filename <- "Additionalfile4.csv" #Give the name of the file containing the data, organized in a similar way as the example file in Additional file 4 bugs.directory <- "C:/winbugs14/WinBUGS14" #Set the directory of WinBUGS14 #Then select all text below, and copy it into the R console and press enter. Figures should appear in your working directory ############# data <- read.csv(paste(path, filename, sep= "")) levels(data$Site) #Power model for(z in 1: length(levels(data$Site))){ subdata <- subset(data, Site==levels(data$Site)[z]) # Select data for one site subdata2 <- subset(subdata, HLC+LTC>0) #Exclude data with 0 mosquitoes in both of the traps subdata2 X<-c(subdata2$HLC,subdata2$LTC) obs<-length(X) method<- c(rep(1, length(subdata2$HLC)),rep(2, length(subdata2$HLC))) set<-rep(c(1:length(subdata2$HLC)),2) sets<-length(subdata2$HLC) data.win<- list(obs=obs,sets=sets, X=X, set=set, method=method) data.win library(R2WinBUGS) modelPOWERCURVEWITHRANDOMPARAMETERS<-function(){ for (i in 1:obs) { X[i] ~dpois(EX[i]) EX[i] <-exp(logEY[i]*power[method[i]]) logEY[i]<- setEffect[set[i]]+b1[method[i]] } for (k in 1:2) { b1avg[k] ~ dnorm(0.0,1.0E-1) multavg[k]<-exp(b1avg[k]) sigmab1[k]~ dunif(0,2) taub1[k] <-1/(sigmab1[k]*sigmab1[k]) gamavg[k] ~ dnorm(0.0,1.0E-1) sigmagam[k]~ dunif(0,1) taugam[k] <-1/(sigmagam[k]*sigmagam[k]) } for (j in 1:sets) { setEffect[j]~dnorm(siteMean,sitePrec) } siteMean ~ dnorm(0.0,1.0E-1) siteSigma ~ dunif(0,2) sitePrec <- 1/(siteSigma*siteSigma) multiplier[1]<-1.0 b1[1]<-0.0 power[1]<-1.0 for (k2 in 1:1) { logpower[k2+1]~dnorm(gamavg[k2],taugam[k2]) b1[k2+1] ~ dnorm(b1avg[k2],taub1[k2]) power[k2+1]<-exp(logpower[k2+1]) multiplier[k2+1]<-exp(b1[k2+1]) for (sqrtx in 1:30) { yval[sqrtx,k2+1] <- pow(multiplier[k2+1]*xval[sqrtx],power[k2+1]) } } for (sqrtx in 1:30) { xval[sqrtx] <- sqrtx*sqrtx } for (sqrtx in 1:30) { yval[sqrtx,1] <- pow(multiplier[1]*xval[sqrtx],power[1]) } } write.model(modelPOWERCURVEWITHRANDOMPARAMETERS, con = "modelPOWERCURVEWITHRANDOMPARAMETERS.txt") parameters<-c("siteSigma","siteMean","power","multiplier","yval") inits<-list(list( b1avg=c(1,1), sigmab1=c(1,1), gamavg =c(1,1), sigmagam = c(0.5, 0.5), siteMean=1, siteSigma=1 )) n.iter=40000 n.burnin=20000 n.thin=20 n.chains=1 winoutputmodelPOWERCURVEWITHRANDOMPARAMETERS <- bugs(data= data.win, inits=inits, parameters=parameters, model.file= "modelPOWERCURVEWITHRANDOMPARAMETERS.txt", n.iter=n.iter, n.burnin=n.burnin, n.thin=n.thin, n.chains=n.chains, bugs.directory=bugs.directory, debug=FALSE) #Set debug=TRUE for WinBUGS result viewing print(winoutputmodelPOWERCURVEWITHRANDOMPARAMETERS,digits.summary=2) quant.mat<-matrix(nrow=30, ncol=3) for(i in 1: 30){ sims.yval<-winoutputmodelPOWERCURVEWITHRANDOMPARAMETERS$sims.list$yval[,i,1] quant.mat[i,]<-quantile(sims.yval, probs=c(0.025, 0.5, 0.975)) } quant.mat png(filename = paste(path, levels(data$Site)[z], ".png", sep=""), width = 2200, height = 2200, pointsize = 12, bg = "white", res = 288) median.res<-quant.mat[,2] LB.res<-quant.mat[,1] UB.res<-quant.mat[,3] op <- par(mar= c(4, 4, 0.5, 0.5) + 0.1) curt<-function(x){x^(1/3)} ltysel<-c(rep(1,8), rep(2,6)) plot(type="n", y=curt(c(1:30)^2), x=curt(median.res), ylim=curt(c(0,31)^2), xlim=curt(c(0,31)^2), ylab="", xlab="", xaxt="n", yaxt="n") clip(-100,curt(30^2),-100,curt(30^2)) #restricts plotting area polygon(x=curt(c(0:30,30:0)^2), y=curt(c(c(0,LB.res) , c(rev(UB.res),0))), col=rgb(255/255, 165/255, 0, alpha=1), border=NA) lines(x=curt(c(0:30)^2), y=curt(c(0, median.res)), col=1) #lines(x=curt(c(0:30)^2), y=curt(c(0, LB.res)), col=3) #lines(x=curt(c(0:30)^2), y=curt(c(0, UB.res)), col=2) points(curt(subdata2$HLC), curt(subdata2$LTC)) axis(2, at=c(0:10), labels= c(0:10)^3) axis(1, at=c(0:10), labels= c(0:10)^3) #cat(LB.res, sep=c(", ")) LB.gam <- c(0.05371309, 0.386385, 0.8130519, 1.178634, 1.43138, 1.590949, 1.740548, 1.869774, 2.010284, 2.118458, 2.21268, 2.313937, 2.390382, 2.516749, 2.603819, 2.65975, 2.713556, 2.760355, 2.800479, 2.851123, 2.900146, 2.947673, 2.993306, 3.025993, 3.082321, 3.125233, 3.173611, 3.210164, 3.246942, 3.280516) #cat(UB.res, sep=c(", ")) UB.gam <- c(4339.182, 196824.8, 1995010, 9856485, 35313627, 121138340, 359888903, 765957490, 1852684899, 3474106039, 6230142224, 11556716013, 24930032193, 39519937685, 77643238823, 115186907396, 193349593967, 319553800634, 514044526081, 695170665565, 904294639852, 1.162397e+12, 1.478058e+12, 1.891395e+12, 2.546385e+12, 3.388962e+12, 4.462531e+12, 5.818344e+12, 7.516545e+12, 9.627321e+12) #cat(LB.res, sep=c(", ")) LB.fun <- c(0.2503309, 0.7246348, 1.004347, 1.128024, 1.20409, 1.259225, 1.318183, 1.368927, 1.390521, 1.425818, 1.463982, 1.508605, 1.536189, 1.550728, 1.58857, 1.602611, 1.615852, 1.628557, 1.639601, 1.650094, 1.661574, 1.687523, 1.696694, 1.705648, 1.725928, 1.735756, 1.74554, 1.756478, 1.769961, 1.777932) #cat(UB.res, sep=c(", ")) UB.fun <- c(1481.127, 22295.91, 106160.7, 343870.5, 931374.7, 2041190, 3993353, 7481826, 13296706, 22820073, 39103854, 59915333, 92416737, 131803268, 178839427, 237955258, 311210370, 400861383, 509095908, 637378380, 838850513, 1137018596, 1509827944, 1792820582, 2235758730, 3038523877, 4082234701, 5426162515, 7051582281, 8495796829) lines(y=curt(c(0:30)^2), x=curt(c(0, LB.gam)), col="grey", lwd=3) lines(y=curt(c(0:30)^2), x=curt(c(0, UB.gam)), col="grey", lwd=3) lines(y=curt(c(0:30)^2), x=curt(c(0, LB.fun)), col="grey", lt=3, lwd=3) lines(y=curt(c(0:30)^2), x=curt(c(0, UB.fun)), col="grey", lt=3, lwd=3) mtext("Light trap catch on cube root scale", side=2, outer=FALSE, line=3) mtext("Indoor human landing catch on cube root scale", side=1, outer=FALSE, line=3) par(op) dev.off() } #Linear model for(z in 1: length(levels(data$Site))){ subdata <- subset(data, Site==levels(data$Site)[z]) # Select data for one site subdata2 <- subset(subdata, HLC>0 & LTC>0) #Exclude data with 0 mosquitoes in one of the traps subdata2 <- subset(subdata, HLC+LTC>0) #Exclude data with 0 mosquitoes in both of the traps subdata2 X<-c(subdata2$HLC,subdata2$LTC) obs<-length(X) method<- c(rep(1, length(subdata2$HLC)),rep(2, length(subdata2$HLC))) set<-rep(c(1:length(subdata2$HLC)),2) sets<-length(subdata2$HLC) data.win<- list(obs=obs,sets=sets, X=X, set=set, method=method) data.win library(R2WinBUGS) modelLINEARWITHRANDOMPARAMETERS<-function(){ for (i in 1:obs) { X[i] ~dpois(EX[i]) EX[i] <-exp(logEY[i]) logEY[i]<- setEffect[set[i]]+b1[method[i]] } for (k in 1:2) { b1avg[k] ~ dnorm(0.0,1.0E-1) multavg[k]<-exp(b1avg[k]) sigmab1[k]~ dunif(0,2) taub1[k] <-1/(sigmab1[k]*sigmab1[k]) } for (j in 1:sets) { setEffect[j]~dnorm(siteMean,sitePrec) } siteMean ~ dnorm(0.0,1.0E-1) siteSigma ~ dunif(0,2) sitePrec <- 1/(siteSigma*siteSigma) multiplier[1]<-1.0 b1[1]<-0.0 for (k2 in 1:1) { b1[k2+1] ~ dnorm(b1avg[k2],taub1[k2]) multiplier[k2+1]<-exp(b1[k2+1]) for (sqrtx in 1:30) { yval[sqrtx,k2+1] <- multiplier[k2+1]*xval[sqrtx] } } for (sqrtx in 1:30) { xval[sqrtx] <- sqrtx*sqrtx } for (sqrtx in 1:30) { yval[sqrtx,1] <- multiplier[1]*xval[sqrtx] } } write.model(modelLINEARWITHRANDOMPARAMETERS, con = "modelLINEARWITHRANDOMPARAMETERS.txt") parameters<-c("siteSigma","siteMean","multiplier","yval") inits<-list(list( b1avg=c(1,1), sigmab1=c(1,1), siteMean=1, siteSigma=1 )) n.iter=40000 n.burnin=20000 n.thin=20 n.chains=1 winoutputmodelLINEARWITHRANDOMPARAMETERS <- bugs(data= data.win, inits=inits, parameters=parameters, model.file= "modelLINEARWITHRANDOMPARAMETERS.txt", n.iter=n.iter, n.burnin=n.burnin, n.thin=n.thin, n.chains=n.chains, bugs.directory=bugs.directory, debug=FALSE) #Set debug=TRUE for WinBUGS result viewing print(winoutputmodelLINEARWITHRANDOMPARAMETERS,digits.summary=2) quant.mat<-matrix(nrow=30, ncol=3) for(i in 1: 30){ sims.yval<-winoutputmodelLINEARWITHRANDOMPARAMETERS$sims.list$yval[,i,1] quant.mat[i,]<-quantile(sims.yval, probs=c(0.025, 0.5, 0.975)) } quant.mat png(filename = paste(path, "LIN", levels(data$Site)[z], ".png", sep=""), width = 2200, height = 2200, pointsize = 12, bg = "white", res = 288) median.res<-quant.mat[,2] LB.res<-quant.mat[,1] UB.res<-quant.mat[,3] op <- par(mar= c(4, 4, 0.5, 0.5) + 0.1) curt<-function(x){x^(1/3)} ltysel<-c(rep(1,8), rep(2,6)) plot(type="n", y=curt(c(1:30)^2), x=curt(median.res), ylim=curt(c(0,31)^2), xlim=curt(c(0,31)^2), ylab="", xlab="", xaxt="n", yaxt="n") clip(-100,curt(30^2),-100,curt(30^2)) #restricts plotting area polygon(x=curt(c(0:30,30:0)^2), y=curt(c(c(0,LB.res) , c(rev(UB.res),0))), col=rgb(255/255, 165/255, 0, alpha=1), border=NA) lines(x=curt(c(0:30)^2), y=curt(c(0, median.res)), col=1) #lines(x=curt(c(0:30)^2), y=curt(c(0, LB.res)), col=3) #lines(x=curt(c(0:30)^2), y=curt(c(0, UB.res)), col=2) points(curt(subdata2$HLC), curt(subdata2$LTC)) axis(2, at=c(0:10), labels= c(0:10)^3) axis(1, at=c(0:10), labels= c(0:10)^3) #cat(LB.res, sep=c(", ")) LB.gam.lin<-c(0.2010875, 0.80435, 1.809788, 3.2174, 5.027188, 7.23915, 9.853288, 12.8696, 16.28809, 20.10875, 24.33159, 28.9566, 33.98379, 39.41315, 45.24469, 51.4784, 58.11429, 65.15235, 72.59259, 80.435, 88.67959, 97.32635, 106.3753, 115.8264, 125.6797, 135.9351, 146.5928, 157.6526, 169.1146, 180.9788) #cat(UB.res, sep=c(", ")) UB.gam.lin<-c(4.822025, 19.2881, 43.39823, 77.1524, 120.5506, 173.5929, 236.2792, 308.6096, 390.584, 482.2025, 583.465, 694.3716, 814.9222, 945.1169, 1084.956, 1234.438, 1393.565, 1562.336, 1740.751, 1928.81, 2126.513, 2333.86, 2550.851, 2777.486, 3013.766, 3259.689, 3515.256, 3780.468, 4055.323, 4339.823) #cat(LB.res, sep=c(", ")) LB.fun.lin<-c(0.1686, 0.6744, 1.5174, 2.6976, 4.215, 6.0696, 8.2614, 10.7904, 13.6566, 16.86, 20.4006, 24.2784, 28.4934, 33.0456, 37.935, 43.1616, 48.7254, 54.6264, 60.8646, 67.44, 74.3526, 81.6024, 89.1894, 97.1136, 105.375, 113.9736, 122.9094, 132.1824, 141.7926, 151.74) #cat(UB.res, sep=c(", ")) UB.fun.lin<-c(2.425275, 9.7011, 21.82748, 38.8044, 60.63188, 87.3099, 118.8385, 155.2176, 196.4473, 242.5275, 293.4583, 349.2396, 409.8715, 475.3539, 545.6869, 620.8704, 700.9045, 785.7891, 875.5243, 970.11, 1069.546, 1173.833, 1282.97, 1396.958, 1515.797, 1639.486, 1768.025, 1901.416, 2039.656, 2182.748) lines(y=curt(c(0:30)^2), x=curt(c(0, LB.gam.lin)), col="grey", lwd=3) lines(y=curt(c(0:30)^2), x=curt(c(0, UB.gam.lin)), col="grey", lwd=3) lines(y=curt(c(0:30)^2), x=curt(c(0, LB.fun.lin)), col="grey", lt=3, lwd=3) lines(y=curt(c(0:30)^2), x=curt(c(0, UB.fun.lin)), col="grey", lt=3, lwd=3) mtext("Light trap catch on cube root scale", side=2, outer=FALSE, line=3) mtext("Indoor human landing catch on cube root scale", side=1, outer=FALSE, line=3) par(op) dev.off() }