Skip to content

predict() fails for split fixest models with non-regular FEs (“factor level [...] is duplicated”) #620

@francescodelprato

Description

@francescodelprato

Hey Laurent,

thanks for the great package. I am using fixest 0.13.2 under R 4.5.1.

Here's the problem: predict() on a split model (fsplit=~female) with multi-way FEs errors with “factor level [...] is duplicated” when the FEs are non-regular (after singleton removal). The main (all-gender, in my case) submodel predicts fine; the gender submodels fail because predict.fixest rebuilds factors from duplicated fixef_names.

Here's a minimal reproducible example:

library(fixest)

packageVersion("fixest")  # 0.13.2 for me

set.seed(0)
N <- 200; G <- 10
dt <- data.frame(
  id     = sample(1:50, N, TRUE),
  firm   = sample(1:G, N, TRUE),
  year   = sample(1:5, N, TRUE),
  female = sample(0:1, N, TRUE),
  x      = rnorm(N)
)
dt$y <- rnorm(N)

mod <- feols(y ~ x | id + firm + year, dt, fsplit = ~ female)
# NOTE: 3 fixed-effect singletons were removed; FEs flagged as not regular.

predict(mod[[1]], newdata = dt, fixef = FALSE)  # works
predict(mod[[2]], newdata = dt, fixef = FALSE)  # error: factor level [...] is duplicated

This raises

Error in `levels<-`(`*tmp*`, value = as.character(levels)) : 
  factor level [2] is duplicated

while predict(mod[[2]], ...) should return the linear predictor (or NA for new levels) like the full-sample model.

A minimal workaround is to compute manual Xb's to bypass predict’s factor rebuild:

compute_xb <- function(model, data) {
  mm <- model.matrix(model, data = data, type = "rhs")
  as.numeric(mm %*% coef(model))
}

xb_all   <- compute_xb(mod[[1]], dt)
xb_male  <- compute_xb(mod[[2]], dt[dt$female == 0, ])
xb_fem   <- compute_xb(mod[[3]], dt[dt$female == 1, ])

Thanks for your work!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions