Skip to content

fixef(): keep digit-only labels as text when sorting - #705

Merged
kylebutts merged 1 commit into
lrberge:masterfrom
finite-sample:fixef-label-zeros
Nov 16, 2026
Merged

kylebutts merged 1 commit into
lrberge:masterfrom
finite-sample:fixef-label-zeros

Conversation

@soodoku

@soodoku soodoku commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

Closes #674.

With sorted = TRUE (the default), fixef() sorts labels made only of digits by replacing them with as.numeric(labels), so "00001" comes back as "1":

https://github.com/lrberge/fixest/blob/cf946133/R/methods.R#L1366

The names then no longer match the data, and code that maps data to fixef() names fails silently. In the #674 example, sparse_model_matrix(type = "fixef") leaves every unit column empty:

library(fixest)
ul = sprintf("%05d", 1:10)
d = data.frame(y = rnorm(100), time = rep(1:10, each = 10), unit = rep(ul, times = 10))
est = feols(y ~ 0 | time + unit, data = d)
head(names(fixef(est)$unit), 3)
#> master: "1" "2" "3"        this PR: "00001" "00002" "00003"
sum(sparse_model_matrix(est, data = d, type = "fixef"))
#> master: 100                this PR: 190

The labels are still ordered as numbers, so the order is unchanged. They now keep their original text. Fixed effects stored as numbers already had names "1", "2", ... and are unaffected.

Tests, added to the FIXEF chunk:

  • zero-padded labels keep their text, sorted and unsorted;
  • sparse_model_matrix has one 1 per row;
  • numeric fixed effects keep their names.

The first test fails on master. tests/fixest_tests.R runs to completion on this branch and merged with #703 and #704.

🤖 Generated with Claude Code

With sorted = TRUE, fixef() replaced fixed-effect labels made only of digits
by as.numeric(labels) to sort them, so "00001" came back as "1". The names
then no longer matched the data, and anything that maps data to fixef()
names failed silently: sparse_model_matrix(type = "fixef") left every unit
column empty (sum 100 instead of 190 in the lrberge#674 example).

Sort such labels as numbers but keep their original text. The order is
unchanged; fixed effects stored as numbers were already "1", "2", ... and
are unaffected.

Closes lrberge#674.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kylebutts

Copy link
Copy Markdown
Collaborator

@lrberge the code looks good to me; maybe no need for the test?

@kylebutts
kylebutts merged commit d1dfd53 into lrberge:master Nov 16, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Character fixed effects coerced to numeric, causing post-estimation mistakes

2 participants