Search code examples
rlme4random-effectsglmm

How to run a GLMM with Crossed Independent Random Effects


I need to run a Mixed Effects Model but I am stuck because my data is more complicated than what I am used to running. Here is an example of my data frame. This is fake data, but it gets the point across just fine.

My fake data:

df <- data.frame(subject = c("Matt", "Matt", "Matt", "Matt", "Tom", 
    "Tom", "Tom", "Tom", "Sarah", "Sarah", "Sarah", "Sarah"), partner = c("Tom", 
    "Sarah", "Tom", "Sarah", "Matt", "Sarah", "Matt", "Sarah", "Matt", 
    "Tom", "Matt", "Tom"), closeness_score = c(100, 76, 100, 76, 100, 12, 100, 
    12, 76, 42, 76, 12), condition = c("control", "control", "experimental", 
    "experimental", "control", "control", "experimental", "experimental", 
    "control", "control", "experimental", "experimental"), donations = c(9, 
    1, 15, 4, 15, 0, 10, 2, 1, 1, 1, 1), total_donations = c(10, 10, 20, 
    20, 15, 15, 12, 12, 2, 2, 3, 3))

This data is from a group-level experiment where at any point a subject could donate an item to either partner. The number of total donations within a session is designated under total_donations and the number of donations that specifically went to that partner is designated under donations.

I have three subjects, and in any given session a subject could give to either partner. I am trying to determine if closeness_score and condition can predict how many donations went to that partner compared to the other partner.

I am very familiar with the R package lme4 and assume I am going to be using that for this data to run a GLMM. I also found a paper linked here that goes over a similar example, but a lot of it went over my head.

Does anyone have suggestions or code to run this kind of model? Ultimately I am trying to find out if closeness_score and condition predicts how often a subject pulls for each partner.


Solution

  • I think as a start

    glmer(donations/total_donations ~ closeness_score*condition + 
                                      (1|subject) + (1|partner), 
          data = df, 
          family = binomial, 
          weights = total_donations)
    

    should get you fairly close. You don't have a session variable in your example data set, you could also add (1|session). (I assume that your subjects each participate in multiple sessions.)

    In principle you could consider whether different people respond to closeness score or condition differently (i.e. random effect terms of the form (closeness_score|subject) or (closeness_score*condition|subject), but you're quite likely to run out of data/have trouble fitting these models ...

    You should remember to check for overdispersion.