2 元 FE 模型在 R 中提供"empty model"



我正在尝试使用plm在R中运行一个双向固定效果模型。我正试图获得一个事件对市政当局公民投票的处理效果。

我想运行具有市政和公民投票固定效果的模型。每一排或每一个单元都是一对市政公民投票。

我正在尝试使用以下方法来拟合模型:

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是一个数字对象,municipality.code是一个因素,ref.code也是。

有人能帮我吗?谢谢

实现@Heli123上面讨论的另一种方法是为市政当局x公投固定效果创建一个新的虚拟变量。

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

然后,您可以使用新的muni_x_ref变量将prest制成表格。下面的前几行。你可以看到,对于市政当局x公投的每个固定效应,你只有一个自变量的实现。你的fe和自变量是完全共线的,没有变化来估计你的模型。我认为你需要重新思考你想要估计的是什么。

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::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

我发现这篇文章很有用,所以我会尽我所能提供信息,但它实际上是对小插曲的总结,应该会指导任何使用plm的人。注意,在plm((中没有包含为模型提供的输入,这显然是有问题的。

给定(如您所写(,

#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")) 

我们应该循序渐进,确保我们符合"p…"的必要格式对于plm((:

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")

对我来说,当我在个人应用程序中使用它时,在update.frame中进行索引是有益的,因为我可以节省很多时间。

最新更新