Reputation: 1453
I have a dataset where I want to group countries on a broader level then per country. Still, the country is the id variable in my dataset. Example:
library(plm)
Data <- data.frame(iris)
Data$time <- c(rep(1951:2000,3))
Data$mygroup <- c(rep("a",100),rep("b",50))
test <- plm(Sepal.Length ~ Sepal.Width + Petal.Length,data=Data, model="within", index=c("time","Species"))
summary(test)
Can I tell plm
somehow, that I want the within transformation to happen over mygroup
rather than species
? If I use mygroup as the index, I run into trouble with duplicate years per id which I'm guessing bothers plm
when trying to calculate individual specific constants etc.
test2 <- plm(Sepal.Length ~ Sepal.Width + Petal.Length,data=Data, model="within", index=c("time","mygroup"))
I could simply build dummies based on mygroup
and include them but the within transformation should be more efficient and also seems more elegant. I don't see an option for this in the plm
help file though. Any help would be appreciated!
Upvotes: 0
Views: 372
Reputation: 3687
There is no option to sweep out additional effects in plm by the within tansformation (besides regular id and time dimension). As you suggested you can use dummies for mygroup
to do that. If the number of groups is not very large, I do not see any drawbacks - just a minor "pollution" of the output.
Otherwise, if you want to sweep out additional effects by a within transformation you can use the package lfe
for that. It is especially handy if you have a large number of additional effects you want to sweep out and/or if the number of groups is very large as you avoid pollution of our output and computation is faster for large data sets (lfe
implements the within transformation in C
).
By the way, you should swap "time"
and "Species"
in your example:
test <- plm(Sepal.Length ~ Sepal.Width + Petal.Length,data=Data, model="within", index=c("Species", "time"))
Upvotes: 3