Debian Codegraph @

tests/testthat/test_plm_overspecified_problem.R

raw
1 context("plm objects - balanced panel with cluster-level interactions")
2
3 skip_if_not_installed("plm")
4 library(plm, quietly=TRUE)
5
6 set.seed(20200721)
7 G <- 93
8 N <- 4 * G
9 Ts <- 4
10
11 beta1 <- rgamma(Ts, shape = 0.3, rate = 0.1)
12 beta2 <- rgamma(Ts, shape = 0.2, rate = 0.1)
13
14 grp <- factor(rep(1:G, each = 4 * Ts))
15 ID <- factor(rep(1:N, each = Ts))
16 trt <- factor(rep(LETTERS[1:4], times = N))
17 X1 <- rep(rnorm(N), each = Ts)
18 X2 <- rep(rnorm(N), each = Ts)
19 Y <- beta1[trt] * X1 + beta2[trt] * X2 + rnorm(N)[ID] + rnorm(N * Ts, sd = 0.5)
20
21 dat <- data.frame(grp, ID, trt, X1, X2, Y)
22 dat <- pdata.frame(dat, index = c("ID","trt"))
23
24 obj <- plm(Y ~ trt + trt * X1 + trt * X2,
25 data=dat,
26 model="within")
27
28 # cluster <- dat$grp
29 # type <- "CR2"
30 # target <- NULL
31 # inverse_var <- TRUE
32 # form <- "sandwich"
33 # ignore_FE <- FALSE
34 #
35 #
36 # colnames(model_matrix.plm(obj))
37 # model.matrix(obj) %>% str()
38 # model.matrix(obj, cstcovar.rm = "all") %>% str()
39
40 CR_types <- paste0("CR",0:2)
41
42 test_that("vcovCR works with cluster-level interactions.", {
43 meat_list <- lapply(CR_types, function(x) vcovCR(obj = obj, cluster=dat$grp, type = x, form = "meat"))
44 bread_dim <- dim(bread(obj))
45 lapply(meat_list, function(x) expect_identical(dim(x), bread_dim))
46
47 V_CR_list <- lapply(CR_types, function(x) vcovCR(obj = obj, cluster=dat$grp, type = x))
48 lapply(V_CR_list, expect_s3_class, class = "vcovCR")
49
50 })
51
52 test_that("CR0 agrees with built-in CRVE for plm", {
53 V_plm <- vcovHC(obj, method = "arellano", type = "HC0")
54 V_CR0 <- vcovCR(obj, type = "CR0")
55 expect_equal(V_plm, as.matrix(V_CR0), check.attributes = FALSE)
56 })