### Install lmeresampler development version from GitHub Source: https://github.com/aloy/lmeresampler/blob/master/README.md Installs the latest development version of lmeresampler from GitHub using devtools. Requires devtools; if not installed, it will be installed first. ```R if(!require(devtools)) install.packages("devtools") devtools::install_github("aloy/lmeresampler") ``` -------------------------------- ### Example: bootstrap_pvals.merMod with wild bootstrap Source: https://github.com/aloy/lmeresampler/blob/master/docs/reference/bootstrap_pvals.html Example usage of bootstrap_pvals.merMod with type = "wild", B = 1000, hccme = "hc2", and aux.dist = "mammen". Wrapped in if (FALSE) because it takes a while to run. ```r if (FALSE) { # This takes a while to run bootstrap_pvals.merMod(jsp_mod, type = "wild", B = 1000, hccme = "hc2", aux.dist = "mammen") } ``` -------------------------------- ### Run alternative bootstrap methods Source: https://github.com/aloy/lmeresampler/blob/master/docs/reference/bootstrap.html Examples for running case, residual, REB0, and wild bootstrap methods. ```R ## running a cases bootstrap - only resampling the schools boo2 <- bootstrap(model = vcmodA, .f = mySumm, type = "case", B = 100, resample = c(TRUE, FALSE)) ## running a cases bootstrap - resampling the schools and students within the school boo3 <- bootstrap(model = vcmodA, .f = mySumm, type = "case", B = 100, resample = c(TRUE, TRUE)) ## running a residual bootstrap boo4 <- bootstrap(model = vcmodA, .f = mySumm, type = "residual", B = 100) ## running an REB0 bootstrap boo5 <- bootstrap(model = vcmodA, .f = mySumm, type = "reb", B = 100, reb_typ = 0) ## Running the Wild bootstrap boo6 <- bootstrap(model = vcmodA, .f = mySumm, type = "wild", B= 100, hccme = "hc2", aux.dist = "mammen") ``` -------------------------------- ### Run wild bootstrap Source: https://github.com/aloy/lmeresampler/blob/master/man/bootstrap.Rd Runs a wild bootstrap with 100 iterations, using hccme = "hc2" and aux.dist = "mammen". This example is wrapped in \dontrun{} and is not executed during package checks. ```R ## Running the Wild bootstrap boo6 <- bootstrap(model = vcmodA, .f = mySumm, type = "wild", B= 100, hccme = "hc2", aux.dist = "mammen") ``` -------------------------------- ### Install lmeresampler from CRAN Source: https://github.com/aloy/lmeresampler/blob/master/README.md Installs the latest released version of the lmeresampler package from CRAN. ```R install.packages("lmeresampler") ``` -------------------------------- ### Calculate bootstrap p-values for a merMod object Source: https://github.com/aloy/lmeresampler/blob/master/man/bootstrap_pvals.Rd Example of running a wild bootstrap on a merMod object. Note that this operation can be time-consuming. ```R # This takes a while to run bootstrap_pvals.merMod(jsp_mod, type = "wild", B = 1000, hccme = "hc2", aux.dist = "mammen") ``` -------------------------------- ### Run REB0 bootstrap Source: https://github.com/aloy/lmeresampler/blob/master/man/bootstrap.Rd Runs a REB0 bootstrap with 100 iterations and reb_typ = 0. This example is wrapped in \dontrun{} and is not executed during package checks. ```R ## running an REB0 bootstrap boo5 <- bootstrap(model = vcmodA, .f = mySumm, type = "reb", B = 100, reb_typ = 0) ``` -------------------------------- ### Run case bootstrap with different resampling options Source: https://github.com/aloy/lmeresampler/blob/master/man/bootstrap.Rd Runs case bootstraps with 100 iterations, either resampling only schools or resampling both schools and students within schools. These examples are wrapped in \dontrun{} and are not executed during package checks. ```R ## running a cases bootstrap - only resampling the schools boo2 <- bootstrap(model = vcmodA, .f = mySumm, type = "case", B = 100, resample = c(TRUE, FALSE)) ``` ```R ## running a cases bootstrap - resampling the schools and students within the school boo3 <- bootstrap(model = vcmodA, .f = mySumm, type = "case", B = 100, resample = c(TRUE, TRUE)) ``` -------------------------------- ### CGR Semi-Parametric Bootstrap Examples Source: https://github.com/aloy/lmeresampler/blob/master/docs/articles/Vignette.html Examples of applying the CGR semi-parametric bootstrap algorithm to lme4 and nlme models. ```R # lme4 lmer_cgr_boot <- bootstrap(vcmodA, .f = fixef, type = "cgr", B = 100) # nlme lme_cgr_boot <- bootstrap(vcmodB, .f = fixef, type = "cgr", B = 100) ``` -------------------------------- ### Calculate confidence intervals with confint() Source: https://github.com/aloy/lmeresampler/blob/master/docs/articles/lmeresampler-vignette.html Examples of calculating confidence intervals for bootstrap objects, including default settings and custom method/level specifications. ```R # all ci types, 0.95 confidence level confint(lme_reb_boot2) #> # A tibble: 18 × 6 #> term estimate lower upper type level #> #> 1 beta.(Intercept) 14.2 12.8 15.5 norm 0.95 #> 2 beta.mathAge8 0.639 0.593 0.685 norm 0.95 #> 3 beta.genderM -0.357 -1.08 0.370 norm 0.95 #> 4 beta.classnonmanual 0.720 -0.0578 1.50 norm 0.95 #> 5 vc.(Intercept) 3.31 1.58 5.04 norm 0.95 #> 6 vc2 19.7 17.2 22.3 norm 0.95 #> 7 beta.(Intercept) 14.2 12.8 15.6 basic 0.95 #> 8 beta.mathAge8 0.639 0.592 0.684 basic 0.95 #> 9 beta.genderM -0.357 -1.18 0.302 basic 0.95 #> 10 beta.classnonmanual 0.720 -0.0567 1.45 basic 0.95 #> 11 vc.(Intercept) 3.31 1.59 4.88 basic 0.95 #> 12 vc2 19.7 17.2 21.9 basic 0.95 #> 13 beta.(Intercept) 14.2 12.7 15.5 perc 0.95 #> 14 beta.mathAge8 0.639 0.594 0.686 perc 0.95 #> 15 beta.genderM -0.357 -1.02 0.466 perc 0.95 #> 16 beta.classnonmanual 0.720 -0.00994 1.50 perc 0.95 #> 17 vc.(Intercept) 3.31 1.75 5.04 perc 0.95 #> 18 vc2 19.7 17.5 22.2 perc 0.95 # 90% percentile bootstrap intervals confint(lmer_reb_boot0, method = "perc", level= 0.90) #> # A tibble: 12 × 6 #> term estimate lower upper type level #> #> 1 (Intercept) 14.2 13.1 15.3 norm 0.9 #> 2 mathAge8 0.639 0.595 0.675 norm 0.9 #> 3 genderM -0.357 -0.829 0.209 norm 0.9 #> 4 classnonmanual 0.720 0.145 1.34 norm 0.9 #> 5 (Intercept) 14.2 13.1 15.3 basic 0.9 #> 6 mathAge8 0.639 0.599 0.674 basic 0.9 #> 7 genderM -0.357 -0.915 0.146 basic 0.9 #> 8 classnonmanual 0.720 0.237 1.33 basic 0.9 #> 9 (Intercept) 14.2 13.0 15.2 perc 0.9 #> 10 mathAge8 0.639 0.604 0.679 perc 0.9 #> 11 genderM -0.357 -0.861 0.201 perc 0.9 #> 12 classnonmanual 0.720 0.113 1.20 perc 0.9 ``` -------------------------------- ### Run residual bootstrap Source: https://github.com/aloy/lmeresampler/blob/master/man/bootstrap.Rd Runs a residual bootstrap with 100 iterations using the custom summary function. This example is wrapped in \dontrun{} and is not executed during package checks. ```R ## running a residual bootstrap boo4 <- bootstrap(model = vcmodA, .f = mySumm, type = "residual", B = 100) ``` -------------------------------- ### Perform REB Bootstrap with lme4 and nlme Source: https://github.com/aloy/lmeresampler/blob/master/vignettes/lmeresampler-vignette.Rmd Examples of using the bootstrap function with type = "reb" for both lme4 and nlme model objects, demonstrating the use of different reb_type variations. ```r # lme4 lmer_reb_boot0 <- bootstrap(vcmodA, .f = fixef, type = "reb", B = 100, reb_type = 0) lmer_reb_boot1 <- bootstrap(vcmodA, .f = fixef, type = "reb", B = 100, reb_type = 1) # nlme lme_reb_boot2 <- bootstrap(vcmodB, .f = fixef, type = "reb", B = 100, reb_type = 2) ``` -------------------------------- ### Perform Wild Bootstrap with lme4 and nlme Source: https://github.com/aloy/lmeresampler/blob/master/vignettes/lmeresampler-vignette.Rmd Examples of using the bootstrap function with type = "wild" for both lme4 and nlme model objects, specifying HCCME and auxiliary distribution parameters. ```r # lme4 lmer_wild_boot <- bootstrap(vcmodA, .f = fixef, type = "wild", hccme = "hc2", aux.dist = "mammen", B = 100) lmer_wild_boot <- bootstrap(vcmodA, .f = fixef, type = "wild", hccme = "hc3", aux.dist = "mammen", B = 100) # nlme lme_wild_boot <- bootstrap(vcmodB, .f = fixef, type = "wild", hccme = "hc2", aux.dist = "rademacher", B = 100) lme_wild_boot <- bootstrap(vcmodB, .f = fixef, type = "wild", hccme = "hc3", aux.dist = "rademacher", B = 100) ``` -------------------------------- ### R code: Example of computing sample size for a normal linear model Source: https://github.com/aloy/lmeresampler/blob/master/docs/reference/resid_bootstrap.html This example demonstrates how to use the power function to calculate the required sample size for a normal linear model. The function is adjusted to find the sample size for a given power. ```r power(u = 3, v = NULL, f2 = 0.3, sig.level = 0.05, power = 0.8) ``` -------------------------------- ### Load jsp728 dataset with data() Source: https://github.com/aloy/lmeresampler/blob/master/docs/reference/jsp728.html Loads the jsp728 dataset into the R environment. This is the standard way to access the data after installing the lmeresampler package. ```R data(jsp728) ``` -------------------------------- ### Parallelize bootstrap with lmeresampler Source: https://github.com/aloy/lmeresampler/blob/master/vignettes/lmeresampler-vignette.Rmd Demonstrates how to set up a PSOCK cluster and use foreach with %dopar% to parallelize the bootstrap function. ```r library(purrr) library(foreach) library(doParallel) set.seed(1234) numCores <- 2 cl <- makeCluster(numCores, type = "PSOCK") # make a socket cluster doParallel::registerDoParallel(cl) # how the CPU knows to run in parallel clusterCall(cl, function(x) .libPaths(x), .libPaths()) b_parallel2 <- foreach(B = rep(250, 2), .combine = combine_lmeresamp, .packages = c("lmeresampler", "lme4")) %dopar% { bootstrap(vcmodA, .f = fixef, type = "parametric", B = B) } stopCluster(cl) ``` -------------------------------- ### Run bootstrap in parallel via foreach Source: https://github.com/aloy/lmeresampler/blob/master/docs/reference/bootstrap.html Configures a parallel socket cluster and uses foreach to combine bootstrap results. ```R ## Running a bootstrap in parallel via foreach library(foreach) library(doParallel) set.seed(1234) numCores <- 2 cl <- makeCluster(numCores, type = "PSOCK") # make a socket cluster doParallel::registerDoParallel(cl) # how the CPU knows to run in parallel b_parallel <- foreach(B = rep(250, 2), .combine = combine_lmeresamp, .packages = c("lmeresampler", "lme4")) %dopar% { bootstrap(vcmodA, .f = fixef, type = "parametric", B = B) } stopCluster(cl) ``` -------------------------------- ### Run a parametric bootstrap Source: https://github.com/aloy/lmeresampler/blob/master/docs/reference/bootstrap.html Demonstrates loading lme4, defining a custom summary function, and executing a parametric bootstrap with 20 resamples. ```R library(lme4) #> Loading required package: MatrixvcmodA <- lmer(mathAge11 ~ mathAge8 + gender + class + (1 | school), data = jsp728) ## you can write your own function to return stats, or use something like 'fixef' mySumm <- function(.) { s <- getME(., "sigma") c(beta = getME(., "beta"), sigma = s, sig01 = unname(s * getME(., "theta"))) } ## running a parametric bootstrap set.seed(1234) boo1 <- bootstrap(model = vcmodA, .f = mySumm, type = "parametric", B = 20) ## to print results in a formatted way print(boo1) #> Bootstrap type: parametric #> #> Number of resamples: 20 #> #> term observed rep.mean se bias #> 1 beta1 14.1577509 14.1949464 0.66538125 0.0371954794 #> 2 beta2 0.6388895 0.6385629 0.02608992 -0.0003265699 #> 3 beta3 -0.3571922 -0.4561735 0.35477238 -0.0989812789 #> 4 beta4 0.7200815 0.7927683 0.31179264 0.0726868125 #> 5 sigma 4.4416279 4.4036722 0.11963706 -0.0379557056 #> 6 sig01 1.8199553 1.7894322 0.23985803 -0.0305230950 #> #> There were 0 messages, 0 warnings, and 0 errors. ``` -------------------------------- ### reb_bootstrap() Source: https://github.com/aloy/lmeresampler/blob/master/docs/reference/index.html REB Bootstrap for Two-Level Nested LMEs. ```APIDOC ## reb_bootstrap() ### Description REB Bootstrap for Two-Level Nested LMEs. ### Usage reb_bootstrap() ``` -------------------------------- ### Parallelized bootstrap execution with foreach Source: https://github.com/aloy/lmeresampler/blob/master/docs/articles/lmeresampler-vignette.html Sets up a socket cluster and uses foreach with combine_lmeresamp to perform parallelized bootstrap resampling. ```R library(purrr) library(foreach) library(doParallel) set.seed(1234) numCores <- 2 cl <- makeCluster(numCores, type = "PSOCK") # make a socket cluster doParallel::registerDoParallel(cl) # how the CPU knows to run in parallel clusterCall(cl, function(x) .libPaths(x), .libPaths()) #> [[1]] #> [1] "/private/var/folders/p0/kzl82ys143d1wd2kfj24jm_8000pww/T/RtmpVRsEB5/temp_libpathe7816248d8c" #> [2] "/Library/Frameworks/R.framework/Versions/4.1/Resources/library" #> #> [[2]] #> [1] "/private/var/folders/p0/kzl82ys143d1wd2kfj24jm_8000pww/T/RtmpVRsEB5/temp_libpathe7816248d8c" #> [2] "/Library/Frameworks/R.framework/Versions/4.1/Resources/library" b_parallel2 <- foreach(B = rep(250, 2), .combine = combine_lmeresamp, .packages = c("lmeresampler", "lme4")) %dopar% { bootstrap(vcmodA, .f = fixef, type = "parametric", B = B) } stopCluster(cl) ``` -------------------------------- ### parametric_bootstrap() Source: https://github.com/aloy/lmeresampler/blob/master/docs/reference/index.html Parametric Bootstrap for Nested LMEs. ```APIDOC ## parametric_bootstrap() ### Description Parametric Bootstrap for Nested LMEs. ### Usage parametric_bootstrap() ``` -------------------------------- ### case_bootstrap() Source: https://github.com/aloy/lmeresampler/blob/master/docs/reference/index.html Cases Bootstrap for Nested LMEs. ```APIDOC ## case_bootstrap() ### Description Cases Bootstrap for Nested LMEs. ### Usage case_bootstrap() ``` -------------------------------- ### case_bootstrap.lme Source: https://github.com/aloy/lmeresampler/blob/master/revdep/library.noindex/lmeresampler/new/lmeresampler/html/00Index.html Case bootstrap for objects of class 'lme'. ```APIDOC ## case_bootstrap.lme ### Description Case bootstrap for objects of class 'lme'. ### Usage ## S3 method for class 'lme' case_bootstrap(model, .f, B, resample, ...) ### Arguments - **model**: An object of class 'lme'. - **.f**: A function to apply to the model. - **B**: Number of bootstrap replicates. - **resample**: The resampling method. - **...**: Additional arguments. ### Value An object of class "lmeresamp". ### Examples \dontrun{ case_bootstrap(lme_model, .f = fixef, B = 1000, resample = "within") } ``` -------------------------------- ### Residual bootstrap with nlme model Source: https://github.com/aloy/lmeresampler/blob/master/docs/articles/Vignette.html Use bootstrap() with type = 'residual' to perform a residual bootstrap on a fitted nlme model (vcmodB). The example uses B = 100 resamples and extracts fixed effects via fixef. ```R # nlme lme_res_boot <- bootstrap(vcmodB, .f = fixef, type = "residual", B = 100) ``` -------------------------------- ### wild_bootstrap() Source: https://github.com/aloy/lmeresampler/blob/master/docs/reference/index.html Wild Bootstrap for LME models. ```APIDOC ## wild_bootstrap() ### Description Wild Bootstrap for LME models. ### Usage wild_bootstrap() ``` -------------------------------- ### R code: Using the power function for a normal linear model Source: https://github.com/aloy/lmeresampler/blob/master/docs/reference/resid_bootstrap.html This snippet shows the basic usage of the power function to compute the statistical power. The example uses a simple model with a normal distribution and a linear relation. ```r power(u = 3, v = 20, f2 = 0.3, sig.level = 0.05) ``` -------------------------------- ### reb_bootstrap.lme Source: https://github.com/aloy/lmeresampler/blob/master/revdep/library.noindex/lmeresampler/new/lmeresampler/html/00Index.html REB bootstrap for objects of class 'lme'. ```APIDOC ## reb_bootstrap.lme ### Description REB bootstrap for objects of class 'lme'. ### Usage ## S3 method for class 'lme' reb_bootstrap(model, .f, B, resample, ...) ### Arguments - **model**: An object of class 'lme'. - **.f**: A function to apply to the model. - **B**: Number of bootstrap replicates. - **resample**: The resampling method. - **...**: Additional arguments. ### Value An object of class "lmeresamp". ### Examples \dontrun{ reb_bootstrap(lme_model, .f = fixef, B = 1000, resample = "within") } ``` -------------------------------- ### Fit example models with lme4 and nlme Source: https://github.com/aloy/lmeresampler/blob/master/docs/articles/Vignette.html Fits two-level linear mixed-effects models using the jsp728 data set. vcmodA is fit with lme4's lmer() and vcmodB with nlme's lme(). Both predict mathAge11 with fixed effects for mathAge8, gender, and class, and a random intercept for school. ```r library(lme4) vcmodA <- lmer(mathAge11 ~ mathAge8 + gender + class + (1 | school), data = jsp728) library(nlme) vcmodB <- lme(mathAge11 ~ mathAge8 + gender + class, random = ~1|school, data = jsp728) ``` -------------------------------- ### Parallel bootstrap with parLapply Source: https://github.com/aloy/lmeresampler/blob/master/man/bootstrap.Rd Sets up a PSOCK cluster, registers parallel backend, defines a bootstrap function that fits a mixed model and runs a parametric bootstrap, executes it in parallel, and combines results. ```R cl <- makeCluster(numCores, type = "PSOCK") # make a socket cluster doParallel::registerDoParallel(cl) # how the CPU knows to run in parallel boot_mod <- function(...) { library(lme4) library(lmeresampler) vcmodA <- lmer(mathAge11 ~ mathAge8 + gender + class + (1 | school), data = jsp728) bootstrap(vcmodA, .f = fixef, type = "parametric", B = 250) } result <- parLapply(cl, seq_len(2), boot_mod) b_parallel2 <- do.call("combine_lmeresamp", result) stopCluster(cl) ``` -------------------------------- ### print() Source: https://github.com/aloy/lmeresampler/blob/master/docs/reference/index.html Print a summary of an lmeresamp object. ```APIDOC ## print() ### Description Print a summary of an `lmeresamp` object. ### Usage print() ``` -------------------------------- ### case_bootstrap.merMod Source: https://github.com/aloy/lmeresampler/blob/master/revdep/library.noindex/lmeresampler/new/lmeresampler/html/00Index.html Case bootstrap for objects of class 'merMod'. ```APIDOC ## case_bootstrap.merMod ### Description Case bootstrap for objects of class 'merMod'. ### Usage ## S3 method for class 'merMod' case_bootstrap(model, .f, B, resample, ...) ### Arguments - **model**: An object of class 'merMod'. - **.f**: A function to apply to the model. - **B**: Number of bootstrap replicates. - **resample**: The resampling method. - **...**: Additional arguments. ### Value An object of class "lmeresamp". ### Examples \dontrun{ case_bootstrap(lmer_model, .f = fixef, B = 1000, resample = "within") } ``` -------------------------------- ### bootstrap.lme Source: https://github.com/aloy/lmeresampler/blob/master/revdep/library.noindex/lmeresampler/new/lmeresampler/html/00Index.html Bootstrap method for objects of class 'lme'. ```APIDOC ## bootstrap.lme ### Description Bootstrap method for objects of class 'lme'. ### Usage ## S3 method for class 'lme' bootstrap(model, .f, type, resample, ...) ### Arguments - **model**: An object of class 'lme'. - **.f**: A function to apply to the model. - **type**: The type of bootstrap. - **resample**: The resampling method. - **...**: Additional arguments. ### Value An object of class "lmeresamp". ### Examples \dontrun{ bootstrap(lme_model, .f = fixef, type = "parametric", resample = "within") } ``` -------------------------------- ### print.lmeresamp(x, ci = FALSE, ...) Source: https://github.com/aloy/lmeresampler/blob/master/man/print.Rd Prints a summary of an lmeresamp object. Optionally includes confidence intervals when ci is TRUE. ```APIDOC ## print.lmeresamp(x, ci = FALSE, ...) ### Description Prints summary statistics and confidence intervals, if desired, for an lmeresamp object. ### Method print method for lmeresamp objects ### Usage print(x, ci = FALSE, ...) ### Parameters - **x** (lmeresamp) - Required - The lmeresamp object to print. - **ci** (logical) - Optional - A logical value specifying whether confidence intervals should be printed. Default is FALSE. - **...** - Optional - Not used. ### Details If the bootstrap statistics are stored in a vector (as opposed to a data frame or tibble), then summary statistics will be calculated and printed. The printed data frame will include the name of the term (if applicable), the observed value (observed), the mean of the bootstrap replicated (rep.mean), the standard error (se), and the bootstrap bias estimate (bias). In addition, the number of resamples will be printed. If any messages, warnings, or errors were generated during the bootstrap procedure, they will be summarized below, and you should check the message, warning, and error elements of the lmeresamp object to investigate further. ``` -------------------------------- ### summary() Source: https://github.com/aloy/lmeresampler/blob/master/docs/reference/index.html Calculate summary statistics for lmeresamp objects. ```APIDOC ## summary() ### Description Calculate summary statistics for `lmeresamp` objects. ### Usage summary() ``` -------------------------------- ### parametric_bootstrap.lme Source: https://github.com/aloy/lmeresampler/blob/master/revdep/library.noindex/lmeresampler/new/lmeresampler/html/00Index.html Parametric bootstrap for objects of class 'lme'. ```APIDOC ## parametric_bootstrap.lme ### Description Parametric bootstrap for objects of class 'lme'. ### Usage ## S3 method for class 'lme' parametric_bootstrap(model, .f, B, resample, ...) ### Arguments - **model**: An object of class 'lme'. - **.f**: A function to apply to the model. - **B**: Number of bootstrap replicates. - **resample**: The resampling method. - **...**: Additional arguments. ### Value An object of class "lmeresamp". ### Examples \dontrun{ parametric_bootstrap(lme_model, .f = fixef, B = 1000, resample = "within") } ``` -------------------------------- ### Compare bootstrap runtime with and without parallelization Source: https://github.com/aloy/lmeresampler/blob/master/vignettes/lmeresampler-vignette.Rmd Compares the execution time of a sequential bootstrap call against a parallelized version using system.time. ```r system.time(b_nopar <- bootstrap(vcmodA, .f = fixef, type = "parametric", B = 5000)) numCores <- 2 cl <- makeCluster(numCores, type = "PSOCK") doParallel::registerDoParallel(cl) clusterCall(cl, function(x) .libPaths(x), .libPaths()) system.time({ b_parallel2 <- foreach(B = rep(2500, 2), .combine = combine_lmeresamp, .packages = c("lmeresampler", "lme4", "purrr", "dplyr")) %dopar% { bootstrap(vcmodA, .f = fixef, type = "parametric", B = B) } }) stopCluster(cl) ``` -------------------------------- ### Run bootstrap in parallel via parLapply Source: https://github.com/aloy/lmeresampler/blob/master/docs/reference/bootstrap.html Uses parLapply to distribute bootstrap tasks across a cluster. ```R ## Running a bootstrap in parallel via parLapply cl <- makeCluster(numCores, type = "PSOCK") # make a socket cluster doParallel::registerDoParallel(cl) # how the CPU knows to run in parallel boot_mod <- function(...) { library(lme4) library(lmeresampler) vcmodA <- lmer(mathAge11 ~ mathAge8 + gender + class + (1 | school), data = jsp728) bootstrap(vcmodA, .f = fixef, type = "parametric", B = 250) } result <- parLapply(cl, seq_len(2), boot_mod) b_parallel2 <- do.call("combine_lmeresamp", result) stopCluster(cl) ``` -------------------------------- ### Print lmeresamp object Source: https://github.com/aloy/lmeresampler/blob/master/docs/articles/Vignette.html Displays the summary of a bootstrap object returned by the bootstrap() function. ```R print(lmer_reb_boot0) #> Bootstrap type: reb0 #> #> Number of resamples: 100 #> #> observed rep.mean se bias #> (Intercept) 14.1577509 14.0769408 0.81106998 -0.0808101564 #> mathAge8 0.6388895 0.6395461 0.02724457 0.0006565948 #> genderM -0.3571922 -0.4409426 0.38588213 -0.0837504079 #> classnonmanual 0.7200815 0.7021238 0.43128316 -0.0179576564 ``` -------------------------------- ### Define custom summary function and run parametric bootstrap Source: https://github.com/aloy/lmeresampler/blob/master/man/bootstrap.Rd Defines a custom summary function mySumm that extracts fixed effects, sigma, and random effects from a fitted model, then runs a parametric bootstrap with 20 iterations. The print function displays the results in a formatted way. ```R ## you can write your own function to return stats, or use something like 'fixef' mySumm <- function(.) { s <- getME(., "sigma") c(beta = getME(., "beta"), sigma = s, sig01 = unname(s * getME(., "theta"))) } ## running a parametric bootstrap set.seed(1234) boo1 <- bootstrap(model = vcmodA, .f = mySumm, type = "parametric", B = 20) ## to print results in a formatted way print(boo1) ``` -------------------------------- ### Load lmeresampler package Source: https://github.com/aloy/lmeresampler/blob/master/vignettes/lmeresampler-vignette.Rmd Loads the lmeresampler package into the R session. ```r ```{r setup} library(lmeresampler) ``` ``` -------------------------------- ### Print bootstrap object summary Source: https://github.com/aloy/lmeresampler/blob/master/docs/articles/lmeresampler-vignette.html Displays the summary of a bootstrap object created by the bootstrap() function. ```r print(lmer_reb_boot0) #> Bootstrap type: reb0 #> #> Number of resamples: 100 #> #> term observed rep.mean se bias #> 1 (Intercept) 14.1577509 14.1106714 0.69124112 -0.047079508 #> 2 mathAge8 0.6388895 0.6428107 0.02426523 0.003921188 #> 3 genderM -0.3571922 -0.4043930 0.31541703 -0.047200785 #> 4 classnonmanual 0.7200815 0.6974058 0.36313295 -0.022675666 #> #> There were 0 messages, 0 warnings, and 0 errors. ``` -------------------------------- ### combine_lmeresamp(...) Source: https://github.com/aloy/lmeresampler/blob/master/docs/reference/combine.html Combines bootstrap results from processes split for parallelization. Takes two or more lmeresamp objects and returns a single combined lmeresamp object with unified output and bootstrap statistics. ```APIDOC ## combine_lmeresamp(...) ### Description Combines bootstrap results from processes split for parallelization to yield unified output and bootstrap statistics. ### Usage combine_lmeresamp(...) ### Arguments - **...** - two or more objects of class `lmeresamp`, to be combined into one. ### Details This helper function combines the different processes split for parallelization to yield unified output and bootstrap statistics. ### Value A single object of class `lmeresamp` containing the combined bootstrap results. ``` -------------------------------- ### wild_bootstrap.merMod Source: https://github.com/aloy/lmeresampler/blob/master/revdep/library.noindex/lmeresampler/new/lmeresampler/html/00Index.html Wild bootstrap for objects of class 'merMod'. ```APIDOC ## wild_bootstrap.merMod ### Description Wild bootstrap for objects of class 'merMod'. ### Usage ## S3 method for class 'merMod' wild_bootstrap(model, .f, B, resample, ...) ### Arguments - **model**: An object of class 'merMod'. - **.f**: A function to apply to the model. - **B**: Number of bootstrap replicates. - **resample**: The resampling method. - **...**: Additional arguments. ### Value An object of class "lmeresamp". ### Examples \dontrun{ wild_bootstrap(lmer_model, .f = fixef, B = 1000, resample = "within") } ``` -------------------------------- ### bootstrap() Source: https://github.com/aloy/lmeresampler/blob/master/docs/reference/index.html Bootstrap nested linear mixed-effects models. ```APIDOC ## bootstrap() ### Description Bootstrap Nested Linear Mixed-Effects Models. ### Usage bootstrap() ``` -------------------------------- ### parametric_bootstrap.merMod Source: https://github.com/aloy/lmeresampler/blob/master/revdep/library.noindex/lmeresampler/new/lmeresampler/html/00Index.html Parametric bootstrap for objects of class 'merMod'. ```APIDOC ## parametric_bootstrap.merMod ### Description Parametric bootstrap for objects of class 'merMod'. ### Usage ## S3 method for class 'merMod' parametric_bootstrap(model, .f, B, resample, ...) ### Arguments - **model**: An object of class 'merMod'. - **.f**: A function to apply to the model. - **B**: Number of bootstrap replicates. - **resample**: The resampling method. - **...**: Additional arguments. ### Value An object of class "lmeresamp". ### Examples \dontrun{ parametric_bootstrap(lmer_model, .f = fixef, B = 1000, resample = "within") } ``` -------------------------------- ### Print lmeresamp object Source: https://github.com/aloy/lmeresampler/blob/master/vignettes/lmeresampler-vignette.Rmd Prints information about the bootstrap call, with an optional ci argument to include confidence intervals. ```R print(lmer_reb_boot0) print(lme_reb_boot2, ci = TRUE) ``` -------------------------------- ### S3 method signature for summary.lmeresamp Source: https://github.com/aloy/lmeresampler/blob/master/docs/reference/summary.html Usage signature for the summary method on lmeresamp objects. The object argument is the lmeresamp object to be summarized; the ... argument is not used. ```R # S3 method for lmeresamp summary(object, ...) ``` -------------------------------- ### parametric_bootstrap(model, .f, B, .refit = TRUE) Source: https://github.com/aloy/lmeresampler/blob/master/docs/reference/parametric_bootstrap.html Generate parametric bootstrap replicates of a statistic for a nested linear mixed-effects model. This function simulates bootstrap samples from the estimated distribution functions and returns an object of class 'lmeresamp'. ```APIDOC ## parametric_bootstrap(model, .f, B, .refit = TRUE) ### Description Generate parametric bootstrap replicates of a statistic for a nested linear mixed-effects model. ### Usage S3 method for merMod: parametric_bootstrap(model, .f, B, .refit = TRUE) S3 method for lme: parametric_bootstrap(model, .f, B, .refit = TRUE) Generic: parametric_bootstrap(model, .f, B, .refit = TRUE) ### Arguments - **model** - The model object you wish to bootstrap. - **.f** - A function returning the statistic(s) of interest. - **B** - The number of bootstrap resamples. - **.refit** - A logical value indicating whether the model should be refit to the bootstrap resample, or if the simulated bootstrap resample should be returned. Defaults to TRUE. ### Value The returned value is an object of class "lmeresamp". ### Details The parametric bootstrap simulates bootstrap samples from the estimated distribution functions. That is, error terms and random effects are simulated from their estimated normal distributions and are combined into bootstrap samples via the fitted model equation. ### References Chambers, R. and Chandra, H. (2013) A random effect block bootstrap for clustered data. Journal of Computational and Graphical Statistics, 22, 452--470. Van der Leeden, R., Meijer, E. and Busing F. M. (2008) Resampling multilevel models. In J. de Leeuw and E. Meijer, editors, Handbook of Multilevel Analysis, pages 401--433. New York: Springer. ### See Also Examples are given in bootstrap.merMod. Also see resid_bootstrap, case_bootstrap, reb_bootstrap, wild_bootstrap for more details on specific bootstraps. See bootMer in the lme4 package for an implementation of (semi-)parametric bootstrap for mixed models. ``` -------------------------------- ### summary(object, ...) Source: https://github.com/aloy/lmeresampler/blob/master/docs/reference/summary.html S3 method for lmeresamp objects. Calculates basic summary statistics such as the mean, standard error, and bias of the bootstrap replicates. ```APIDOC ## summary(object, ...) ### Description Calculate basic summary statistics such as the mean, standard error, and bias of the bootstrap replicates. ### Usage ```r # S3 method for lmeresamp summary(object, ...) ``` ### Arguments - **object** - The lmeresamp object to be summarized. - **...** - not used ### Details If the bootstrap statistics are stored in a vector (as opposed to a data frame or tibble), then summary statistics will be calculated. The printed data frame will include the name of the term (if applicable), the observed value (`observed`), the mean of the bootstrap replicated (`rep.mean`), the standard error (`se`), and the bootstrap bias estimate (`bias`). In addition, the number of resamples will be printed. If any messages, warnings, or errors were generated during the bootstrap procedure, they will be summarized below, and you should check the `message`, `warning`, and `error` elements of the `lmeresamp` object to investigate further. ``` -------------------------------- ### reb_bootstrap.merMod Source: https://github.com/aloy/lmeresampler/blob/master/revdep/library.noindex/lmeresampler/new/lmeresampler/html/00Index.html REB bootstrap for objects of class 'merMod'. ```APIDOC ## reb_bootstrap.merMod ### Description REB bootstrap for objects of class 'merMod'. ### Usage ## S3 method for class 'merMod' reb_bootstrap(model, .f, B, resample, ...) ### Arguments - **model**: An object of class 'merMod'. - **.f**: A function to apply to the model. - **B**: Number of bootstrap replicates. - **resample**: The resampling method. - **...**: Additional arguments. ### Value An object of class "lmeresamp". ### Examples \dontrun{ reb_bootstrap(lmer_model, .f = fixef, B = 1000, resample = "within") } ``` -------------------------------- ### wild_bootstrap(model, .f, B, hccme, aux.dist, .refit = TRUE) Source: https://github.com/aloy/lmeresampler/blob/master/docs/reference/wild_bootstrap.html Generate wild bootstrap replicates of a statistic for a linear mixed-effects model. Supports lmerMod and lme model objects. ```APIDOC ## wild_bootstrap(model, .f, B, hccme, aux.dist, .refit = TRUE) ### Description Generate wild bootstrap replicates of a statistic for a linear mixed-effects model. ### Usage # S3 method for lmerMod wild_bootstrap( model, .f, B, hccme = c("hc2", "hc3"), aux.dist = c("mammen", "rademacher", "norm", "webb", "gamma"), .refit = TRUE ) # S3 method for lme wild_bootstrap( model, .f, B, hccme = c("hc2", "hc3"), aux.dist = c("mammen", "rademacher", "norm", "webb", "gamma"), .refit = TRUE ) wild_bootstrap(model, .f, B, hccme, aux.dist, .refit = TRUE) ### Arguments - **model** - The model object you wish to bootstrap. - **.f** - A function returning the statistic(s) of interest. - **B** - The number of bootstrap resamples. - **hccme** - either "hc2" or "hc3", indicating which heteroscedasticity consistent covariance matrix estimator to use. - **aux.dist** - one of "mammen", "rademacher", "norm", "webb", or "gamma" indicating which auxiliary distribution to draw the errors from. - **.refit** - a logical value indicating whether the model should be refit to the bootstrap resample, or if the simulated bootstrap resample should be returned. Defaults to TRUE. ### Value The returned value is an object of class "lmeresamp". ### Details The wild bootstrap algorithm for LMEs implemented here was outlined by Modugno & Giannerini (2015). The algorithm is outlined below: 1. Draw a random sample equal to the number of groups (clusters) from an auxillary distribution with mean zero and unit variance. Denote these as w_1, ..., w_g. 2. Calculate the selected heteroscedasticity consistent matrix estimator for the marginal residuals, v_i. 3. Generate bootstrap responses using the fitted equation: y_i* = X_i * beta + v_i * w_j. 4. Refit the model and extract the statistic(s) of interest. 5. Repeat steps 2-4 B times. ### References Modugno, L., & Giannerini, S. (2015). The Wild Bootstrap for Multilevel Models. Communications in Statistics -- Theory and Methods, 44(22), 4812--4825. ### See Also - Examples are given in bootstrap - parametric_bootstrap, resid_bootstrap, case_bootstrap, reb_bootstrap, wild_bootstrap for more details on a specific bootstrap. - bootMer in the lme4 package for an implementation of (semi-)parametric bootstrap for mixed models. ``` -------------------------------- ### Print lmeresamp object with confidence intervals Source: https://github.com/aloy/lmeresampler/blob/master/docs/articles/Vignette.html Displays the summary of a bootstrap object including confidence intervals by setting ci = TRUE. ```R print(lme_reb_boot2, ci = TRUE) #> Bootstrap type: reb2 #> #> Number of resamples: 100 #> #> observed rep.mean se bias #> beta.(Intercept) 14.1577509 14.1577509 0.74474925 0 #> beta.mathAge8 0.6388895 0.6388895 0.02556231 0 #> beta.genderM -0.3571922 -0.3571922 0.36349159 0 #> beta.classnonmanual 0.7200815 0.7200815 0.39700106 0 #> sigma.(Intercept) 3.3122380 3.3122380 0.62752568 0 #> sigma2 19.7280582 19.7280582 1.51845206 0 #> #> 95 basic interval: #> basic.lower basic.upper #> beta.(Intercept) 12.8440930 15.7497846 #> beta.mathAge8 0.5868947 0.6811430 #> beta.genderM -0.9118761 0.3587842 #> beta.classnonmanual -0.1117500 1.4513645 #> sigma.(Intercept) 2.1188934 4.5182002 #> sigma2 16.7484212 22.5134118 #> #> 95 percent normal-t interval: #> norm.t.lower norm.t.upper #> (Intercept) 12.71765712 15.5978447 #> mathAge8 0.58892933 0.6888497 #> genderM -1.02495574 0.3105713 #> classnonmanual -0.03972045 1.4798834 #> sd((Intercept)) 1.36162176 2.4325683 #> sigma 4.21190797 4.6838768 #> #> 95 percent bootstrap-t interval: #> boot.t.lower boot.t.upper #> (Intercept) 14.1577509 14.1577509 #> mathAge8 0.6388895 0.6388895 #> genderM -0.3571922 -0.3571922 #> classnonmanual 0.7200815 0.7200815 #> #> 95 percent percentile-t interval: #> perc.t.lower perc.t.upper #> beta.(Intercept) 12.56571727 15.4714088 #> beta.mathAge8 0.59663599 0.6908843 #> beta.genderM -1.07316860 0.1974917 #> beta.classnonmanual -0.01120156 1.5519130 #> sigma.(Intercept) 2.10627572 4.5055825 #> sigma2 16.94270452 22.7076952 ``` -------------------------------- ### Residual bootstrap with lme4 and nlme models Source: https://github.com/aloy/lmeresampler/blob/master/vignettes/lmeresampler-vignette.Rmd Shows how to perform the residual bootstrap using the bootstrap() function with type = "residual". The algorithm involves rescaling error terms and EBLUPs, sampling with replacement, and refitting the model B times. ```R # lme4 lmer_res_boot <- bootstrap(vcmodA, .f = fixef, type = "residual", B = 100) # nlme lme_res_boot <- bootstrap(vcmodB, .f = fixef, type = "residual", B = 100) ```