# NOT RUN {
## An example to calculate confidence intervals
## The test statistic is that for "t4" in data(mam15)
data(mam15) # load mam15.mt
sa <- 10^seq(-2,2,length=13) # parameter for multiscale bootstrap
## Definition of a test statistic of interest.
## "myfun" returns the maximum difference of log-likelihood value
## for a tree named a.
myfun <- function(x,w,a) maxdif(wsumrow(x,w))[[a]]
maxdif <- function(x) {
i1 <- which.max(x) # the largest element
x <- -x + x[i1]
x[i1] <- -min(x[-i1]) # the second largest value
x
}
wsumrow <- function(x,w) {
apply(w*x,2,sum)*nrow(x)/sum(w)
}
# }
# NOT RUN {
## a quick example with nb=1000 (fairely fast in 2017)
## Compute multiscale bootstrap replicates
nb <- 1000 # nb = 10000 is better but slower
# the following line takes some time (less than 1 minute in 2017)
sim <- scaleboot(mam15.mt,nb,sa,myfun,"t4",count=FALSE,onlyboot=TRUE)
## show 90<!-- % confidence interval for p=(0.05, 0.95) -->
## each tail is also interpreted as 95<!-- % confidence limit -->
(conf1 <- sbconf(sim$stat,sim$sa,model="sing.3",k=1)) # with k=1
(conf2 <- sbconf(conf1,model="sing.3",k=2)) # with k=2
(conf3 <- sbconf(conf2,model="sing.3",k=3)) # with k=3
## plot diagnostics for computing the confidence limits
plot(conf3) # AIC values for models v.s. test statistic value
plot(conf3,yval="zval",type="l") # corrected "k.3" z-value
# }
# NOT RUN {
# }
# NOT RUN {
## a longer example with nb=10000 (it was slow in 2010)
## In the following, we used 40 cpu's.
nb <- 10000
library(parallel)
cl <- makeCluster(40)
clusterExport(cl,c("maxdif","wsumrow"))
## Compute multiscale bootstrap replicates
## (It took 80 secs using 40 cpu's)
sim <- scaleboot(mam15.mt,nb,sa,myfun,"t4",count=FALSE,
cluster=cl,onlyboot=TRUE)
## Modify option "probs0" to a fine grid with 400 points
## default: 0.001 0.010 0.100 0.900 0.990 0.999
## NOTE: This modification is useful only when cl != NULL,
## in which case calls to sbfit for the grid points
## are made in parallel, although iterations seen later
## are made sequentially.
sboptions("probs0",pnorm(seq(qnorm(0.001),qnorm(0.999),length=400)))
## Calculate bootstrap confidence intervals using "k.1" p-value.
## (It took 70 secs using 40 cpu's)
## First, sbfit is applied to bp's determined by option "probs0"
## Then, additional fitting is made only twice for iteration.
## p[1]=0.05 iter=1 t=4.342723 e=0.0003473446 r=0.0301812
## p[2]=0.95 iter=1 t=42.76558 e=0.002572495 r=0.1896809
conf1 <- sbconf(sim$stat,sim$sa,model="sing.3",k=1,cluster=cl)
## The confidence interval with "k.1" is printed as
## 0.05 0.95
## 4.342723 42.765582
conf1
## Calculate bootstrap confidence intervals
## using "k.2" and "k.3" p-values.
## (It took only 10 secs)
## p[1]=0.05 iter=1 t=-2.974480 e=0.003729190 r=0.04725755
## p[2]=0.95 iter=1 t=39.51767 e=0.001030929 r=0.06141937
## 0.05 0.95
## -2.974480 39.517671
conf2 <- sbconf(conf1,model="sing.3",k=2)
conf2
## p[1]=0.05 iter=1 t=-3.810157 e=0.01068678 r=0.08793868
## p[2]=0.95 iter=1 t=39.32669 e=0.001711107 r=0.09464663
## 0.05 0.95
## -3.810157 39.326686
conf3 <- sbconf(conf2,model="sing.3",k=3)
conf3
## plot diagnostics
plot(conf3) # AIC values for models v.s. test statistic value
plot(conf3,yval="zval",type="l") # corrected "k.3" z-value
stopCluster(cl)
# }
# NOT RUN {
# }
Run the code above in your browser using DataLab