我正在嘗試在多個時間段之間進行“經典”差異。我想做的模型是:
y = a b1x1 b2_treat b3_period b_4(treat*period) u (eq.1)
所以基本上我測驗不同的設定只是為了確保我以正確的方式指定我的模型,使用不同的包。我想使用 fixst 包,所以我嘗試將估計值與標準 lm() 包的估計值進行比較。然而,結果不同——系數和標準錯誤。
我的問題是:
- lm_mod、lm_mod2 或 feols_mod 回歸是否正確指定(如 eq.1)?
如果沒有,如果有人能告訴我如何在 lm() 中獲得與 feols() 相同的結果,我將不勝感激!
# libraries
library(fixest)
library(modelsummary)
library(tidyverse)
# load data
data(base_did)
# make df for lm_mod with 5 as the reference-period
base_ref_5 <- base_did %>%
mutate(period = as.factor(period)) %>%
mutate(period = relevel(period, ref = 5))
# Notice that i use base_ref_5 for the lm model and base_did for the feol_mod.
lm_mod <- lm(y ~ x1 treat*period, base_ref_5)
lm_mod2 <- lm(y ~ x1 treat period treat*period, base_ref_5)
feols_mod <- feols(y ~ x1 i(period, treat, ref = 5), base_did)
# compare models
models <- list("lm" = lm_mod,
"lm2" = lm_mod2,
"feols" = feols_mod)
msummary(models, stars = T)
**EDIT:**
the reason why I created base_ref_5 was so that both regressions would have period 5 as the reference period, if that was unclear.
**EDIT 2**:
added a third model (lm_mod2) which is much closer, but there is still a difference.
uj5u.com熱心網友回復:
這里有兩個問題。
- 在
lm()模型中,period變數是相互作用的,但被視為連續的數字變數。相比之下,呼叫i(period, treat)將period視為一個因素(檔案中對此進行了清楚的解釋)。 - 該
i()函式僅包括相互作用,而不包括本構項。
這里有兩個模型來說明相似之處:
library(fixest)
data(base_did)
lm_mod <- lm(y ~ x1 factor(period) * factor(treat), base_did)
feols_mod <- feols(y ~ x1 factor(period) i(period, treat), base_did)
coef(lm_mod)["x1"]
#> x1
#> 0.9799697
coef(feols_mod)["x1"]
#> x1
#> 0.9799697
請注意,我只回答了您關于lm和之間的相似之處的部分問題feols。StackOverflow 是一個編程問答網站。如果您對統計模型的正確規范有疑問,您可能想在 CrossValidated 上提問。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/347580.html
下一篇:如何在R中對這些資料進行排序
