Repository navigation
Conversation
|
@grantmcdermott this is related to your issues, right? Do you mind reviewing this and see if it fixes your problems? |
Thanks. I'll try to take a look by the end of the week. |
|
@soodoku two problems I found.
m = feols(mpg ~ hp + i(cyl), mtcars)
model.matrix(m, data = mtcars["hp"], subset = TRUE)
m = feols(mpg ~ hp + i(cyl, factor(am), ref = 4), mtcars)
model.matrix(m, data = mtcars)
|
|
BTW, the goal of this PR matches fit <- lm(mpg ~ hp + factor(cyl):factor(am), data = mtcars)
model.matrix(fit, data = mtcars[1:5, ])But it is not clear what to do in this situation where new levels exist in the new dataset: fit <- lm(mpg ~ hp + factor(cyl):factor(am), data = mtcars[mtcars$cyl %in% c(4, 6), ])
model.matrix(fit, data = mtcars)
#> Error in `model.frame.default()`:
#> ! factor factor(cyl) has new levels 8
library(fixest)
fit <- feols(mpg ~ hp + i(cyl, i.am), data = mtcars[mtcars$cyl %in% c(4, 6), ])
head(model.matrix(fit, data = mtcars))
#> (Intercept) hp cyl::4:am::1 cyl::6:am::0 cyl::6:am::1 cyl::8:am::0
#> [1,] 1 110 0 0 1 0
#> [2,] 1 110 0 0 1 0
#> [3,] 1 93 1 0 0 0
#> [4,] 1 110 0 1 0 0
#> [5,] 1 175 0 0 0 1
#> cyl::8:am::1
#> [1,] 0
#> [2,] 0
#> [3,] 0
#> [4,] 0
#> [5,] 0 |
With new data, model.matrix() built bare i() terms with the lowest level present in that data as the reference, not the reference used at estimation. When the estimation reference was absent, a column for an estimated coefficient was silently dropped and X %*% coef() disagreed with predict(). For feols(mpg ~ hp + i(cyl), mtcars) on subset(mtcars, cyl != 4) the columns were (Intercept), hp, cyl::8 against coefficients (Intercept), hp, cyl::6, cyl::8. As predict() already does, build the i() columns with i_noref = TRUE on new data and keep those that were estimated. An estimated level with no row in the new data is now a column of zeros, so the columns match coef() in that case too (for example i(cyl, ref = 6) or i(cyl, hp) on data without cyl == 4). Calls without new data are unchanged. Closes lrberge#687. Diagnosis and the i_noref suggestion by @grantmcdermott in that issue. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5654bb2 to
8c3a9e2
Compare
|
Thanks for catching both of those, @kylebutts — rebased on
|
Closes #687. This follows the diagnosis and the
i_norefsuggestion in that issue.On new data,
model.matrix()builds a barei()term with the lowest level present in that data as the reference. If the estimation reference is absent, a column for an estimated coefficient is silently dropped:What the PR does. On new data,
model.matrix()builds thei()columns withi_noref = TRUE, aspredict()already does, and keeps those that were estimated.One step beyond the issue, which you may want to reject. An estimated level with no row in the new data now comes back as a column of zeros. Examples are
i(cyl, ref = 6)ori(cyl, hp)on data withoutcyl == 4. On master those columns are missing, soX %*% coef(m)fails or misaligns. The values are exact, since the indicator is identically zero on those rows. Without this, the columns still would not matchcoef()in that case. If you'd rather keep the PR to the reference-level case only, I can drop that part.Calls without new data take the same path as before.
Tests, added to the
Model matrixchunk:coef()andX %*% coefequalspredict(), with the reference absent;i(cyl, hp)cases, with the zero column.The first test fails on master.
tests/fixest_tests.Rruns to completion on this branch and merged with #703 and #705.🤖 Generated with Claude Code