Debian Codegraph @

tests/testthat/test_ivreg_ivreg.R

raw
1 #############################
2
3 context("ivreg::ivreg objects")
4 set.seed(20190513)
5
6 skip_if_not_installed("ivreg")
7
8 library(ivreg, 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::ivreg(log(packs) ~ log(rprice) + log(rincome) | log(rincome) + tdiff + I(tax/cpi),
21 data = Cigs)
22 obj_wt <- ivreg::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(XZ %*% chol2inv(chol(t(XZ) %*% XZ)) %*% t(XZ))
41 expect_equal(hatvalues(obj_un, type = "stage2"), 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 # Clustering variable must have length equal to the number of rows in the data used to fit obj.
164
165 test_that("clubSandwich works with dropped observations", {
166 dat_miss <- Cigs
167 dat_miss$rincome[sample.int(nrow(Cigs), size = round(nrow(Cigs) / 10))] <- NA
168 iv_dropped <- update(obj_un, data = dat_miss)
169 dat_complete <- subset(dat_miss, !is.na(rincome))
170 iv_complete <- update(obj_un, data = dat_complete)
171
172 CR_drop <- lapply(CR_types, function(x) vcovCR(iv_dropped, cluster = dat_miss$state, type = x))
173 CR_complete <- lapply(CR_types, function(x) vcovCR(iv_complete, cluster = dat_complete$state, type = x))
174 expect_equal(CR_drop, CR_complete)
175
176 test_drop <- lapply(CR_types, function(x) coef_test(iv_dropped, vcov = x, cluster = dat_miss$state, test = "All", p_values = FALSE))
177 test_complete <- lapply(CR_types, function(x) coef_test(iv_complete, vcov = x, cluster = dat_complete$state, test = "All", p_values = FALSE))
178 expect_equal(test_drop, test_complete)
179 })
180
181
182
183 test_that("weight scale doesn't matter", {
184
185 iv_fit_w <- update(obj_un, weights = rep(4, nobs(obj_un)))
186
187 unweighted_fit <- lapply(CR_types, function(x) vcovCR(obj_un, cluster = Cigs$state, type = x))
188 weighted_fit <- lapply(CR_types, function(x) vcovCR(iv_fit_w, cluster = Cigs$state, type = x))
189
190 expect_equal(lapply(unweighted_fit, as.matrix),
191 lapply(weighted_fit, as.matrix),
192 tol = 5 * 10^-7)
193
194 target <- 1 + rpois(nrow(Cigs), lambda = 8)
195 unweighted_fit <- lapply(CR_types, function(x) vcovCR(obj_un, cluster = Cigs$state, type = x, target = target))
196 weighted_fit <- lapply(CR_types, function(x) vcovCR(iv_fit_w, cluster = Cigs$state, type = x, target = target * 15))
197
198 expect_equal(lapply(unweighted_fit, as.matrix),
199 lapply(weighted_fit, as.matrix),
200 tol = 5 * 10^-7)
201
202 })
203
204 test_that("clubSandwich works with weights of zero.", {
205
206 n_Cigs <- nrow(Cigs)
207 Cigs$awt <- rpois(n_Cigs, lambda = 1.4)
208 table(Cigs$awt)
209
210 iv_full <- update(obj_un, weights = awt)
211 Cigs_sub <- subset(Cigs, awt > 0)
212 iv_sub <- update(iv_full, data = Cigs_sub)
213
214 CR_full <- lapply(CR_types, function(x) vcovCR(iv_full, cluster = Cigs$state, type = x))
215 CR_sub <- lapply(CR_types, function(x) vcovCR(iv_sub, cluster = Cigs_sub$state, type = x))
216 expect_equal(CR_full, CR_sub, check.attributes = FALSE)
217
218 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))
219 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))
220 expect_equal(test_full, test_sub, check.attributes = FALSE)
221
222 dat_miss <- Cigs
223 miss_indicator <- sample.int(n_Cigs, size = round(n_Cigs / 10))
224 dat_miss$rincome[miss_indicator] <- NA
225 with(dat_miss, table(awt, is.na(rincome)))
226
227 iv_dropped <- update(iv_full, data = dat_miss)
228 dat_complete <- subset(dat_miss, !is.na(rincome))
229 iv_complete <- update(iv_full, data = dat_complete)
230
231 CR_drop <- lapply(CR_types, function(x) vcovCR(iv_dropped, cluster = dat_miss$state, type = x))
232 CR_complete <- lapply(CR_types, function(x) vcovCR(iv_complete, cluster = dat_complete$state, type = x))
233 expect_equal(CR_drop, CR_complete)
234
235 test_drop <- lapply(CR_types, function(x) coef_test(iv_dropped, vcov = x, cluster = dat_miss$state, test = "All", p_values = FALSE))
236 test_complete <- lapply(CR_types, function(x) coef_test(iv_complete, vcov = x, cluster = dat_complete$state, test = "All", p_values = FALSE))
237 expect_equal(test_drop, test_complete)
238
239 })
240
241
242 #-------------------------------------------------------------------------------
243 # Other estimation methods
244
245 ols_un <- ivreg::ivreg(log(packs) ~ log(rprice) + log(rincome) | log(rincome) + tdiff + I(tax/cpi),
246 data = Cigs, method = "OLS")
247 ols_wt <- ivreg::ivreg(log(packs) ~ log(rprice) + log(rincome) | log(rincome) + tdiff + I(tax/cpi),
248 data = Cigs,
249 weights = population, method = "OLS")
250
251 mom_un <- ivreg::ivreg(log(packs) ~ log(rprice) + log(rincome) | log(rincome) + tdiff + I(tax/cpi),
252 data = Cigs, method = "M")
253 mom_wt <- ivreg::ivreg(log(packs) ~ log(rprice) + log(rincome) | log(rincome) + tdiff + I(tax/cpi),
254 data = Cigs,
255 weights = population, method = "M")
256
257 rob_un <- ivreg::ivreg(log(packs) ~ log(rprice) + log(rincome) | log(rincome) + tdiff + I(tax/cpi),
258 data = Cigs, method = "MM")
259 rob_wt <- ivreg::ivreg(log(packs) ~ log(rprice) + log(rincome) | log(rincome) + tdiff + I(tax/cpi),
260 data = Cigs,
261 weights = population, method = "MM")
262
263 test_that("vcovCR does not currently support ivreg models estimated using method = 'M' or method = 'MM'", {
264 expect_error(vcovCR(mom_un, cluster = Cigs$state, type = "CR2"))
265 expect_error(vcovCR(mom_wt, cluster = Cigs$state, type = "CR2"))
266 expect_error(vcovCR(rob_un, cluster = Cigs$state, type = "CR2"))
267 expect_error(vcovCR(rob_wt, cluster = Cigs$state, type = "CR2"))
268 })
269