Debian Codegraph @

inst/doc/panel-data-CRVE.R

raw
1 ## ----message = FALSE, warning = FALSE-------------------------------------------------------------
2 library(clubSandwich)
3 data(MortalityRates)
4
5 # subset for deaths in motor vehicle accidents, 1970-1983
6 MV_deaths <- subset(MortalityRates, cause=="Motor Vehicle" &
7 year <= 1983 & !is.na(beertaxa))
8
9 # fit by OLS
10 lm_unweighted <- lm(mrate ~ 0 + legal + beertaxa +
11 factor(state) + factor(year), data = MV_deaths)
12
13 ## -------------------------------------------------------------------------------------------------
14 coef_test(lm_unweighted, vcov = "CR1",
15 cluster = MV_deaths$state, test = "naive-t")[1:2,]
16
17 ## -------------------------------------------------------------------------------------------------
18 coef_test(lm_unweighted, vcov = "CR2",
19 cluster = MV_deaths$state, test = "Satterthwaite")[1:2,]
20
21 ## ----echo = FALSE, results = "asis"---------------------------------------------------------------
22 plm_available <- requireNamespace("plm", quietly = TRUE)
23 if (!plm_available) cat("## Building the remainder of the vignette requires the plm package. Please install it. {-}")
24
25 ## ----message = FALSE, eval = plm_available--------------------------------------------------------
26 library(plm)
27 plm_unweighted <- plm(mrate ~ legal + beertaxa, data = MV_deaths,
28 effect = "twoways", index = c("state","year"))
29 coef_test(plm_unweighted, vcov = "CR1", cluster = "individual", test = "naive-t")
30 coef_test(plm_unweighted, vcov = "CR2", cluster = "individual", test = "Satterthwaite")
31
32 ## -------------------------------------------------------------------------------------------------
33 lm_weighted <- lm(mrate ~ 0 + legal + beertaxa + factor(state) + factor(year),
34 weights = pop, data = MV_deaths)
35 coef_test(lm_weighted, vcov = "CR1",
36 cluster = MV_deaths$state, test = "naive-t")[1:2,]
37 coef_test(lm_weighted, vcov = "CR2",
38 cluster = MV_deaths$state, test = "Satterthwaite")[1:2,]
39
40 ## -------------------------------------------------------------------------------------------------
41 coef_test(lm_weighted, vcov = "CR2",
42 cluster = MV_deaths$state, target = 1 / MV_deaths$pop,
43 test = "Satterthwaite")[1:2,]
44
45 ## ----eval = plm_available-------------------------------------------------------------------------
46 plm_random <- plm(mrate ~ 0 + legal + beertaxa + year, data = MV_deaths,
47 effect = "individual", index = c("state","year"),
48 model = "random")
49 coef_test(plm_random, vcov = "CR1", test = "naive-t")[1:2,]
50 coef_test(plm_random, vcov = "CR2", test = "Satterthwaite")[1:2,]
51
52 ## ----eval = plm_available-------------------------------------------------------------------------
53 MV_deaths <- within(MV_deaths, {
54 legal_cent <- legal - tapply(legal, state, mean)[factor(state)]
55 beer_cent <- beertaxa - tapply(beertaxa, state, mean)[factor(state)]
56 })
57
58 plm_Hausman <- plm(mrate ~ 0 + legal + beertaxa + legal_cent + beer_cent + factor(year),
59 data = MV_deaths,
60 effect = "individual", index = c("state","year"),
61 model = "random")
62 coef_test(plm_Hausman, vcov = "CR2", test = "Satterthwaite")[1:4,]
63
64 ## ----eval = plm_available-------------------------------------------------------------------------
65 Wald_test(plm_Hausman,
66 constraints = constrain_zero(c("legal_cent","beer_cent")),
67 vcov = "CR1", test = "chi-sq")
68
69 ## ----eval = plm_available-------------------------------------------------------------------------
70 Wald_test(plm_Hausman,
71 constraints = constrain_zero(c("legal_cent","beer_cent")),
72 vcov = "CR2")
73