简体   繁体   中英

Panel regression by group

I have searched many questions, but none of them is using plm .

I have a firm-year panel data dt , each firm have different year observations. I have a panel regression:

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

I want to use this panel regression for each firm group. I tried by , group by in dplyr , and lmList , but none of them works on plm . Because there is lagged terms in my regression, I must use plm regression.

Assuming you have a group column:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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