if (FALSE) {
### A benchmark of plm without and with speed-up
library("plm")
library("collapse")
library("microbenchmark")
rm(list = ls())
data("wlddev", package = "collapse")
form <- LIFEEX ~ PCGDP + GINI
# produce big data set (taken from collapse's vignette)
wlddevsmall <- get_vars(wlddev, c("iso3c","year","OECD","PCGDP","LIFEEX","GINI","ODA"))
wlddevsmall$iso3c <- as.character(wlddevsmall$iso3c)
data <- replicate(100, wlddevsmall, simplify = FALSE)
rm(wlddevsmall)
uniquify <- function(x, i) {
x$iso3c <- paste0(x$iso3c, i)
x
}
data <- unlist2d(Map(uniquify, data, as.list(1:100)), idcols = FALSE)
data <- pdata.frame(data, index = c("iso3c", "year"))
pdim(data) # Balanced Panel: n = 21600, T = 59, N = 1274400 // but many NAs
# data <- na.omit(data)
# pdim(data) # Unbalanced Panel: n = 13300, T = 1-31, N = 93900
times <- 1 # no. of repetitions for benchmark - this takes quite long!
onewayFE <- microbenchmark(
{options("plm.fast" = FALSE); plm(form, data = data, model = "within")},
{options("plm.fast" = TRUE); plm(form, data = data, model = "within")},
times = times)
summary(onewayFE, unit = "relative")
## two-ways FE benchmark requires pkg fixest and lfe
## (End-users shall only set option plm.fast. Option plm.fast.pkg.FE.tw shall
## _not_ be set by the end-user, it is determined automatically when pkg plm
## is attached; however, it needs to be set explicitly in this example for the
## benchmark.)
if(requireNamespace("fixest", quietly = TRUE) &&
requireNamespace("lfe", quietly = TRUE)) {
twowayFE <- microbenchmark(
{options("plm.fast" = FALSE);
plm(form, data = data, model = "within", effect = "twoways")},
{options("plm.fast" = TRUE, "plm.fast.pkg.FE.tw" = "collapse");
plm(form, data = data, model = "within", effect = "twoways")},
{options("plm.fast" = TRUE, "plm.fast.pkg.FE.tw" = "fixest");
plm(form, data = data, model = "within", effect = "twoways")},
{options("plm.fast" = TRUE, "plm.fast.pkg.FE.tw" = "lfe");
plm(form, data = data, model = "within", effect = "twoways")},
times = times)
summary(twowayFE, unit = "relative")
}
onewayRE <- microbenchmark(
{options("plm.fast" = FALSE); plm(form, data = data, model = "random")},
{options("plm.fast" = TRUE); plm(form, data = data, model = "random")},
times = times)
summary(onewayRE, unit = "relative")
twowayRE <- microbenchmark(
{options("plm.fast" = FALSE); plm(form, data = data, model = "random", effect = "twoways")},
{options("plm.fast" = TRUE); plm(form, data = data, model = "random", effect = "twoways")},
times = times)
summary(twowayRE, unit = "relative")
}
Run the code above in your browser using DataLab