按组面板回归

Panel regression by group

我搜索了很多问题,但其中 none 个问题使用的是 plm

我有一个公司年面板数据dt,每个公司都有不同的年份观察。我有一个面板回归:

plm(y~ lag(x1, 1)+ lag(x2, 1)+ lag(log(x3), 1),
data= dt, model= "within", effect= "twoways", index= c("firm", "fyear"))

我想对每个公司组使用此面板回归。 我在 dplyrlmList 中尝试了 bygroup by,但其中 none 在 plm 上有效。因为我的回归中有滞后项,所以我必须使用 plm 回归。

假设您有一个 group 列:

library(plm)
#get data
data("Produc", package = "plm")
dt=Produc[,c("state","year","gsp","pcap","pc","emp","region")]
colnames(dt)=c("firm","fyear","y","x1","x2","x3","group") #your names + group
plm(y~ lag(x1, 1)+ lag(x2, 1)+ lag(log(x3), 1),
    data= dt, model= "within", effect= "twoways", index= c("firm", "fyear"))
#Model Formula: y ~ lag(x1, 1) + lag(x2, 1) + lag(log(x3), 1)
#
#Coefficients:
#     lag(x1, 1)      lag(x2, 1) lag(log(x3), 1) 
#       -0.50586         0.88671     12417.08080 

# split and lapply
dts=split(dt,dt$group)
regs=lapply(dts,function(dtt)plm(y~ lag(x1, 1)+ lag(x2, 1)+ lag(log(x3), 1),
                            data= dtt, model= "within", effect= "twoways", 
                            index= c("firm", "fyear"))$coefficients)
do.call("rbind",regs)

# > do.call("rbind",regs)
# lag(x1, 1) lag(x2, 1) lag(log(x3), 1)
# 1 -1.51064158 1.96541347        8163.974
# 2 -0.06268382 0.95112243      110801.043
# 3  1.06379297 0.06485576       63507.876
# 4  1.10813773 0.56661881        7429.956
# 5  0.90277939 0.52108922       46308.862
# 6  2.38345950 0.36220682       73134.118
# 7  2.68155543 0.31143095      -23427.304
# 8  1.94446802 0.22973996        5196.212
# 9  1.35110639 1.25191285      -82916.241