JuliaStats / MixedModels.jl

A Julia package for fitting (statistical) mixed-effects models
http://juliastats.org/MixedModels.jl/stable
MIT License
402 stars 47 forks source link

Multi-membership models #330

Open dhalpern opened 4 years ago

dhalpern commented 4 years ago

This is following up on a post on the Julia discourse board. In several mixed modeling packages in R, it is possible to fit multi-membership models where each row in a data matrix might belong to several groups that are captured by a single random effect(e.g., the mm function in brms ). This functionality is useful for network models like the social relations models where you might be trying to predict whether two people are friends based on shared characteristics. You would also want to include random effects for each person (since some people might be friendlier than others) so you might set up a model as y ~ x + (1 | person_1) + (1 | person_2). However, it doesn't matter whether a person appears as person_1 or person_2 so you want to constrain the random effects to be symmetric across both columns.

For a frequentist example, Ben Bolker has shown how to implement it in lme4 by editing the random effects matrix prior to fitting. This is a request to implement the same functionality in MixedModels in order to take advantage of the speed relative to lme4. @dmbates pointed out that it should be possible to do something similar by editing the A matrix in allterms. and then running updateL! before fitting.

I'm not much of a Julia programmer but here is an attempt at some simulated data for the model I described above:

x = randn(100)
b = 1
REs = randn(10)
person_1 = repeat(1:10, inner=10)
person_2 = repeat(1:10, 10)
y = a + b * x + REs[person_1] + REs[person_2]
dmbates commented 4 years ago

Your example would have some cases where person_1 and person_2 are the same. Did you intend this?

dhalpern commented 4 years ago

Yes, sorry to just include that without comment. I realize that doesn't make sense necessarily with the friends example I gave but I think there might be use cases where you want to compare something with itself (e.g., at a later time). For instance, in one of my research projects, we measure the similarity of fMRI BOLD data from two presentations of the same item (at different times) and presentations of different items. We have random effects for each of the items and then a fixed effect for whether it's the same item to see whether it's more similar than we might have expected from presentations of different items.

palday commented 3 years ago

As @dmbates indicated in his Discourse comment, this isn't a trivial fix, but I started working on it a few weeks ago. I haven't figured out a good formula syntax yet, but I tried constructing the Z matrices as done in Ben Bolker's example. The problem is that some of the algorithmic advances in MixedModels.jl (relative to lme4) depend on sparsity patterns that no longer hold in multimembership models (e.g., diagonal and block diagonal), which means I need to do a fair amount of work adding in extra linear algebra methods. Ultimately, I may decide to spin these efforts off into a different package, but I'm not sure when I'll have more time to work on this.