outer.boot.func <- function(sp, defree = 10, Nrep = 399, indexfile) { # outer.boot.func: # outer function for bootstrapping. # Calls the function bootstrap.func that will compute a # bootstrap resample, fit the GAM and find the indices. # # NOTE: the BATCH file bootstrap.in probably requires less memory than # this function and is probably preferable. # # sp is the dataframe with columns headed "site", "year", "count", # and no missing values. # Nrep is number of bootstrap replicates; defree is degrees of freedom # for the GAM fit. Indexfile is the name of a file in the working # directory (see below): it should be in quotes (" "). # # Example: # cb.bootind.399.10 <- outer.boot.func(cb, 10, 399, "cb.bootind.399.10") # # NOTE: # The file indexfile is a safeguard against premature termination of the # bootstrap routine: all results are written to this file as the # function proceeds. If the program crashes, the early results can be # salvaged by: # sp.bootind.crash <- matrix(scan("indexfile"), nrow = k, byrow = T) # where k is the number of replicates completed before the crash. # The remaining replicates can then be made up by restarting the # oter.boot.func function. # # ** IMPORTANT NOTE ** # When restarting any Splus simulation after a crash, be sure to reset the # random seed before resuming: otherwise the previous results (before the # crash) will just be repeated. The easiest way to reset the seed is # simply to generate some new random numbers: e.g. runif(100). # So (e.g.) if the procedure crashed after replicate 50 out of 399, the # full run and salvage operation would be something like: # cb.bootind.399.10 <- outer.boot.func(cb, 10, 399,"cb.results") # (this crashes and the object cb.bootind.399.10 is never formed) # cb.bootind.crash50 <- matrix(scan("cb.results"), nrow=50, byrow=T) # runif(100) # cb.bootind.last349 <- outer.boot.func(cb, 10, 349, "cb.results2") # cb.bootind.399.10 <- rbind(cb.bootind.crash50, cb.bootind.last349) # # # START OF FUNCTION # some housekeeping jobs first: if(!is.character(indexfile)) stop( "filenames have to be character strings") # # the dataset sp is assumed to have NO MISSING ENTRIES: if(length(sp$count[is.na(sp$count)]) > 0) stop( "no missing data allowed") # # start bootstrap loop: assign("sp", sp, frame = 1) assign("defree", defree, frame = 1) assign("indexfile", indexfile, frame = 1) replicate.func <- function(i) { print(i) rep.indices <- bootstrap.func(sp, defree = defree) write(rep.indices, file = indexfile, ncolumns = 4, append = T) rep.indices } index.matrix <- sapply(1:Nrep, replicate.func) matrix(unlist(index.matrix), byrow = T, nrow = Nrep) }