Debian Codegraph @

tests/testthat/test_estfun.R

raw
1 context("estfun objects")
2
3 skip_if_not_installed("zoo")
4 skip_if_not_installed("AER")
5
6 library(zoo, quietly=TRUE)
7 library(AER, quietly=TRUE)
8
9 CR_types <- paste0("CR",0:4)
10
11 data("CigarettesSW", package = "AER")
12
13 Cigs <- within(CigarettesSW, {
14 rprice <- price/cpi
15 rincome <- income/population/cpi
16 tdiff <- (taxs - tax)/cpi
17 })
18
19 obj_un <- ivreg(log(packs) ~ log(rprice) + log(rincome) + I(tax/cpi) | log(rincome) + tdiff + I(tax/cpi),
20 data = Cigs)
21 obj_wt <- ivreg(log(packs) ~ log(rprice) + log(rincome) + I(tax/cpi) | log(rincome) + tdiff + I(tax/cpi),
22 data = Cigs,
23 weights = population)
24
25 red_form_un <- lm(log(packs) ~ log(rincome) + I(tax/cpi) + tdiff, data = Cigs)
26 red_form_wt <- lm(log(packs) ~ log(rincome) + I(tax/cpi) + tdiff, data = Cigs, weights = population)
27 stage1_un <- lm(log(rprice) ~ log(rincome) + I(tax/cpi) + tdiff, data = Cigs)
28 stage1_wt <- lm(log(rprice) ~ log(rincome) + I(tax/cpi) + tdiff, data = Cigs, weights = population)
29
30 test_that("estfun works for lm.", {
31
32 V_CR <- lapply(CR_types, function(type) as.matrix(vcovCR(obj = red_form_un, cluster = Cigs$state, type = type)))
33 e_CR <- lapply(CR_types, function(type) vcovCR(obj = red_form_un, cluster = Cigs$state, type = type, form = "estfun"))
34 expect_equal(lapply(e_CR, tcrossprod), V_CR)
35
36 V_CR <- lapply(CR_types, function(type) as.matrix(vcovCR(obj = red_form_wt, cluster = Cigs$state, type = type)))
37 e_CR <- lapply(CR_types, function(type) vcovCR(obj = red_form_wt, cluster = Cigs$state, type = type, form = "estfun"))
38 expect_equal(lapply(e_CR, tcrossprod), V_CR)
39
40 V_CR <- lapply(CR_types, function(type) as.matrix(vcovCR(obj = stage1_un, cluster = Cigs$state, type = type)))
41 e_CR <- lapply(CR_types, function(type) vcovCR(obj = stage1_un, cluster = Cigs$state, type = type, form = "estfun"))
42 expect_equal(lapply(e_CR, tcrossprod), V_CR)
43
44 V_CR <- lapply(CR_types, function(type) as.matrix(vcovCR(obj = stage1_wt, cluster = Cigs$state, type = type)))
45 e_CR <- lapply(CR_types, function(type) vcovCR(obj = stage1_wt, cluster = Cigs$state, type = type, form = "estfun"))
46 expect_equal(lapply(e_CR, tcrossprod), V_CR)
47
48 })
49
50 test_that("stacked estimating equations are equivalent to 2SLS.", {
51
52 e_CR <- lapply(CR_types, function(type) vcovCR(obj = red_form_un, cluster = Cigs$state, type = type, form = "estfun"))
53 f_CR <- lapply(CR_types, function(type) vcovCR(obj = stage1_un, cluster = Cigs$state, type = type, form = "estfun"))
54
55 V_CR_stack <- mapply(function(x, y) tcrossprod(rbind(x, y)), x = e_CR, y = f_CR, SIMPLIFY = FALSE)
56 gamma <- coef(stage1_un)["tdiff"]
57 beta <- coef(red_form_un)["tdiff"]
58 delta <- beta / gamma
59
60 V_CR_2SLS <- lapply(CR_types, function(type) vcovCR(obj = obj_un, cluster = Cigs$state, type = type))
61 V_CR_2SLS <- sapply(V_CR_2SLS, function(x) diag(x)["log(rprice)"])
62 V_delta <- sapply(V_CR_stack, function(x) sum(x[c(4,8), c(4,8)] * tcrossprod(c(1,-delta))) / gamma^2)
63 })