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