1 ## PsN R script for plotting results of bootstrap
5 ## August 2006, April 2007
8 ## autogenerated code begins
9 ## we can set other things, like colours, line types, layout, etc here if we wish
11 ## This script assumes "ofv" is the last column before we get into the parameter estimates
13 min.failed <- FALSE # do we want to omit minimization failed runs?
14 cov.failed <- FALSE # do we want to omit covariance failed runs?
15 cov.warnings <- TRUE # do we want to omit covariance failed runs?
16 boundary <- TRUE # do we want to omit boundary runs?
17 showoriginal <- TRUE # show line for original estimate
18 showmean <- TRUE # show line for mean
19 showmedian <- FALSE # show line for median
20 show95CI <- TRUE # show line for 95 % confidence interval (percentile)
21 showquart <- FALSE # show line for quartiles
23 excl.id <- c() # exclude samples that have this individual
25 ## autogenerated code ends
28 bootstrap.data <- read.csv("raw_results1.csv", header=T)
29 incl.ids <- read.csv("included_individuals1.csv", header=F)
31 ## replace underscores
32 for (i in 1:length(names(bootstrap.data))) {
33 names(bootstrap.data)[i] <- gsub("\_", "\.", names(bootstrap.data)[i])
36 ## find ofv column index
40 for (i in names(bootstrap.data)) {
49 ## get number of parameters
50 n <- length(colnames(bootstrap.data)) - index
51 nparams <- length(colnames(bootstrap.data))
53 ## separate out original model fit
54 p1 <- subset(bootstrap.data, bootstrap.data$model != 0)
55 o1 <- subset(bootstrap.data, bootstrap.data$model == 0)
57 incl.flag <- rep(0,length(rownames(p1)))
59 incl.flag <- incl.flag + rowSums( incl.ids == i )
62 p1 <- p1[(incl.flag==0),]
64 #names(p1)[2] <- "minimization.successful"
65 #names(p1)[3] <- "covariance.step.successful"
66 #names(p1)[4] <- "covariance.step.warnings"
67 #names(p1)[5] <- "estimate.near.boundary"
71 p1 <- subset(p1, minimization.successful == 1)
74 p1 <- subset(p1, covariance.step.successful == 1)
77 p1 <- subset(p1, covariance.step.warnings == 0)
80 p1 <- subset(p1, estimate.near.boundary == 0)
83 ## stats and plots for each- single
85 for (i in index:nparams) {
86 if (mode(p1[[i]]) == "numeric" &&
87 sum(p1[[i]],na.rm=T)) {
88 sp <- summary(p1[[i]])
89 # IQR <- diff(summary(p1[[i]])[c(5,2)])
90 dp <- density(p1[[i]], na.rm=T)
91 parmlabel <- names(p1)[i]
93 pdf(file=paste("bootstrap.", parmlabel, ".pdf", sep=""), paper="special",
94 title=paste("Bootstrap results - ", parmlabel, sep=""),width=10,height=7 )
96 qu <- quantile(p1[[i]], c(0.025, 0.975), na.rm=T)
98 legend=paste("n = ", nrow(p1), sep="")
100 legend=paste(legend, "; Mean = ", sp[4], sep="")
103 legend=paste(legend, "; Median = ", sp[3], sep="")
106 legend=paste(legend, "; Orig = ", o1[[i]], sep="")
110 main = paste("Bootstrap results - ", parmlabel, sep=""),
112 # ylim = c(0, max(dp$y)),
114 xlim = c(min(dp$x), max(dp$x)),
116 # xlim = c(min(p1[[i]]) - min(p1[[i]])*0.15,max(p1[[i]]) + max(p1[[i]])*0.15),
119 # sub=paste(paste(paste("n = ", nrow(p1), sep=""), "; Median = ", sp[4], sep=""), "; Orig = ", o1[[i]], sep="") )
121 #h <- hist(p1[[i]], prob=T, plot=F)
123 lines(dp, lwd=2, lty=3, col="red")
126 abline(v=sp[2], lwd= 1, lty=3, col="red") ## 1st quartile
127 abline(v=sp[5], lwd= 1, lty=3, col="red") ## 3rd quartile
130 abline(v=sp[4], lty=2, lwd=1, col="red") ## mean
133 abline(v=sp[3], lty=1, lwd=2, col="red") ## median
136 abline(v=o1[[i]], lty=2, lwd=1, col="red") ## original
139 abline(v=qu[1], lty=4, lwd=1, col="red") ## 2.5% CL
140 abline(v=qu[2], lty=4, lwd=1, col="red") ## 97.5% CL
141 text(qu[1], max(dp$y), labels=signif(qu[1], digits = 3), cex = .8, adj = c(0,0), pos='2')
142 text(qu[2], max(dp$y), labels=signif(qu[2], digits = 3), cex = .8, adj = c(0,0), pos='4')
144 # abline(v=sp[4], lty=1, lwd=2, col="red") ## median
145 # abline(v=o1[[i]], lty=2, lwd=1, col="red") ## original
146 # abline(v=qu[1], lty=4, lwd=1, col="red") ## 2.5% CL
147 # abline(v=qu[2], lty=4, lwd=1, col="red") ## 97.5% CL
149 # text(qu[1], max(dp$y), labels=signif(qu[1], digits = 3), cex = .8, adj = c(0,0), pos='2')
150 # text(qu[2], max(dp$y), labels=signif(qu[2], digits = 3), cex = .8, adj = c(0,0), pos='4')
151 #text(sp[4], max(h$density), labels=paste("Med: ", signif(sp[4], digits = 3), sep=""), adj = c(-1,0), cex = .8, pos='2')
155 ## stats and plots for each - 6 per sheet
160 for (i in index:nparams) {
161 if (mode(p1[[i]]) == "numeric" &&
162 sum(p1[[i]],na.rm=T)) {
163 sp <- summary(p1[[i]])
164 # IQR <- diff(summary(p1[[i]])[c(5,2)])
165 dp <- density(p1[[i]], na.rm=T)
166 parmlabel <- names(p1)[i]
170 pdf(file=paste("bootstrap.page", bspage, ".pdf", sep=""), paper="special",
171 title="Bootstrap results",width=10,height=7)
176 qu <- quantile(p1[[i]], c(0.025, 0.975), na.rm=T)
178 legend=paste("n = ", nrow(p1), sep="")
180 legend=paste(legend, "; Mean = ", sp[3], sep="")
183 legend=paste(legend, "; Median = ", sp[4], sep="")
186 legend=paste(legend, "; Orig = ", o1[[i]], sep="")
190 main = paste("Bootstrap results - ", parmlabel, sep=""),
192 # ylim = c(0, max(dp$y)),
194 xlim = c(min(dp$x), max(dp$x)),
196 # xlim = c(min(p1[[i]]) - min(p1[[i]])*0.15,max(p1[[i]]) + max(p1[[i]])*0.15),
199 #=paste(paste(paste("n = ", nrow(p1), sep=""), "; Median = ", sp[4], sep=""), "; Orig = ", o1[[i]], sep="") )
201 #h <- hist(p1[[i]], prob=T, plot=F)
203 lines(dp, lwd=2, lty=3, col="red")
206 abline(v=sp[2], lwd= 1, lty=3, col="red") ## 1st quartile
207 abline(v=sp[5], lwd= 1, lty=3, col="red") ## 3rd quartile
210 abline(v=sp[3], lty=2, lwd=1, col="red") ## mean
213 abline(v=sp[4], lty=1, lwd=2, col="red") ## median
216 abline(v=o1[[i]], lty=2, lwd=1, col="red") ## original
219 abline(v=qu[1], lty=4, lwd=1, col="red") ## 2.5% CL
220 abline(v=qu[2], lty=4, lwd=1, col="red") ## 97.5% CL
221 text(qu[1], max(dp$y), labels=signif(qu[1], digits = 3), cex = .8, adj = c(0,0), pos='2')
222 text(qu[2], max(dp$y), labels=signif(qu[2], digits = 3), cex = .8, adj = c(0,0), pos='4')
225 # abline(v=sp[4], lty=1, lwd=2, col="red") ## median
226 # abline(v=o1[[i]], lty=2, lwd=1, col="red") ## original
227 # abline(v=qu[1], lty=4, lwd=1, col="red") ## 2.5% CL
228 # abline(v=qu[2], lty=4, lwd=1, col="red") ## 97.5% CL
230 # text(qu[1], max(dp$y), labels=signif(qu[1], digits = 3), cex = .8, adj = c(0,0), pos='2')
231 # text(qu[2], max(dp$y), labels=signif(qu[2], digits = 3), cex = .8, adj = c(0,0), pos='4')
232 #text(sp[4], max(h$density), labels=paste("Med: ", signif(sp[4], digits = 3), sep=""), adj = c(-1,0), cex = .8, pos='2')