R 未按data.table中的组使用felm()正确分配IDX

R 未按data.table中的组使用felm()正确分配IDX,r,data.table,R,Data.table,看起来lfe包中的felm函数中可能有一个bug,但我在这里发布这篇文章是想看看这是否是我思维中的一个错误,或者是在data.table中信息的可用性方面的一个冲突 下面是一个简单的例子,我们将lm()的解决方案与felm()的解决方案进行比较 #测试LFE包输出不正确的IDX值。 要求(数据表) 要求(lfe) #圆柱回归模型的求解。 dt为了完整性,您可能希望显示它在data.table之外正常工作,lappy(split(dt,dt$cyl),function(d)getfe(felm(m

看起来
lfe
包中的
felm
函数中可能有一个bug,但我在这里发布这篇文章是想看看这是否是我思维中的一个错误,或者是在
data.table
中信息的可用性方面的一个冲突

下面是一个简单的例子,我们将
lm()
的解决方案与
felm()
的解决方案进行比较

#测试LFE包输出不正确的IDX值。
要求(数据表)
要求(lfe)
#圆柱回归模型的求解。

dt为了完整性,您可能希望显示它在data.table之外正常工作,
lappy(split(dt,dt$cyl),function(d)getfe(felm(mpg~wt | gear,data=d))
vs
lappy(split(dt,dt$cyl),function(d)lm(mpg~0+wt+factor(gear),data=d)$coef)
。我想,提交一份bug报告可能比在这里提问要好。谢谢@Frank,我不知道
split
。看起来像一个
数据表
问题。提交错误报告。
# Testing LFE package output incorrect IDX values.
require(data.table)
require(lfe)

# Solutions of regression models by cylinder.
dt <- data.table(mtcars)
sol <- dt[, list(model=list(lm(mpg ~ 0 + wt + factor(gear)))), by=cyl]
sol2 <- dt[, list(model=list(felm(mpg ~ wt | gear, .SD))), by=cyl] # using felm()

# Get coefficients from both models.
coef <- sol[, list(coef=coef(model[[1]]),
                   idx=names(coef(model[[1]]))), keyby=cyl]
coef2 <- sol2[, getfe(model[[1]]), keyby=cyl]

# Visually compare values for different gears.
coef[idx%like%"gear"]
#    cyl     coef           idx
# 1:   4 36.14931 factor(gear)3
# 2:   4 41.05802 factor(gear)4
# 3:   4 39.05476 factor(gear)5
# 4:   6 32.89790 factor(gear)3
# 5:   6 31.93766 factor(gear)4
# 6:   6 30.61227 factor(gear)5
# 7:   8 25.03750 factor(gear)3
# 8:   8 23.60107 factor(gear)5

coef2
#    cyl   effect obs comp   fe idx
# 1:   4 36.14931   1    1 gear   3
# 2:   4 41.05802   8    1 gear   4
# 3:   4 39.05476   2    1 gear   5
# 4:   6 32.89790   2    1 gear   3
# 5:   6 31.93766   4    1 gear   4
# 6:   6 30.61227   1    1 gear   5
# 7:   8 25.03750  12    1 gear   3
# 8:   8 23.60107   2    1 gear   4 # should be 5!