lrberge / fixest

Fixed-effects estimations
https://lrberge.github.io/fixest/
361 stars 59 forks source link

demean with data.table doesn't work when using a formula #433

Closed pbaylis closed 5 months ago

pbaylis commented 11 months ago

Hi there, I often use data.table and fixest together and have run across this a few times: seems like the very-convenient formula approach to demean doesn't work on data.tables. MWE:

library(data.table)
library(fixest)

data(trade)
trade_dt = as.data.table(trade)

demean(dist_km ~ Origin, data = trade) # Works
demean(dist_km ~ Origin, data = trade_dt) # Error: "Error in `[.data.table`(data, , dataNames %in% linear.varnames, FALSE) : The items in the 'by' or 'keyby' list are length(s) (1). Each must be length 38325; the same length as there are rows in x (after subsetting if i is provided)."

Versions are fixest_0.11.1 and data.table_1.14.8, running on OS X. Also tested on Ubuntu, same issue. Obviously there are some very easy work-arounds (convert to a data.frame or use the non-formula approach) but I figured it might be worth knowing.

kylebutts commented 11 months ago

Thanks for flagging @pbaylis! I opened a PR with fixes #435, but it might be a while to change before Laurent reviews

lrberge commented 10 months ago

it might be a while to change before Laurent reviews

Alas truth hurts!

Thanks Kyle for the fix and Patrick for the report! I cannot imagine this bug has been lying there for so long! I'll try to have a look soon.

kylebutts commented 10 months ago

@lrberge Did not mean that as an insult at all!! The more I work with this code, the more impressive it becomes! You've done enough FOSS work for a lifetime already ;)

lrberge commented 5 months ago

Thanks for your patience! Now should be fixed!