How to fit a multiple membership model when the factor is nested? The accepted answer to this question explains how to fit a mixed effects model where there is multiple membership. I would like to know how to proceed when the multiple membership factor itself is nested in another random factor.
 A: lmerMultiMember is an R package that implements multiple membership models in lme4. To use nested multiple membership factors in lmerMultiMember, you pre-generate an indicator matrix for the nested factor. For example, let's say we have a model where we try to predict journal article citations based on word count, while accounting each articles' authors and the scientific field the articles were published in. One possible (toy) model for this would be to nest authors in scientific fields, because authors don't necessarily have the same reputation in every field they publish in.
The usual formula for such a model might be lmer(citations ~ wordcount + (1 | author/field))
However, journal articles will have often have multiple authors, so in order to properly account for the authorship effects, we'll need a multiple membership model. Assuming a dataframe df with a single membership factor field and a multiple membership factor authors which contains the authors for each article, separated by commas, we specify the nested model as follows:
library(lmerMultiMember)

Wa <- weights_from_vector(df$authors, sep = ",")  # create multimembership matrix
Wf <- Matrix::fac2sparse(df$field)  # make single membership matrix using fac2sparse()
Waf <- interaction_weights(Wa, Wf)  # pre-generate interaction matrix

lmer(citations ~ wordcount + (1 | author) + (1 | authorXfield),
     memberships = list(author = Wa, authorXfield = Waf))

The interaction_weights() function creates an indicator matrix Waf as described in this answer, but for the interaction between two other indicator matrices, Wa (multimembership matrix for authors) and Wf (single membership matrix for scientific field). We just have to tell lmerMultiMember to use these indicator matrices by specifying the random effects using dummy (non-existent) variable names in the model formula, and then tell it which dummies map to which indicator matrices by passing a named list through the memberships argument.
lmerMultiMember is not yet on CRAN, but can be installed from https://github.com/jvparidon/lmerMultiMember
