小组分组回归

时间:2018-08-18 20:35:51

标签: r plm

我搜索了很多问题,但是没有一个人在使用plm

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

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

我想对每个企业集团使用此面板回归。 我尝试了by中的group bydplyrlmList,但是它们都不适用于plm。由于回归中存在滞后项,因此必须使用plm回归。

1 个答案:

答案 0 :(得分:1)

假设您有一个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