datalorax / equatiomatic

Convert models to LaTeX equations
https://datalorax.github.io/equatiomatic/
Creative Commons Attribution 4.0 International
616 stars 43 forks source link

Error with interaction slope in lmer model #154

Closed JCruk closed 3 years ago

JCruk commented 3 years ago

I am trying to extract the equation for a model with an interaction term as slope, which results in an error:

Error in dimnames(mat) <- list(unique(unlist(rand_lev$vcov_greek[sd_rows])),  : 
  length of 'dimnames' [1] not equal to array extent

An example (it takes a long time to run):

# Make some data
Sim_Data <- tibble(
  expand_grid(
    Subject = factor(1:20),
    Cond1 = factor(letters[1:5]),
    Cond2 = factor( LETTERS[1:5]),
    Trial = factor(c(1:5))
  )
) %>%
  mutate(Rand = rnorm(n()))

# Fit model
sim1 <- lmer(Rand ~ Cond1 * Cond2 + (Cond1 + Cond2 + Cond1:Cond2|Subject), data = Sim_Data)

# Extract equation
extract_eq(sim1)

The equation is extracted as expected without the interaction term in the slope:

# Fit model
sim2 <- lmer(Rand ~ Cond1 * Cond2 + (Cond1 + Cond2|Subject), data = Sim_Data)

# Extract equation
extract_eq(sim2)
datalorax commented 3 years ago

Thanks, my guess is this is related to #143. Can you try installing from GitHub with remotes::install_github("datalorax/equatiomatic") and run it again? I'm trying to run it locally to test it but, like you said, the model is taking a long time.

datalorax commented 3 years ago

Just an update - the model finally converged and I did get the same error. I'll try to dig into this soon. That random effects variance-covariance matrix is massive. Theoretically it shouldn't matter so I'll try to track it down anyway, but... the equation is going to be pretty intractable.

datalorax commented 3 years ago

Also - I am wondering if it does relate to the size of the model (similar to #150) because this works without a problem

m <- lme4::lmer(math ~ minority*female + (minority*female|sch.id), equatiomatic::hsb)
#> boundary (singular) fit: see ?isSingular
equatiomatic::extract_eq(m)
#> Registered S3 method overwritten by 'broom.mixed':
#>   method      from 
#>   tidy.gamlss broom

Created on 2021-02-18 by the reprex package (v0.3.0)

JCruk commented 3 years ago

Just an update - the model finally converged and I did get the same error. I'll try to dig into this soon. That random effects variance-covariance matrix is massive. Theoretically it shouldn't matter so I'll try to track it down anyway, but... the equation is going to be pretty intractable.

Thanks!

I pulled the latest from github (with a force = TRUE) and refit with less levels in both conditions and it works fine, so it is likely due to the size of the model.

# Make some data
Sim_Data <- tibble(
  expand_grid(
    Subject = factor(1:20),
    Cond1 = factor(letters[1:3]),
    Cond2 = factor( LETTERS[1:2]),
    Trial = factor(c(1:5))
  )
) %>%
  mutate(Rand = rnorm(n()))

# Fit model
sim1 <- lmer(Rand ~ Cond1 * Cond2 + (Cond1 * Cond2|Subject), data = Sim_Data)

# Extract equation
extract_eq(sim1)

I imagine a two 5-level factor model with all interactions, everywhere, would be an infrequently encountered corner-case.

Thanks for this fantastic work!

datalorax commented 3 years ago

Great! I'd still like to know why it's not working, but since we already have one issue open related to the size of the models I think I'll close this one. Thanks for the report!