繁体   English   中英

2 路 FE model 在 R 中给出“空模型”

[英]2-way FE model gives “empty model” in R

我正在尝试使用 plm 在 R 中运行 2 路固定效果 model。 我正试图获得一个事件对市政公投投票的处理效果。

我想运行具有自治市和公投固定效果的 model。 每一行或单元是一对市政*公投。

我正在尝试使用以下方法安装 model:

model_2fe <- plm(vote.ant.immig ~ pre.post, data = clean.df, effect = "twoways", index = c("municipality.code", "ref.code")) 

我不断得到以下信息:

Error in plm.fit(data, model, effect, random.method, random.models, random.dfcor, : empty model

如果有帮助:pre.post 是指示治疗条件的因素 (1,0),vote.ant.immig 是一个数字 object,市政当局.code 是一个因素,ref.code 也是一个因素。

谁能帮帮我? 谢谢!

使用?plm::detect.lindep中的提示,您可以看到双向固定效果转换后的数据是什么样子。 这里有几行代码可以让你到达那里:

dat <- pdata.frame(<inputdata>, index = c("municipalitycode", "refcode"))

fml <- voteantimmig ~ prepost
mf <- model.frame(dat, fml)
modmat_2FE <- model.matrix(mf, model = "within", effect = "twoways")
all(abs(modmat_2FE) < 0.0000000000001) # TRUE
## look at your transformed data in modmat_2FE -> all zero -> empty model

## Analyse why this is the case, per individual, per time period:
modmat_FEi <- model.matrix(mf, model = "within", effect = "individual")
all(abs(modmat_FEi) < 0.0000000000001) # FALSE
## look at your transformed data in modmat_FEi
unique(modmat_FEi) # not so many different values
## look at your transformed data in modmat_FEi with individual and time index next to it: 
modmat_FEiindexes <- cbind(modmat_FEi, dat$municipalitycode, dat$refcode)
## => not much variation left within each individual. 

modmat_FEt <- model.matrix(mf, model = "within", effect = "time")
all(abs(modmat_FEt) < 0.0000000000001) # TRUE
## look at your transformed data in modmat_FEt -> all zero

完成上述@Helix123 讨论的另一种方法是为市政 x 公投固定效应创建一个新的虚拟变量。

clean.df$muni_x_ref <- factor(paste(clean.df$municipality, clean.df$refcode, sep='X'))

然后,您可以使用新的muni_x_ref变量将 prepost 制成表格。 下面的前几行。 您会看到,对于每个自治市 x 公投固定效应,您只有一个自变量实现。 您的 fe 和自变量是完全共线的,并且没有变化来估计您的 model。 我相信您需要重新考虑要估算的内容。

 table(clean.df$muni_x_refcode, df$prepost)


                            0 1
  AdlikonX5240              1 0
  AdlikonX5250              1 0
  AdlikonX5320              1 0
  AdlikonX5470              1 0
  AdlikonX5521              1 0
  AdlikonX5522              1 0
  AdlikonX5710              1 0
  AdlikonX5800              0 1
  AdlikonX5880              0 1
  AdlikonX5970              0 1
  AdlikonX6040              0 1
  AdlikonX6090              0 1
  Aeugst am AlbisX5240      1 0
  Aeugst am AlbisX5250      1 0
  Aeugst am AlbisX5320      1 0
  Aeugst am AlbisX5470      1 0
  Aeugst am AlbisX5521      1 0
  Aeugst am AlbisX5522      1 0
  Aeugst am AlbisX5710      1 0
  Aeugst am AlbisX5800      0 1
  Aeugst am AlbisX5880      0 1
  Aeugst am AlbisX5970      0 1
  Aeugst am AlbisX6040      0 1
  Aeugst am AlbisX6090      0 1
  Affoltern am AlbisX5240   1 0
  Affoltern am AlbisX5250   1 0
  Affoltern am AlbisX5320   1 0
  Affoltern am AlbisX5470   1 0
  Affoltern am AlbisX5521   1 0
  Affoltern am AlbisX5522   1 0
  Affoltern am AlbisX5710   1 0 ....

我发现这篇文章很有用,所以我会提供我能提供的信息,但它实际上是一个小插图的摘要,应该指导任何使用 plm 的人。 请注意,您没有在 plm() 中包含为 model 提供的输入,这显然是有问题的。

鉴于(如您所写),

#note, no model input for plm()
model_2fe <- plm(vote.ant.immig ~ pre.post,
                 data = clean.df, 
                 effect = "twoways", 
                 index = c("municipality.code", "ref.code")) 

我们应该一步一步地进行,并确保我们适合 plm() 的必要格式 'p...':

pform <- pFormula(vote.ant.immig ~ pre.post)
#then make the pdata.frame 
pclean.df <- pdata.frame(clean.df,index = c("municipalitycode", "refcode"))
#then make the df with necessary variables for/from pform (the formula) 
pmf.clean.df <- model.frame(pclean.df,pform)
#then make the design/model matrix "e.g., by expanding factors to a set of dummy variables (depending on the contrasts) and expanding interactions similarly" (quote from ?model.matrix).
pmodmat.fe <- model.matrix(pform,data=pmf.clean.df,  model = "within",effect = "twoways") 
#then check for linear dependence
detect.lindep(pmodmat.fe)
#lastly, run the regression with fixed effects 
mod.fe <- plm(pform,
               data=pclean.df, 
               model="within",
               effects="twoways")

对我来说,当我在个人应用程序中使用它时,在 pdata.frame 中建立索引是有益的,因为我可以节省大量时间。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM