Debian Codegraph @

tests/testthat/test_ignore_absorption.R

raw
1 context("ignoring absorbed fixed effects")
2 set.seed(20190513)
3
4 skip_if_not_installed("plm")
5
6 library(plm)
7
8 data(MortalityRates)
9 MV_Mortality <- subset(MortalityRates, cause=="Motor Vehicle" & state %in% 1:8)
10 table(MV_Mortality$state)
11 MV_Mortality$state_fac <- factor(MV_Mortality$state)
12 # MV_Mortality$pop <- with(MV_Mortality, 1 + rbinom(nlevels(state_fac), size = 4, prob = 0.5)[state_fac])
13 summary(MV_Mortality$pop)
14 MV_Mortality$pop_scale <- with(MV_Mortality, pop / mean(pop))
15 summary(MV_Mortality$pop_scale)
16
17 # model specification
18
19 specification <- mrate ~ 0 + legal + beertaxa + beerpercap + winepercap + factor(state)
20
21 #-----------------------
22 # unweighted
23 #-----------------------
24
25 ols_LSDV <- lm(specification, data = MV_Mortality)
26 ols_within <- plm(update(specification, . ~ . - 0 - factor(state)), data = MV_Mortality, effect = "individual", index = c("state","year"))
27
28 test_that("Unweighted lsdv and within estimators are equivalent", {
29 lsdv <- coef_test(ols_LSDV, vcov = "CR2", cluster = MV_Mortality$state, coefs = 1:4, p_values = FALSE)
30 wthn <- coef_test(ols_within, vcov = "CR2", p_values = FALSE)
31 expect_equal(lsdv, wthn)
32 })
33
34 #-----------------------
35 # iv-weights
36 #-----------------------
37
38 wls_LSDV <- lm(specification, weights = pop_scale, data = MV_Mortality)
39
40 MV_Mortality_full <- model.frame(lm(specification, weights = pop_scale, data = MV_Mortality))
41 U_mat <- model.matrix(update(specification, . ~ . - factor(state)), data = MV_Mortality_full)
42 T_mat <- model.matrix(~ factor(state), data = MV_Mortality_full)
43 w <- MV_Mortality_full$"(weights)"
44 state <- MV_Mortality_full$"factor(state)"
45 U_absorb <- residuals(stats:::lm.wfit(x = T_mat, y = U_mat, w = w))[,-31]
46 Y_absorb <- residuals(stats:::lm.wfit(x = T_mat, y = MV_Mortality_full$mrate, w = w))
47 wls_within <- lm(Y_absorb ~ 0 + U_absorb, weights = w)
48
49 test_that("Inverse-variance weighted lsdv and within estimators are equivalent.", {
50 lsdv <- coef_test(wls_LSDV, vcov = "CR2", cluster = MV_Mortality$state, inverse_var = TRUE, p_values = FALSE)[1:4,]
51 wthn <- coef_test(wls_within, vcov = "CR2", cluster = state, inverse_var = TRUE, p_values = FALSE)[1:4,]
52 expect_equal(lsdv$beta, wthn$beta, check.attributes = FALSE, tolerance = 10^-5)
53 expect_equal(lsdv$SE, wthn$SE, check.attributes = FALSE, tolerance = 10^-2)
54 expect_equal(lsdv$tstat, wthn$tstat, check.attributes = FALSE, tolerance = 10^-2)
55 expect_equal(lsdv$df_Satt, wthn$df_Satt, check.attributes = FALSE, tolerance = 10^-2)
56 })
57
58 #-----------------------
59 # p-weights
60 #-----------------------
61
62 test_that("Probability-weighted lsdv and within estimators are not necessarily equivalent.", {
63 lsdv <- coef_test(wls_LSDV, vcov = "CR2", cluster = MV_Mortality$state, inverse_var = FALSE, coefs = 1:4, p_values = FALSE)
64 wthn <- coef_test(wls_within, vcov = "CR2", cluster = state, inverse_var = FALSE, p_values = FALSE)
65 expect_equal(lsdv$beta, wthn$beta, check.attributes = FALSE, tolerance = 10^-5)
66 expect_equal(lsdv$SE, wthn$SE, check.attributes = FALSE, tolerance = 10^-2)
67 expect_equal(lsdv$tstat, wthn$tstat, check.attributes = FALSE, tolerance = 10^-2)
68 expect_equal(lsdv$df_Satt, wthn$df_Satt, check.attributes = FALSE, tolerance = 10^-2)
69 })
70
71
72