RoyalTS
RoyalTS

Reputation: 10203

panel regression with non-individual-specific fixed effects

I have weekly observations of revenues from the sale of different products, separately for different countries, like so:

df <- data.frame(year=rep(c(2002,2003), each=16),
             week=rep(1:4,4),
             product=rep(c('A','B'), each=8, times=2),
             country=rep(c('usa','germany'), each=4, times=4),
             revenue=abs(rnorm(32)))

That means observations of revenues are only unique for a combination of year-week-country-product

I would now like to estimate a model that includes fixed effects for the interaction of country and year and for each product but cannot figure out how to do this:

Are there any ways of estimating this with plm or are there other packages which do this sort of thing? I know I could demean the data within the groups described above, estimate via lm and then do a df-correction, but I'd rather avoid this.

Upvotes: 2

Views: 466

Answers (1)

Matthew
Matthew

Reputation: 2677

First, create a variable, "fe", that identifies unique combinations of country, year, product.

library(data.table)
# convert data.frame to data.table
setDT(df)
# create a new group variable
df[, fe := .GRP, by = list(country, year, product)]
head(df)
   year week product country    revenue fe
1: 2002    1       A     usa 0.84131750  1
2: 2002    2       A     usa 0.07530538  1
3: 2002    3       A     usa 0.56183346  1
4: 2002    4       A     usa 0.80720792  1
5: 2002    1       A germany 1.25329883  2
6: 2002    2       A germany 0.44860296  2

Now use plm or felm. I like felm since it also works with multiple fixed effects and interactive fixed effects

library(lfe)
felm(revenue ~ week | fe, df)

Upvotes: 1

Related Questions