Debian Codegraph @

tests/testthat/test_plm-fixed-effects.R

raw
1 context("plm objects - fixed effects")
2 set.seed(20190513)
3
4 skip_if_not_installed("plm")
5 library(plm, quietly=TRUE)
6
7 data("Produc", package = "plm")
8 Produc$cluster <- sample(LETTERS[1:10], size = nrow(Produc), replace=TRUE)
9 Produc_scramble <- Produc[sample(nrow(Produc)),]
10 n <- nrow(Produc_scramble)
11
12 plm_individual <- plm(log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp,
13 data = Produc_scramble, index = c("state","year"),
14 effect = "individual", model = "within")
15 lm_individual <- lm(log(gsp) ~ 0 + state + log(pcap) + log(pc) + log(emp) + unemp, data = Produc)
16 individual_names <- names(coef(plm_individual))
17 individual_index <- names(coef(lm_individual)) %in% individual_names
18
19 test_that("individual effects agree with lm", {
20 expect_equal(vcovCR(plm_individual, type="CR0")[individual_names,individual_names],
21 vcovCR(lm_individual, cluster = Produc$state, type = "CR0")[individual_index,individual_index])
22 expect_equal(vcovCR(plm_individual, type="CR1")[individual_names,individual_names],
23 vcovCR(lm_individual, cluster = Produc$state, type = "CR1")[individual_index,individual_index])
24 expect_equal(vcovCR(plm_individual, type="CR2")[individual_names,individual_names],
25 vcovCR(lm_individual, cluster = Produc$state, type = "CR2")[individual_index,individual_index])
26 })
27
28 plm_time <- plm(log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp,
29 data = Produc_scramble, index = c("state","year"),
30 effect = "time", model = "within")
31 lm_time <- lm(log(gsp) ~ 0 + factor(year) + log(pcap) + log(pc) + log(emp) + unemp, data = Produc)
32 time_names <- names(coef(plm_time))
33 time_index <- names(coef(lm_time)) %in% time_names
34
35 test_that("time effects agree with lm", {
36 expect_equal(vcovCR(plm_time, type="CR0")[time_names,time_names],
37 vcovCR(lm_time, cluster = Produc$year, type = "CR0")[time_index,time_index])
38 expect_equal(vcovCR(plm_time, type="CR1")[time_names,time_names],
39 vcovCR(lm_time, cluster = Produc$year, type = "CR1")[time_index,time_index])
40 expect_equal(vcovCR(plm_time, type="CR2")[time_names,time_names],
41 vcovCR(lm_time, cluster = Produc$year, type = "CR2")[time_index,time_index])
42 })
43
44 plm_twoways <- plm(log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp,
45 data = Produc_scramble, index = c("state","year"),
46 effect = "twoways", model = "within")
47 lm_twoways <- lm(log(gsp) ~ 0 + state + factor(year) + log(pcap) + log(pc) + log(emp) + unemp, data = Produc)
48 twoway_names <- names(coef(plm_twoways))
49 twoway_index <- names(coef(lm_twoways)) %in% twoway_names
50
51 test_that("two-way effects agree with lm", {
52
53 # clustering on individual
54 expect_equal(vcovCR(plm_twoways, cluster = "individual", type="CR0")[twoway_names,twoway_names],
55 vcovCR(lm_twoways, cluster = Produc$state, type = "CR0")[twoway_index,twoway_index])
56 expect_equal(vcovCR(plm_twoways, cluster = "individual", type="CR1")[twoway_names,twoway_names],
57 vcovCR(lm_twoways, cluster = Produc$state, type = "CR1")[twoway_index,twoway_index])
58 expect_equal(vcovCR(plm_twoways, cluster = "individual", type="CR2")[twoway_names,twoway_names],
59 vcovCR(lm_twoways, cluster = Produc$state, type = "CR2")[twoway_index,twoway_index])
60
61 # clustering on time
62 expect_equal(vcovCR(plm_twoways, cluster = "time", type="CR0")[twoway_names,twoway_names],
63 vcovCR(lm_twoways, cluster = Produc$year, type = "CR0")[twoway_index,twoway_index])
64 expect_equal(vcovCR(plm_twoways, cluster = "time", type="CR1")[twoway_names,twoway_names],
65 vcovCR(lm_twoways, cluster = Produc$year, type = "CR1")[twoway_index,twoway_index])
66 expect_equal(vcovCR(plm_twoways, cluster = "time", type="CR2")[twoway_names,twoway_names],
67 vcovCR(lm_twoways, cluster = Produc$year, type = "CR2")[twoway_index,twoway_index])
68
69 # clustering on a randomly generated factor
70 expect_equal(vcovCR(plm_twoways, cluster = Produc_scramble$cluster, type="CR0")[twoway_names,twoway_names],
71 vcovCR(lm_twoways, cluster = Produc$cluster, type = "CR0")[twoway_index,twoway_index])
72 expect_equal(vcovCR(plm_twoways, cluster = Produc_scramble$cluster, type="CR1")[twoway_names,twoway_names],
73 vcovCR(lm_twoways, cluster = Produc$cluster, type = "CR1")[twoway_index,twoway_index])
74 expect_equal(vcovCR(plm_twoways, cluster = Produc_scramble$cluster, type="CR2")[twoway_names,twoway_names],
75 vcovCR(lm_twoways, cluster = Produc$cluster, type = "CR2")[twoway_index,twoway_index])
76
77 })
78
79 test_that("bread works", {
80 y <- plm_individual$model$"log(gsp)"
81 expect_true(check_bread(plm_individual, cluster = findCluster.plm(plm_individual), y = y))
82 sigma_sq_ind <- with(plm_individual, sum(residuals^2) / df.residual)
83 expect_equal(vcov(plm_individual), bread(plm_individual) * sigma_sq_ind / v_scale(plm_individual))
84
85 expect_true(check_bread(plm_time, cluster = findCluster.plm(plm_time), y = y))
86 sigma_sq_time <- with(plm_time, sum(residuals^2) / df.residual)
87 expect_equal(vcov(plm_time), bread(plm_time) * sigma_sq_time / v_scale(plm_time))
88
89 expect_true(check_bread(plm_twoways, cluster = Produc_scramble$state, y = y))
90 expect_true(check_bread(plm_twoways, cluster = Produc_scramble$year, y = y))
91 sigma_sq_two <- with(plm_twoways, sum(residuals^2) / df.residual)
92 expect_equal(vcov(plm_twoways), bread(plm_twoways) * sigma_sq_two / v_scale(plm_twoways))
93 })
94
95 test_that("CR0 and CR1S agree with arellano vcov", {
96 expect_equal(vcovHC(plm_individual, method="arellano", type = "HC0", cluster = "group"),
97 as.matrix(vcovCR(plm_individual, type = "CR0")), check.attributes = FALSE)
98 expect_equal(vcovHC(plm_individual, method="arellano", type = "sss", cluster = "group"),
99 as.matrix(vcovCR(plm_individual, type = "CR1S")), check.attributes = FALSE)
100
101 expect_equal(vcovHC(plm_time, method="arellano", type = "HC0", cluster = "time"),
102 as.matrix(vcovCR(plm_time, type = "CR0")), check.attributes = FALSE)
103 expect_equal(vcovHC(plm_time, method="arellano", type = "sss", cluster = "time"),
104 as.matrix(vcovCR(plm_time, type = "CR1S")), check.attributes = FALSE)
105
106 expect_equal(vcovHC(plm_twoways, method="arellano", type = "HC0", cluster = "group"),
107 as.matrix(vcovCR(plm_twoways, cluster = "individual", type = "CR0")), check.attributes = FALSE)
108 expect_equal(vcovHC(plm_twoways, method="arellano", type = "sss", cluster = "group"),
109 as.matrix(vcovCR(plm_twoways, cluster = "individual", type = "CR1S")), check.attributes = FALSE)
110 expect_equal(vcovHC(plm_twoways, method="arellano", type = "HC0", cluster = "time"),
111 as.matrix(vcovCR(plm_twoways, cluster = "time", type = "CR0")), check.attributes = FALSE)
112 expect_equal(vcovHC(plm_twoways, method="arellano", type = "sss", cluster = "time"),
113 as.matrix(vcovCR(plm_twoways, cluster = "time", type = "CR1S")), check.attributes = FALSE)
114 })
115
116 test_that("vcovCR options work for CR2", {
117 CR2_iv <- vcovCR(plm_individual, type = "CR2")
118 expect_equal(vcovCR(plm_individual, cluster = Produc_scramble$state, type = "CR2"), CR2_iv)
119 expect_equal(vcovCR(plm_individual, type = "CR2", inverse_var = TRUE), CR2_iv)
120 expect_equal(vcovCR(plm_individual, type = "CR2", target = rep(1, n), inverse_var = TRUE), CR2_iv)
121
122 CR2_not <- vcovCR(plm_individual, type = "CR2", inverse_var = FALSE)
123 expect_equivalent(CR2_not, CR2_iv)
124 expect_equal(vcovCR(plm_individual, cluster = Produc_scramble$state, type = "CR2", inverse_var = FALSE), CR2_not)
125 expect_equal(vcovCR(plm_individual, type = "CR2", target = rep(1, n)), CR2_not)
126 expect_equal(vcovCR(plm_individual, type = "CR2", target = rep(1, n), inverse_var = FALSE), CR2_not)
127 expect_false(identical(vcovCR(plm_individual, type = "CR2", target = 1 / Produc_scramble$emp), CR2_not))
128 })
129
130 test_that("vcovCR options work for CR4", {
131 CR4_iv <- vcovCR(plm_individual, type = "CR4")
132 expect_equal(vcovCR(plm_individual, cluster = Produc_scramble$state, type = "CR4"), CR4_iv)
133 expect_equal(vcovCR(plm_individual, type = "CR4", inverse_var = TRUE), CR4_iv)
134 expect_equal(vcovCR(plm_individual, type = "CR4", target = rep(1, n), inverse_var = TRUE), CR4_iv)
135
136 CR4_not <- vcovCR(plm_individual, type = "CR4", inverse_var = FALSE)
137 expect_equivalent(CR4_not, CR4_iv)
138 expect_equal(vcovCR(plm_individual, cluster = Produc_scramble$state, type = "CR4", inverse_var = FALSE), CR4_not)
139 expect_equal(vcovCR(plm_individual, type = "CR4", target = rep(1, n)), CR4_not)
140 expect_equal(vcovCR(plm_individual, type = "CR4", target = rep(1, n), inverse_var = FALSE), CR4_not)
141 expect_false(identical(vcovCR(plm_individual, type = "CR4", target = 1 / Produc_scramble$emp), CR4_not))
142 })
143
144 test_that("CR2 and CR4 are target-unbiased", {
145
146 skip_on_cran()
147
148 expect_true(check_CR(plm_individual, vcov = "CR2"))
149 expect_true(check_CR(plm_individual, vcov = "CR4"))
150 expect_true(check_CR(plm_individual, vcov = "CR2", inverse_var = FALSE))
151 expect_true(check_CR(plm_individual, vcov = "CR4", inverse_var = FALSE))
152
153 expect_true(check_CR(plm_time, vcov = "CR2"))
154 expect_true(check_CR(plm_time, vcov = "CR4"))
155 expect_true(check_CR(plm_time, vcov = "CR2", inverse_var = FALSE))
156 expect_true(check_CR(plm_time, vcov = "CR4", inverse_var = FALSE))
157
158 expect_true(check_CR(plm_twoways, vcov = "CR2", cluster = "individual"))
159 expect_true(check_CR(plm_twoways, vcov = "CR4", cluster = "individual"))
160 expect_true(check_CR(plm_twoways, vcov = "CR2", cluster = "individual", inverse_var = FALSE))
161 expect_true(check_CR(plm_twoways, vcov = "CR4", cluster = "individual", inverse_var = FALSE))
162 expect_true(check_CR(plm_twoways, vcov = "CR2", cluster = "time"))
163 expect_true(check_CR(plm_twoways, vcov = "CR4", cluster = "time"))
164 expect_true(check_CR(plm_twoways, vcov = "CR2", cluster = "time", inverse_var = FALSE))
165 expect_true(check_CR(plm_twoways, vcov = "CR4", cluster = "time", inverse_var = FALSE))
166 expect_true(check_CR(plm_twoways, vcov = "CR2", cluster = Produc_scramble$cluster))
167 expect_true(check_CR(plm_twoways, vcov = "CR4", cluster = Produc_scramble$cluster))
168 expect_true(check_CR(plm_twoways, vcov = "CR2", cluster = Produc_scramble$cluster, inverse_var = FALSE))
169 expect_true(check_CR(plm_twoways, vcov = "CR4", cluster = Produc_scramble$cluster, inverse_var = FALSE))
170 })
171
172
173 test_that("vcovCR is equivalent to vcovHC when clusters are all of size 1", {
174 library(sandwich, quietly=TRUE)
175
176 CR_types <- paste0("CR",c(0,2))
177 HC_types <- paste0("HC",c(0,2))
178
179 CR_individual <- lapply(CR_types, function(t) as.matrix(vcovCR(plm_individual, cluster = 1:n, type = t)))
180 HC_individual <- lapply(HC_types, function(t) vcovHC(lm_individual, type = t)[individual_index,individual_index])
181 expect_equal(CR_individual, HC_individual)
182
183 CR_time <- lapply(CR_types, function(t) as.matrix(vcovCR(plm_time, cluster = 1:n, type = t)))
184 HC_time <- lapply(HC_types, function(t) vcovHC(lm_time, type = t)[time_index,time_index])
185 expect_equal(CR_time, HC_time)
186
187 CR_twoways <- lapply(CR_types, function(t) as.matrix(vcovCR(plm_twoways, cluster = 1:n, type = t)))
188 HC_twoways <- lapply(HC_types, function(t) vcovHC(lm_twoways, type = t)[twoway_index,twoway_index])
189 expect_equal(CR_twoways, HC_twoways)
190 })
191
192 test_that("CR2 is equivalent to Welch t-test for DiD design", {
193 m0 <- 4
194 m1 <- 9
195 m <- m0 + m1
196 cluster <- factor(rep(LETTERS[1:m], each = 2))
197 n <- length(cluster)
198 time <- rep(c(1,2), m)
199 trt_clusters <- c(rep(0,m0), rep(1,m1))
200 trt <- (time - 1) * rep(trt_clusters, each = 2)
201 nu <- rnorm(m)[cluster]
202 e <- rnorm(n)
203 y <- 0.4 * trt + nu + e
204
205 dat <- data.frame(y, time, trt, cluster)
206 plm_DID <- plm(y ~ trt, data = dat, index = c("cluster","time"),
207 effect = "twoways", model = "within")
208 plm_Satt <- coef_test(plm_DID, vcov = "CR2", cluster = dat$cluster)["trt",]
209 plm_Wald <- Wald_test(plm_DID, constraints = constrain_zero(1), vcov = "CR2", cluster = dat$cluster)
210 df <- m^2 * (m0 - 1) * (m1 - 1) / (m0^2 * (m0 - 1) + m1^2 * (m1 - 1))
211 y_diff <- apply(matrix(y, nrow = 2), 2, diff)
212 t_Welch <- t.test(y_diff ~ trt_clusters)
213
214 expect_equal(with(t_Welch, estimate[[2]] - estimate[[1]]), plm_Satt$beta)
215 expect_equal(as.numeric(-t_Welch$statistic), with(plm_Satt, beta / SE))
216 expect_equal(as.numeric(-t_Welch$statistic)^2, plm_Wald$Fstat)
217 expect_is(all.equal(as.numeric(t_Welch$parameter), plm_Satt$df_Satt), "character")
218 expect_equal(plm_Satt$df, df)
219 expect_equal(plm_Wald$df_denom, df)
220 })