R: plm — year fixed effects — year and quarter dat

2020-07-08 06:54发布

问题:

I am having a problem setting up a panel data model.

Here is some sample data:

library(plm)

id <- c(1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2)
year <- c(1999,1999,1999,1999,2000,2000,2000,2000,1999,1999,1999,1999,2000,2000,2000,2000)
qtr <- c(1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4)
y <- rnorm(16, mean=0, sd=1)
x <- rnorm(16, mean=0, sd=1)

data <- data.frame(id=id,year=year,qtr=qtr,y_q=paste(year,qtr,sep="_"),y=y,x=x)

I run the following regression using 'id' as the individual index and 'year' as the time index:

reg1 <- plm(y ~ x, data=data,index=c("id", "year"), model="within",effect="time")

Unfortunately, I get the following error:

duplicate couples (time-id) Error in pdim.default(index[[1]], index[[2]]) :

So to get around that, I use the combined variable that is 'y_q':

reg1 <- plm(y ~ x, data=data,index=c("id", "y_q"), model="within",effect="time")

But here's my issue -- I only want to have year fixed effects and not year-quarter.

Is there another way to get around the earlier issue instead of making the tiem index 'y_q'?

Thanks ahead of time for any help!

回答1:

In a panel setting, you usually don't have some duplicate value for each couple id-year.

In your quaterly data it will be difficult to compute a year fixed effect models without aggregating your data to make them yearly.

Check the examples here to see how your data should be formatted for panel data modeling.

Here is oneway to do that :

require(plyr)
yeardata  <- ddply(data, .(year, id), summarize, y = mean(y),
                                                 x = mean(x))


require(plm)
reg1 <- plm(y ~ x, data = yeardata, index = c("id", "year"), model = "within", effect = "time")
fixef(reg1)

##      1999      2000 
## 0.2641997 0.0041193