| 1 |
context("plm objects - random effects") |
| 2 |
set.seed(20190513) |
| 3 |
|
| 4 |
skip_if_not_installed("nlme") |
| 5 |
skip_if_not_installed("plm") |
| 6 |
|
| 7 |
library(nlme, quietly=TRUE) |
| 8 |
library(plm, quietly=TRUE) |
| 9 |
|
| 10 |
data("Grunfeld", package = "plm") |
| 11 |
data("Produc", package = "plm") |
| 12 |
|
| 13 |
# grun_re <- plm(inv ~ value + capital, data = Grunfeld, model="random") |
| 14 |
# Grunfeld$cluster <- sample(LETTERS[1:10], size = nrow(Grunfeld), replace=TRUE) |
| 15 |
# Grunfeld_scramble <- Grunfeld[sample(nrow(Grunfeld)),] |
| 16 |
|
| 17 |
CR_types <- paste0("CR",0:4) |
| 18 |
|
| 19 |
plm_individual <- plm(inv ~ value + capital, data = Grunfeld, model="random") |
| 20 |
obj <- plm_individual |
| 21 |
|
| 22 |
test_that("individual effects agree with gls", { |
| 23 |
icc <- with(plm_individual$ercomp, sigma2[["id"]] / (sigma2[["id"]] + sigma2[["idios"]])) |
| 24 |
gls_individual <- gls(inv ~ value + capital, data = Grunfeld, |
| 25 |
correlation = corCompSymm(value = icc, form = ~ 1 | firm, fixed=TRUE)) |
| 26 |
|
| 27 |
expect_equal(model_matrix(plm_individual), model_matrix(gls_individual)) |
| 28 |
expect_identical(nobs(plm_individual), nobs(gls_individual)) |
| 29 |
V_ratio <- Map("/", targetVariance(plm_individual, cluster = Grunfeld$firm), |
| 30 |
targetVariance(gls_individual, cluster = Grunfeld$firm)) |
| 31 |
expect_equal(lapply(V_ratio, min), lapply(V_ratio, max)) |
| 32 |
expect_equivalent(residuals_CS(plm_individual), residuals_CS(gls_individual)) |
| 33 |
|
| 34 |
CR_plm <- lapply(CR_types, function(x) vcovCR(plm_individual, type = x)) |
| 35 |
CR_gls <- lapply(CR_types, function(x) vcovCR(gls_individual, type = x)) |
| 36 |
expect_equivalent(CR_plm, CR_gls) |
| 37 |
|
| 38 |
test_plm <- lapply(CR_types, function(x) coef_test(plm_individual, vcov = x, test = "All", p_values = FALSE)[,-3]) |
| 39 |
test_gls <- lapply(CR_types, function(x) coef_test(gls_individual, vcov = x, test = "All", p_values = FALSE)[,-3]) |
| 40 |
expect_equivalent(test_plm, test_gls) |
| 41 |
|
| 42 |
}) |
| 43 |
|
| 44 |
plm_time <- plm(log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp, |
| 45 |
data = Produc, index = c("state","year"), |
| 46 |
effect = "time", model = "random") |
| 47 |
|
| 48 |
test_that("time effects agree with gls", { |
| 49 |
icc <- with(plm_time$ercomp, sigma2[[2]] / (sigma2[[2]] + sigma2[[1]])) |
| 50 |
gls_time <- gls(log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp, |
| 51 |
data = Produc, |
| 52 |
correlation = corCompSymm(value = icc, form = ~ 1 | year, fixed = TRUE)) |
| 53 |
|
| 54 |
expect_equal(model_matrix(plm_time), model_matrix(gls_time)) |
| 55 |
expect_identical(nobs(plm_time), nobs(gls_time)) |
| 56 |
expect_equivalent(residuals_CS(plm_time), residuals_CS(gls_time)) |
| 57 |
|
| 58 |
CR_plm <- lapply(CR_types, function(x) vcovCR(plm_time, type = x)) |
| 59 |
CR_gls <- lapply(CR_types, function(x) vcovCR(gls_time, type = x)) |
| 60 |
expect_equivalent(CR_plm, CR_gls) |
| 61 |
|
| 62 |
test_plm <- lapply(CR_types, function(x) coef_test(plm_time, vcov = x, test = "All", p_values = FALSE)[,-3]) |
| 63 |
test_gls <- lapply(CR_types, function(x) coef_test(gls_time, vcov = x, test = "All", p_values = FALSE)[,-3]) |
| 64 |
expect_equivalent(test_plm, test_gls) |
| 65 |
|
| 66 |
}) |
| 67 |
|
| 68 |
|
| 69 |
plm_twoways <- plm(log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp, |
| 70 |
data = Produc, index = c("state","year"), |
| 71 |
effect = "twoways", model = "random") |
| 72 |
|
| 73 |
test_that("two-way effects throws error", { |
| 74 |
expect_error(vcovCR(plm_twoways, type = "CR2")) |
| 75 |
}) |
| 76 |
|
| 77 |
plm_nested <- plm(log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp, |
| 78 |
data = Produc, index = c("state","year","region"), |
| 79 |
effect = "nested", model = "random") |
| 80 |
|
| 81 |
test_that("nested effects agree with lmer", { |
| 82 |
|
| 83 |
skip_if_not_installed("lme4") |
| 84 |
library(lme4, quietly=TRUE) |
| 85 |
|
| 86 |
Produc_sort_order <- with(Produc, order(region, state)) |
| 87 |
|
| 88 |
plm_nested$ercomp$sigma2 |
| 89 |
lmer_nested_fit <- lmer(log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp + (1 | region / state), |
| 90 |
data = Produc) |
| 91 |
theta <- getME(lmer_nested_fit, "theta") |
| 92 |
theta[1:2] <- with(plm_nested$ercomp, sqrt(sigma2[2:3] / sigma2[1])) |
| 93 |
lmer_nested <- update(lmer_nested_fit, start = theta, control = lmerControl(optimizer = NULL)) |
| 94 |
|
| 95 |
expect_equivalent(model_matrix(plm_nested), model_matrix(lmer_nested)[Produc_sort_order,]) |
| 96 |
expect_identical(nobs(plm_nested), nobs(lmer_nested)) |
| 97 |
|
| 98 |
expect_equivalent(targetVariance(plm_nested, cluster = findCluster.plm(plm_nested)), |
| 99 |
targetVariance(lmer_nested, cluster = Produc$region)) |
| 100 |
|
| 101 |
expect_equivalent(weightMatrix(plm_nested, cluster = findCluster.plm(plm_nested)), |
| 102 |
weightMatrix(lmer_nested, cluster = Produc$region), tol = 1e-6) |
| 103 |
|
| 104 |
CR_plm <- lapply(CR_types, function(x) vcovCR(plm_nested, type = x)) |
| 105 |
CR_lmer <- lapply(CR_types, function(x) vcovCR(lmer_nested, type = x)) |
| 106 |
expect_equivalent(CR_plm, CR_lmer) |
| 107 |
|
| 108 |
test_plm <- lapply(CR_types, function(x) coef_test(plm_nested, vcov = x, test = "All", p_values = FALSE)[,-3]) |
| 109 |
test_lmer <- lapply(CR_types, function(x) coef_test(lmer_nested, vcov = x, test = "All", p_values = FALSE)[,-3]) |
| 110 |
compare_ttests(test_plm, test_lmer, tol = 1e-5) |
| 111 |
|
| 112 |
}) |
| 113 |
|
| 114 |
test_that("bread works", { |
| 115 |
|
| 116 |
expect_true(check_bread(plm_individual, |
| 117 |
cluster = findCluster.plm(plm_individual), |
| 118 |
y = plm_individual$model$inv)) |
| 119 |
expect_true(check_bread(plm_time, |
| 120 |
cluster = findCluster.plm(plm_time), |
| 121 |
y = plm_time$model$"log(gsp)")) |
| 122 |
expect_true(check_bread(plm_nested, |
| 123 |
cluster = findCluster.plm(plm_nested), |
| 124 |
y = plm_nested$model$"log(gsp)")) |
| 125 |
}) |
| 126 |
|
| 127 |
test_that("CR0 and CR1S agree with arellano vcov", { |
| 128 |
|
| 129 |
expect_equivalent(vcovHC(plm_individual, method="arellano", type = "HC0", cluster = "group"), |
| 130 |
as.matrix(vcovCR(plm_individual, type = "CR0"))) |
| 131 |
expect_equivalent(vcovHC(plm_individual, method="arellano", type = "sss", cluster = "group"), |
| 132 |
as.matrix(vcovCR(plm_individual, type = "CR1S"))) |
| 133 |
|
| 134 |
expect_equivalent(vcovHC(plm_time, method="arellano", type = "HC0", cluster = "time"), |
| 135 |
as.matrix(vcovCR(plm_time, type = "CR0"))) |
| 136 |
expect_equivalent(vcovHC(plm_time, method="arellano", type = "sss", cluster = "time"), |
| 137 |
as.matrix(vcovCR(plm_time, type = "CR1S"))) |
| 138 |
|
| 139 |
# Can't replicate vcovHC because plm isn't clustering correctly. |
| 140 |
# expect_equivalent(vcovHC(plm_nested, method="arellano", type = "HC0", cluster = "group"), |
| 141 |
# as.matrix(vcovCR(plm_nested, type = "CR0"))) |
| 142 |
# expect_equivalent(vcovHC(plm_nested, method="arellano", type = "sss", cluster = "group"), |
| 143 |
# as.matrix(vcovCR(plm_nested, type = "CR1S"))) |
| 144 |
}) |
| 145 |
|
| 146 |
|
| 147 |
test_that("vcovCR options work for CR2", { |
| 148 |
CR2_iv <- vcovCR(plm_individual, type = "CR2") |
| 149 |
expect_equal(vcovCR(plm_individual, cluster = Grunfeld$firm, type = "CR2"), CR2_iv) |
| 150 |
expect_equal(vcovCR(plm_individual, type = "CR2", inverse_var = TRUE), CR2_iv) |
| 151 |
tgt <- targetVariance(plm_individual, cluster = Grunfeld$firm) |
| 152 |
expect_equivalent(vcovCR(plm_individual, type = "CR2", target = tgt, inverse_var = TRUE), CR2_iv) |
| 153 |
|
| 154 |
CR2_not <- vcovCR(plm_individual, type = "CR2", inverse_var = FALSE) |
| 155 |
expect_equivalent(CR2_not, CR2_iv) |
| 156 |
expect_equal(vcovCR(plm_individual, cluster = Grunfeld$firm, type = "CR2", inverse_var = FALSE), CR2_not) |
| 157 |
expect_equal(vcovCR(plm_individual, type = "CR2", target = tgt), CR2_not) |
| 158 |
expect_equal(vcovCR(plm_individual, type = "CR2", target = tgt, inverse_var = FALSE), CR2_not) |
| 159 |
}) |
| 160 |
|
| 161 |
test_that("vcovCR options work for CR4", { |
| 162 |
CR4_iv <- vcovCR(plm_individual, type = "CR4") |
| 163 |
expect_equal(vcovCR(plm_individual, cluster = Grunfeld$firm, type = "CR4"), CR4_iv) |
| 164 |
expect_equal(vcovCR(plm_individual, type = "CR4", inverse_var = TRUE), CR4_iv) |
| 165 |
tgt <- targetVariance(plm_individual, cluster = Grunfeld$firm) |
| 166 |
expect_equivalent(vcovCR(plm_individual, type = "CR4", target = tgt, inverse_var = TRUE), CR4_iv) |
| 167 |
|
| 168 |
CR4_not <- vcovCR(plm_individual, type = "CR4", inverse_var = FALSE) |
| 169 |
expect_equivalent(CR4_not, CR4_iv) |
| 170 |
expect_equal(vcovCR(plm_individual, cluster = Grunfeld$firm, type = "CR4", inverse_var = FALSE), CR4_not) |
| 171 |
expect_equal(vcovCR(plm_individual, type = "CR4", target = tgt), CR4_not) |
| 172 |
expect_equal(vcovCR(plm_individual, type = "CR4", target = tgt, inverse_var = FALSE), CR4_not) |
| 173 |
}) |
| 174 |
|
| 175 |
test_that("CR2 and CR4 are target-unbiased", { |
| 176 |
|
| 177 |
expect_true(check_CR(plm_individual, vcov = "CR2")) |
| 178 |
expect_true(check_CR(plm_individual, vcov = "CR4")) |
| 179 |
|
| 180 |
expect_true(check_CR(plm_time, vcov = "CR2")) |
| 181 |
expect_true(check_CR(plm_time, vcov = "CR4")) |
| 182 |
|
| 183 |
expect_true(check_CR(plm_nested, vcov = "CR2")) |
| 184 |
expect_true(check_CR(plm_nested, vcov = "CR4")) |
| 185 |
|
| 186 |
}) |
| 187 |
|
| 188 |
|
| 189 |
test_that("vcovCR works when clustering at a level above the random effects.", { |
| 190 |
|
| 191 |
data("Wages", package = "plm") |
| 192 |
Wages$ID <- rep(1:595, each = 7) |
| 193 |
Wages$period <- rep(1:7, times = 595) |
| 194 |
Wages$Grp <- rep(1:119, each = 7 * 5) |
| 195 |
|
| 196 |
plm_ID <- plm(lwage ~ wks + south + smsa + married + exp, |
| 197 |
data = Wages, |
| 198 |
index = c("ID","period","Grp"), |
| 199 |
model="random") |
| 200 |
|
| 201 |
ICC <- with(plm_ID$ercomp, sigma2[2] / sum(sigma2)) |
| 202 |
|
| 203 |
gls_ID <- gls(lwage ~ wks + south + smsa + married + exp, |
| 204 |
data = Wages, |
| 205 |
correlation = corCompSymm(value = ICC, form = ~ 1 | ID, fixed = TRUE)) |
| 206 |
|
| 207 |
plm_vcov_ID <- lapply(CR_types, function(x) vcovCR(plm_ID, type = x)) |
| 208 |
gls_vcov_ID <- lapply(CR_types, function(x) vcovCR(gls_ID, type = x)) |
| 209 |
|
| 210 |
expect_equivalent(plm_vcov_ID, gls_vcov_ID) |
| 211 |
|
| 212 |
plm_vcov_grp <- lapply(CR_types, function(x) vcovCR(plm_ID, cluster = Wages$Grp, type = x)) |
| 213 |
plm_vcov_group <- lapply(CR_types, function(x) vcovCR(plm_ID, cluster = "group", type = x)) |
| 214 |
expect_equal(plm_vcov_grp, plm_vcov_group) |
| 215 |
|
| 216 |
gls_vcov_grp <- lapply(CR_types, function(x) vcovCR(gls_ID, cluster = Wages$Grp, type = x)) |
| 217 |
expect_equivalent(plm_vcov_group, gls_vcov_grp) |
| 218 |
|
| 219 |
plm_ID <- plm(log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp, |
| 220 |
data = Produc, index = c("state","year","region"), |
| 221 |
effect = "individual", model = "random") |
| 222 |
|
| 223 |
ICC <- with(plm_ID$ercomp, sigma2[2] / sum(sigma2)) |
| 224 |
|
| 225 |
gls_ID <- gls(log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp, |
| 226 |
data = Produc, |
| 227 |
correlation = corCompSymm(value = ICC, form = ~ 1 | state, fixed = TRUE)) |
| 228 |
|
| 229 |
plm_vcov_grp <- lapply(CR_types, function(x) vcovCR(plm_ID, cluster = Produc$region, type = x)) |
| 230 |
plm_vcov_group <- lapply(CR_types, function(x) vcovCR(plm_ID, cluster = "group", type = x)) |
| 231 |
expect_equal(plm_vcov_grp, plm_vcov_group) |
| 232 |
|
| 233 |
gls_vcov_grp <- lapply(CR_types, function(x) vcovCR(gls_ID, cluster = Produc$region, type = x)) |
| 234 |
expect_equivalent(plm_vcov_grp, gls_vcov_grp) |
| 235 |
|
| 236 |
}) |