Quick Usage Guide

Colman Humphrey

2019-09-02

glmmboot: Quick Use

For even quicker usage instructions, see the README.

The general idea of this package is that you can throw nearly any model built in the typical R fashion. For now, we just need a somewhat standard way of extracting fixed effects, and that update works, which is nearly always the case.

Assuming your model is called base_model, if coef(summary(base_model)) gives the estimates and standard errors, the method will work. The result of coef is often a list; this package will then produce estimates for each element of the list. Common examples where there are multiple sets of coefficients are zero-inflated models.

The data should be supplied manually, although it’s currently the case that if the dataframe that creates the model is accessible from base_model$model, base_model$frame or base_model@frame, you won’t have to send the data in to the function separately (but you will get a warning). Be careful here - some models supply this but the method introduces bugs. Much safer to supply the data explicitly.

Standard Example

Let’s work through an example with glmmTMB. We’ll use the data that comes with glmmboot.

We’re assuming the x_var variables are fixed, and subj is to be treated as a random effect.

Thus our base analysis is:

We get a warning because the outcome data is proportional. Not to worry.

Now we’ll use the bootstrap. By default it’ll perform block bootstrapping over the highest entropy random effect - but there’s only one, so of course the winner is subj.

For publications etc, better to run about ten thousand resamples to avoid noise having much of an effect. Of course 99 is far too small, only for an example.

And comparing results:

Combining Runs

The above might take a long time in a real setting. If it takes far too long on your machine, you can ideally run it on a bunch of computers. We don’t want each computer to output the fully processed output, only the intermediate outcome. To do this, we set return_coefs_instead = TRUE for each run:

Combining this is simple enough. If we’ve used a few, we don’t want to mess around with even more lists, so we can enter them into the relevant function:

If we’ve run a huge number of such runs, ideally we’ll combine all output to a list of lists, like so:

And we’ll get the same result:

You MUST set return_coefs_instead = TRUE for methods like these that combine output.

Parallel Options

There are two primary ways to run models in parallel:

  1. parallel: this will use parallel::mclapply to run the models.
  2. future: this will use future.apply::future_lapply to run the models

Use he parameter parallelism in the function bootstrap_model() to choose how to parallelise.

parallelism = "none"

This is the default, and will run each model sequentially, using lapply(). If you set num_cores to a number greater than 1, you’ll get an error with this option.

This isn’t the default if you set num_cores to a value greater than 1.

parallelism = "parallel"

This will use parallel::mclapply to run the models. If you set num_cores, then that many cores will be used. If you don’t set num_cores, the function will use num_cores = parallel::detectCores() - 1.

Example:

I’ve heard that parallel::mclapply doesn’t play well on Windows. Instead of implementing parallel::parLapply (which I will do if there’s interest), I would recommend using parallelism = "future", see below.

This becomes the default if you don’t set parallelism but just num_cores.

parallelism = "future"

This uses the very nice future.apply::future_lapply function to run the models in parallel. Note that you MUST set the future::plan (see the docs for the future and future.apply packages) to actually make use of multiple cores etc. Using num_cores is NOT the right way to set the backend, and will cause an error.

This should work well with Windows.

Example:

Only setting num_cores

If you don’t touch the parallelism argument, but just set num_cores, e.g.:

It’ll be as if you set parallelism = "parallel", and will use the parallel::mclapply backend (unless you set num_cores = 1!).

Progress Bars Etc

Unfortunately currently none of these methods have any progress bars setup. There used to be a call to a parallel package that did this, but it seemed to have some bugs. Hopefully this will be possible in the future.

The (not great) options for now: * run a small set to estimate timing before running a large set of models * using the split approach (see the Combining Runs approach above), print out some intermediate output between runs.

Let’s look at an example of the second approach, using the future argument. First, we’ll write a somewhat rough logging function:

This looks like:

As before, don’t forget return_coefs_instead = TRUE. We’d run:

Of course with 50 blocks of size 100, we’d get the equivalent of 5000 samples.

Zero Inflated

Let’s first reuse the model from the glmmTMB vignettes:

We run this more or less the same way as the other models:

In this case the output would be a list with two entries, one matrix in zero_boot$cond, the typical conditional model, and another matrix (in this case a single row, because ziformula = ~1) in zero_cond$zi, each giving the same shape output as we’ve seen above.

In this particular model, the bootstrap model is quite conservative. Assumptions are often useful!