1

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.

nick
  • 1,090
  • 1
  • 11
  • 24
1412mm
  • 55
  • 7
  • See if this post helps you at all https://stackoverflow.com/questions/51796317/r-predict-glm-fit-on-each-column-in-data-frame-using-column-index-number/51810814#51810814 – AndS. Aug 18 '18 at 21:28
  • 1
    I'm confused about why you would want to do the regression for each group in a fixed effects framework--it doesn't make sense to me. Once you get rid of all the firms but one, you get rid of the fixed effects! `plm` won't (and can't) do this and will give an error. You can change to `model = "pooling"` to get the pooled OLS, that will work. – paqmo Aug 18 '18 at 21:28

1 Answers1

2

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
Robert
  • 5,038
  • 1
  • 25
  • 43