Debian Codegraph @

tests/testthat/test_AER_ivreg.R

raw
1 context("AER::ivreg objects")
2 set.seed(20190513)
3
4 skip_if_not_installed("zoo")
5 skip_if_not_installed("AER")
6
7 library(zoo, quietly=TRUE)
8 library(AER, quietly=TRUE)
9
10 data("CigarettesSW", package = "AER")
11
12 Cigs <- within(CigarettesSW, {
13 rprice <- price/cpi
14 rincome <- income/population/cpi
15 tdiff <- (taxs - tax)/cpi
16 })
17
18 CR_types <- paste0("CR",0:4)
19
20 obj_un <- ivreg(log(packs) ~ log(rprice) + log(rincome) | log(rincome) + tdiff + I(tax/cpi),
21 data = Cigs)
22 obj_wt <- ivreg(log(packs) ~ log(rprice) + log(rincome) | log(rincome) + tdiff + I(tax/cpi),
23 data = Cigs,
24 weights = population)
25
26 X <- model.matrix(obj_wt, component = "regressors")
27 Z <- model.matrix(obj_wt, component = "instruments")
28 y <- log(CigarettesSW$packs)
29 w <- weights(obj_wt)
30
31 test_that("Basic calculations from ivreg agree for unweighted model.", {
32 XZ <- model.matrix(obj_un, component = "projected")
33 ZtZ_inv <- chol2inv(chol(t(Z) %*% Z))
34 XZ_check <- Z %*% ZtZ_inv %*% t(Z) %*% X
35
36 expect_equal(XZ, XZ_check, check.attributes=FALSE)
37 expect_equal(coef(obj_un), lm.fit(XZ, y)$coefficients)
38 expect_equal (bread(obj_un), chol2inv(chol(t(XZ) %*% XZ)) * nobs(obj_un), check.attributes=FALSE)
39
40 hii <- diag(X %*% chol2inv(chol(t(XZ) %*% XZ)) %*% t(XZ))
41 expect_equal(hatvalues(obj_un), hii)
42
43 r <- as.vector(y - X %*% coef(obj_un))
44 expect_equal(r, as.vector(residuals_CS(obj_un)))
45 })
46
47 test_that("Basic calculations from ivreg agree for weighted model.", {
48 XZ <- model.matrix(obj_wt, component = "projected")
49 ZwZ_inv <- chol2inv(chol(t(Z) %*% (w * Z)))
50 XZ_check <- Z %*% ZwZ_inv %*% t(Z) %*% (w * X)
51
52 expect_equal(XZ, XZ_check, check.attributes=FALSE)
53 expect_equal(coef(obj_wt), lm.wfit(XZ, y, w)$coefficients)
54 expect_equal(bread(obj_wt), chol2inv(chol(t(XZ) %*% (w * XZ))) * nobs(obj_wt), check.attributes=FALSE)
55
56 hii <- diag(X%*% chol2inv(chol(t(XZ) %*% (w * XZ))) %*% t(w * XZ))
57 expect_false(all(hatvalues(obj_wt) == hii)) # does not agree because hatvalues doesn't work with weighting
58
59 r <- as.vector(y - X %*% coef(obj_wt))
60 expect_equal(r, as.vector(residuals_CS(obj_wt)))
61 })
62
63 test_that("bread works", {
64
65 expect_true(check_bread(obj_un, cluster = Cigs$state, y = log(Cigs$packs)))
66 tsls_vcov <- bread(obj_un) * summary(obj_un)$sigma^2 / v_scale(obj_un)
67 expect_equal(vcov(obj_un), tsls_vcov)
68
69 expect_true(check_bread(obj_wt, cluster = Cigs$state, y = log(Cigs$packs)))
70 wtsls_vcov <- bread(obj_wt) * summary(obj_wt)$sigma^2 / v_scale(obj_wt)
71 expect_equal(vcov(obj_wt), wtsls_vcov)
72 })
73
74
75 test_that("vcovCR options don't matter for CR0", {
76 expect_error(vcovCR(obj_un, type = "CR0"))
77 CR0 <- vcovCR(obj_un, cluster = Cigs$state, type = "CR0")
78 expect_output(print(CR0))
79 attr(CR0, "target") <- NULL
80 attr(CR0, "inverse_var") <- NULL
81 CR0_A <- vcovCR(obj_un, cluster = Cigs$state, type = "CR0", target = 1 / Cigs$population)
82 attr(CR0_A, "target") <- NULL
83 attr(CR0_A, "inverse_var") <- NULL
84 expect_identical(CR0_A, CR0)
85 CR0_B <- vcovCR(obj_un, cluster = Cigs$state, type = "CR0", target = 1 / Cigs$population, inverse_var = FALSE)
86 attr(CR0_B, "target") <- NULL
87 attr(CR0_B, "inverse_var") <- NULL
88 expect_identical(CR0_A, CR0)
89
90 expect_error(vcovCR(obj_un, cluster = Cigs$state, type = "CR0", target = 1 / Cigs$population, inverse_var = TRUE))
91
92 wCR0 <- vcovCR(obj_wt, cluster = Cigs$state, type = "CR0")
93 attr(wCR0, "target") <- NULL
94 attr(wCR0, "inverse_var") <- NULL
95 wCR0_A <- vcovCR(obj_wt, cluster = Cigs$state, type = "CR0", target = 1 / Cigs$population)
96 attr(wCR0_A, "target") <- NULL
97 attr(wCR0_A, "inverse_var") <- NULL
98 expect_identical(wCR0_A, wCR0)
99 wCR0_B <- vcovCR(obj_wt, cluster = Cigs$state, type = "CR0", target = 1 / Cigs$population, inverse_var = FALSE)
100 attr(wCR0_B, "target") <- NULL
101 attr(wCR0_B, "inverse_var") <- NULL
102 expect_identical(wCR0_B, wCR0)
103
104 expect_error(vcovCR(obj_wt, cluster = Cigs$state, type = "CR0", target = 1 / Cigs$population, inverse_var = TRUE))
105 })
106
107 test_that("vcovCR options work for CR2", {
108
109 CR2_iv <- vcovCR(obj_un, cluster = Cigs$state, type = "CR2")
110 expect_equal(vcovCR(obj_un, cluster = Cigs$state, type = "CR2", target = rep(1, nobs(obj_un))), CR2_iv)
111 expect_false(identical(vcovCR(obj_un, cluster = Cigs$state, type = "CR2", target = 1 / Cigs$population), CR2_iv))
112
113 wCR2_id <- vcovCR(obj_wt, cluster = Cigs$state, type = "CR2")
114 expect_identical(vcovCR(obj_wt, cluster = Cigs$state, type = "CR2", inverse_var = FALSE), wCR2_id)
115 expect_identical(vcovCR(obj_wt, cluster = Cigs$state, type = "CR2", target = rep(1, nobs(obj_un))), wCR2_id)
116 expect_identical(vcovCR(obj_wt, cluster = Cigs$state, type = "CR2", target = rep(1, nobs(obj_un)), inverse_var = FALSE), wCR2_id)
117
118 })
119
120 test_that("vcovCR options work for CR4", {
121
122 CR4_not <- vcovCR(obj_un, cluster = Cigs$state, type = "CR4")
123 expect_identical(vcovCR(obj_un, cluster = Cigs$state, type = "CR4", target = rep(1, nobs(obj_un))), CR4_not)
124 expect_identical(vcovCR(obj_un, cluster = Cigs$state, type = "CR4", target = rep(1, nobs(obj_un)), inverse_var = FALSE), CR4_not)
125 expect_false(identical(vcovCR(obj_un, cluster = Cigs$state, type = "CR4", target = 1 / Cigs$population), CR4_not))
126
127 wCR4_id <- vcovCR(obj_wt, cluster = Cigs$state, type = "CR4")
128 expect_identical(vcovCR(obj_wt, cluster = Cigs$state, type = "CR4", inverse_var = FALSE), wCR4_id)
129 expect_identical(vcovCR(obj_wt, cluster = Cigs$state, type = "CR4", target = rep(1, nobs(obj_wt))), wCR4_id)
130 expect_identical(vcovCR(obj_wt, cluster = Cigs$state, type = "CR4", target = rep(1, nobs(obj_wt)), inverse_var = FALSE), wCR4_id)
131
132 })
133
134
135 test_that("CR2 is target-unbiased", {
136 expect_true(check_CR(obj_un, vcov = "CR2", cluster = Cigs$state))
137 expect_true(check_CR(obj_wt, vcov = "CR2", cluster = Cigs$state))
138 })
139
140 test_that("CR4 is target-unbiased", {
141 skip("Need to understand target-unbiasedness for ivreg objects.")
142 expect_true(check_CR(obj_un, vcov = "CR4", cluster = Cigs$state))
143 expect_true(check_CR(obj_wt, vcov = "CR4", cluster = Cigs$state))
144 })
145
146 test_that("vcovCR is equivalent to vcovHC (with HC0 or HC1) when clusters are all of size 1", {
147 library(sandwich, quietly=TRUE)
148 CR0 <- vcovCR(obj_un, cluster = 1:nobs(obj_un), type = "CR0")
149 expect_equal(vcovHC(obj_un, type = "HC0"), as.matrix(CR0))
150 CR1 <- vcovCR(obj_un, cluster = 1:nobs(obj_un), type = "CR1S")
151 expect_equal(vcovHC(obj_un, type = "HC1"), as.matrix(CR1))
152 CR2 <- vcovCR(obj_un, cluster = 1:nobs(obj_un), type = "CR2")
153 expect_false(all(vcovHC(obj_un, type = "HC2") == as.matrix(CR2)))
154 CR3 <- vcovCR(obj_un, cluster = 1:nobs(obj_un), type = "CR3")
155 expect_false(all(vcovHC(obj_un, type = "HC3") == as.matrix(CR3)))
156 })
157
158 test_that("Order doesn't matter.",{
159
160 check_sort_order(obj_wt, Cigs, "state")
161
162 })
163
164 test_that("clubSandwich works with dropped observations", {
165 dat_miss <- Cigs
166 dat_miss$rincome[sample.int(nrow(Cigs), size = round(nrow(Cigs) / 10))] <- NA
167 iv_dropped <- update(obj_un, data = dat_miss)
168 dat_complete <- subset(dat_miss, !is.na(rincome))
169 iv_complete <- update(obj_un, data = dat_complete)
170
171 CR_drop <- lapply(CR_types, function(x) vcovCR(iv_dropped, cluster = dat_miss$state, type = x))
172 CR_complete <- lapply(CR_types, function(x) vcovCR(iv_complete, cluster = dat_complete$state, type = x))
173 expect_equal(CR_drop, CR_complete)
174
175 test_drop <- lapply(CR_types, function(x) coef_test(iv_dropped, vcov = x, cluster = dat_miss$state, test = "All", p_values = FALSE))
176 test_complete <- lapply(CR_types, function(x) coef_test(iv_complete, vcov = x, cluster = dat_complete$state, test = "All", p_values = FALSE))
177 expect_equal(test_drop, test_complete)
178 })
179
180
181
182 test_that("weight scale doesn't matter", {
183
184 iv_fit_w <- update(obj_un, weights = rep(4, nobs(obj_un)))
185
186 unweighted_fit <- lapply(CR_types, function(x) vcovCR(obj_un, cluster = Cigs$state, type = x))
187 weighted_fit <- lapply(CR_types, function(x) vcovCR(iv_fit_w, cluster = Cigs$state, type = x))
188
189 expect_equal(lapply(unweighted_fit, as.matrix),
190 lapply(weighted_fit, as.matrix),
191 tol = 5 * 10^-7)
192
193 target <- 1 + rpois(nrow(Cigs), lambda = 8)
194 unweighted_fit <- lapply(CR_types, function(x) vcovCR(obj_un, cluster = Cigs$state, type = x, target = target))
195 weighted_fit <- lapply(CR_types, function(x) vcovCR(iv_fit_w, cluster = Cigs$state, type = x, target = target * 15))
196
197 expect_equal(lapply(unweighted_fit, as.matrix),
198 lapply(weighted_fit, as.matrix),
199 tol = 5 * 10^-7)
200
201 })
202
203 test_that("clubSandwich works with weights of zero.", {
204
205 n_Cigs <- nrow(Cigs)
206 Cigs$awt <- rpois(n_Cigs, lambda = 1.4)
207 table(Cigs$awt)
208
209 iv_full <- update(obj_un, weights = awt)
210 Cigs_sub <- subset(Cigs, awt > 0)
211 iv_sub <- update(iv_full, data = Cigs_sub)
212
213 CR_full <- lapply(CR_types, function(x) vcovCR(iv_full, cluster = Cigs$state, type = x))
214 CR_sub <- lapply(CR_types, function(x) vcovCR(iv_sub, cluster = Cigs_sub$state, type = x))
215 expect_equal(CR_full, CR_sub, check.attributes = FALSE)
216
217 test_full <- lapply(CR_types, function(x) coef_test(iv_full, vcov = x, cluster = Cigs$state, test = c("z","naive-t","Satterthwaite"), p_values = TRUE))
218 test_sub <- lapply(CR_types, function(x) coef_test(iv_sub, vcov = x, cluster = Cigs_sub$state, test = c("z","naive-t","Satterthwaite"), p_values = TRUE))
219 expect_equal(test_full, test_sub, check.attributes = FALSE)
220
221 dat_miss <- Cigs
222 miss_indicator <- sample.int(n_Cigs, size = round(n_Cigs / 10))
223 dat_miss$rincome[miss_indicator] <- NA
224 with(dat_miss, table(awt, is.na(rincome)))
225
226 iv_dropped <- update(iv_full, data = dat_miss)
227 dat_complete <- subset(dat_miss, !is.na(rincome))
228 iv_complete <- update(iv_full, data = dat_complete)
229
230 CR_drop <- lapply(CR_types, function(x) vcovCR(iv_dropped, cluster = dat_miss$state, type = x))
231 CR_complete <- lapply(CR_types, function(x) vcovCR(iv_complete, cluster = dat_complete$state, type = x))
232 expect_equal(CR_drop, CR_complete)
233
234 test_drop <- lapply(CR_types, function(x) coef_test(iv_dropped, vcov = x, cluster = dat_miss$state, test = "All", p_values = FALSE))
235 test_complete <- lapply(CR_types, function(x) coef_test(iv_complete, vcov = x, cluster = dat_complete$state, test = "All", p_values = FALSE))
236 expect_equal(test_drop, test_complete)
237
238 })
239