Search code examples
rplm

PLM is not recognizing my id variable name


I'm doing a regression analysis considering fixed effects using plm() from package plm. I have selected the twoways method to account for both time and individual effects. However, after runing the below code I keep receiving this message:

Error in pdata.frame(data, index) : 
  variable id does not exist (individual index)

Here the code:

pdata <- DATABASE[,c(2:4,13:21)]
pdata$id <- group_indices(pdata,ISO3.p,Productcode)
coutnin <- dcast.data.table(pdata,ISO3.p+Productcode~.,value.var = "id")
setcolorder(pdata,neworder=c("id","Year"))
pdata <- pdata.frame(pdata,index=c("id","Year"))
reg <- plm(pdata,diff(TV,1) ~ diff(RERcp,1)+diff(GDPR.p,1)-diff(GDPR.r,1), effect="twoways", model="within", index = c("id","Year"))

Please mind that pdata structure shows that there are multiple levels in the id variable which is in numeric form, I tried initially to use a string type variable but I keep receiving the same outcome:

Classes ‘data.table’ and 'data.frame':  1211800 obs. of  13 variables:
 $ id         : int  4835 6050 13158 15247 17164 18401 19564 23553 24895 27541 ...
 $ Year       : int  1996 1996 1996 1996 1996 1996 1996 1996 1996 1996 ...
 $ Productcode: chr  "101" "101" "101" "101" ...
 $ ISO3.p     : Factor w/ 171 levels "ABW","AFG","AGO",..: 8 9 20 22 27 28 29 34 37 40 ...
 $ e          : num  0.245 -0.238 1.624 0.693 0.31 ...
 $ RERcp      : num  -0.14073 -0.16277 1.01262 0.03908 -0.00243 ...
 $ RERpp      : num  -0.1712 NA NA NA -0.0952 ...
 $ RER_GVC    : num  -3.44 NaN NA NA NaN ...
 $ GDPR.p     : num  27.5 26.6 23.5 20.3 27.8 ...
 $ GDPR.r     : num  30.4 30.4 30.4 30.4 30.4 ...
 $ GVCPos     : num  0.141 0.141 0.141 0.141 0.141 ...
 $ GVCPar     : num  0.436 0.436 0.436 0.436 0.436 ...
 $ TV         : num  17.1 17.1 17.1 17.1 17.1 ...
 - attr(*, ".internal.selfref")=<externalptr> 

When I convert the data.table into a pdata.frame I do not receive any warning, it happens only after I run the plm function. From running View(table(index(pdata), useNA = "ifany")) it displays no value larger than 1, therefore I assume I have no duplicates obs in my data.


Solution

  • Try to put the data argument at the second place in the plm statement. In case pdata has been converted to a pdata.frame already, leave out the index argument in the plm statement, i.e., try this:

    reg <- plm(diff(TV,1) ~ diff(RERcp,1)+diff(GDPR.p,1)-diff(GDPR.r,1), data = pdata, effect = "twoways", model = "within")