Debian Codegraph @

tests/testthat/test_plm-first-differences.R

raw
1 context("plm objects - first differences model")
2 set.seed(20190513)
3
4 skip_if_not_installed("plm")
5 skip_if_not_installed("AER")
6
7 library(plm, quietly=TRUE)
8
9 data(Fatalities, package = "AER")
10 Fatalities <- within(Fatalities, {
11 frate <- 10000 * fatal / pop
12 drinkagec <- cut(drinkage, breaks = 18:22, include.lowest = TRUE, right = FALSE)
13 drinkagec <- relevel(drinkagec, ref = 4)
14 })
15
16 plm_FD <- plm(frate ~ beertax + drinkagec + miles + unemp + log(income),
17 data = Fatalities, index = c("state", "year"),
18 model = "fd")
19
20 n_obs <- nobs(plm_FD)
21 target <- with(Fatalities, 1 / pop[year != levels(year)[1]])
22
23 test_that("bread works", {
24 y <- na.omit(diff(plm_FD$model$frate))
25 cluster <- findCluster.plm(plm_FD)
26 expect_true(check_bread(plm_FD, cluster = cluster, y = y))
27 sigma_sq <- with(plm_FD, sum(residuals^2) / df.residual)
28 expect_equal(vcov(plm_FD), bread(plm_FD) * sigma_sq / v_scale(plm_FD))
29 })
30
31 test_that("CR0 and CR1S agree with arellano vcov", {
32
33 expect_equal(vcovHC(plm_FD, method="arellano", type = "HC0", cluster = "group"),
34 as.matrix(vcovCR(plm_FD, type = "CR0")), check.attributes = FALSE)
35 expect_equal(vcovHC(plm_FD, method="arellano", type = "sss", cluster = "group"),
36 as.matrix(vcovCR(plm_FD, type = "CR1S")), check.attributes = FALSE)
37 })
38
39 test_that("CR0 and CR1S agree with arellano vcov for versions <= 2.6-1", {
40
41 skip_if(packageVersion("plm") > "2.6-1")
42
43 X <- model_matrix(plm_FD)
44 e <- residuals(plm_FD)
45 index <- attr(model.frame(plm_FD), "index")
46 cluster <- index[[2]]
47 cluster <- cluster[index[[2]] != levels(index[[2]])[1]]
48 estmats <- sapply(split.data.frame(e * X, cluster, drop = TRUE), colSums)
49 meat <- tcrossprod(estmats)
50 bread <- chol2inv(chol(crossprod(X)))
51 vcov_time <- bread %*% meat %*% bread
52 attr(vcov_time, "dimnames") <- attr(meat, "dimnames")
53
54 expect_equal(vcov_time, as.matrix(vcovCR(plm_FD, cluster = "time", type = "CR0")))
55
56 baloney <- tcrossprod(estmats[,-6])
57 vcov_baloney <- bread %*% baloney %*% bread
58 attr(vcov_baloney, "dimnames") <- attr(baloney, "dimnames")
59 expect_equal(vcov_baloney,
60 vcovHC(plm_FD, method="arellano", type = "HC0", cluster = "time"), check.attributes = FALSE)
61 })
62
63 test_that("CR0 and CR1S agree with arellano vcov for versions > 2.6-1", {
64
65 skip_if_not_installed("plm", minimum_version = "2.6-2")
66
67 expect_equal(vcovHC(plm_FD, method="arellano", type = "HC0", cluster = "time"),
68 as.matrix(vcovCR(plm_FD, type = "CR0", cluster = "time")), check.attributes = FALSE)
69 expect_equal(vcovHC(plm_FD, method="arellano", type = "sss", cluster = "time"),
70 as.matrix(vcovCR(plm_FD, type = "CR1S", cluster = "time")), check.attributes = FALSE)
71 })
72
73 test_that("vcovCR options work for CR2", {
74
75 CR2_iv <- vcovCR(plm_FD, type = "CR2")
76 expect_equal(vcovCR(plm_FD, cluster = Fatalities$state, type = "CR2"), CR2_iv)
77 expect_equal(vcovCR(plm_FD, type = "CR2", inverse_var = TRUE), CR2_iv)
78
79 expect_equal(vcovCR(plm_FD, type = "CR2",
80 target = rep(1, n_obs),
81 inverse_var = TRUE), CR2_iv)
82
83 CR2_not <- vcovCR(plm_FD, type = "CR2", inverse_var = FALSE)
84 expect_equivalent(CR2_not, CR2_iv)
85 expect_equal(vcovCR(plm_FD, cluster = Fatalities$state, type = "CR2", inverse_var = FALSE), CR2_not)
86 expect_equal(vcovCR(plm_FD, type = "CR2", target = rep(1, n_obs)), CR2_not)
87 expect_equal(vcovCR(plm_FD, type = "CR2", target = rep(1, n_obs), inverse_var = FALSE), CR2_not)
88 expect_false(identical(vcovCR(plm_FD, type = "CR2", target = target), CR2_not))
89 })
90
91 test_that("vcovCR options work for CR4", {
92 skip_on_cran()
93 CR4_iv <- vcovCR(plm_FD, type = "CR4")
94 expect_equal(vcovCR(plm_FD, cluster = Fatalities$state, type = "CR4"), CR4_iv)
95 expect_equal(vcovCR(plm_FD, type = "CR4", inverse_var = TRUE), CR4_iv)
96 expect_equal(vcovCR(plm_FD, type = "CR4", target = rep(1, n_obs), inverse_var = TRUE), CR4_iv)
97
98 CR4_not <- vcovCR(plm_FD, type = "CR4", inverse_var = FALSE)
99 expect_equivalent(CR4_not, CR4_iv, tolerance = 10^-3)
100 expect_equal(vcovCR(plm_FD, cluster = Fatalities$state, type = "CR4", inverse_var = FALSE), CR4_not)
101 expect_equal(vcovCR(plm_FD, type = "CR4", target = rep(1, n_obs)), CR4_not)
102 expect_equal(vcovCR(plm_FD, type = "CR4", target = rep(1, n_obs), inverse_var = FALSE), CR4_not)
103 expect_false(identical(vcovCR(plm_FD, type = "CR4", target = target), CR4_not))
104 })
105
106 test_that("CR2 is target-unbiased", {
107
108 expect_true(check_CR(plm_FD, vcov = "CR2", tol = 10^-7))
109 expect_true(check_CR(plm_FD, vcov = "CR2", inverse_var = FALSE))
110
111 expect_true(check_CR(plm_FD, cluster = "time", vcov = "CR2", tol = 10^-7))
112 expect_true(check_CR(plm_FD, cluster = "time", vcov = "CR2", inverse_var = FALSE))
113
114 })
115
116
117 test_that("vcovCR is equivalent to vcovHC when clusters are all of size 1", {
118 CR_types <- paste0("CR",c(0,2))
119 HC_types <- paste0("HC",c(0,2))
120
121 CR_individual <- lapply(CR_types, function(t)
122 as.matrix(vcovCR(plm_FD, cluster = 1:nrow(Fatalities), type = t)))
123 HC_individual <- lapply(HC_types, function(t)
124 vcovHC(plm_FD, method = "white1", type = t))
125 expect_equal(CR_individual, HC_individual, check.attributes = FALSE)
126
127 })
128