Debian Codegraph @

tests/testthat/test_plm-ID-variables.R

raw
1 context("plm objects - ID variables")
2 set.seed(20190513)
3
4 skip_if_not_installed("plm")
5 library(plm, quietly=TRUE)
6
7 data("Produc", package = "plm")
8 Produc <- Produc[sample(nrow(Produc)),]
9 Produc$cluster <- sample(LETTERS[1:10], size = nrow(Produc), replace=TRUE)
10
11
12 plm_individual <- plm(log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp,
13 data = Produc, index = "state",
14 effect = "individual", model = "within")
15
16 lm_individual <- lm(log(gsp) ~ 0 + state + log(pcap) + log(pc) + log(emp) + unemp, data = Produc)
17 individual_names <- names(coef(plm_individual))
18 individual_index <- names(coef(lm_individual)) %in% individual_names
19
20 lm_CR0 <- vcovCR(lm_individual, cluster = Produc$state, type = "CR0")[individual_index,individual_index]
21 lm_CR1 <- vcovCR(lm_individual, cluster = Produc$state, type = "CR1")[individual_index,individual_index]
22 lm_CR2 <- vcovCR(lm_individual, cluster = Produc$state, type = "CR2")[individual_index,individual_index]
23
24 plm_CR0 <- vcovCR(plm_individual, type="CR0")[individual_names,individual_names]
25
26 test_that("individual effects agree with lm under automatic clustering", {
27 plm_CR1 <- vcovCR(plm_individual, type="CR1")[individual_names,individual_names]
28 plm_CR2 <- vcovCR(plm_individual, type="CR2")[individual_names,individual_names]
29
30 expect_equal(plm_CR0, lm_CR0)
31 expect_equal(plm_CR1, lm_CR1)
32 expect_equal(plm_CR2, lm_CR2)
33 })
34
35 test_that("individual effects agree with lm under explicit clustering", {
36 plm_CR1 <- vcovCR(plm_individual, cluster = Produc$state, type="CR1")[individual_names,individual_names]
37 plm_CR2 <- vcovCR(plm_individual, cluster = Produc$state, type="CR2")[individual_names,individual_names]
38
39 expect_equal(plm_CR1, lm_CR1)
40 expect_equal(plm_CR2, lm_CR2)
41 })
42
43 test_that("individual effects agree with lm under random clustering", {
44
45 lm_CR1 <- vcovCR(lm_individual, cluster = Produc$cluster, type = "CR1")[individual_index,individual_index]
46 lm_CR2 <- vcovCR(lm_individual, cluster = Produc$cluster, type = "CR2")[individual_index,individual_index]
47
48 plm_CR1 <- vcovCR(plm_individual, cluster = Produc$cluster, type="CR1")[individual_names,individual_names]
49 plm_CR2 <- vcovCR(plm_individual, cluster = Produc$cluster, type="CR2")[individual_names,individual_names]
50
51 expect_equal(plm_CR1, lm_CR1)
52 expect_equal(plm_CR2, lm_CR2)
53 })
54
55 test_that("CR0 and CR1S agree with arellano vcov", {
56 expect_equal(vcovHC(plm_individual, method="arellano", type = "HC0", cluster = "group"),
57 as.matrix(plm_CR0), check.attributes = FALSE)
58 expect_equal(vcovHC(plm_individual, method="arellano", type = "sss", cluster = "group"),
59 as.matrix(vcovCR(plm_individual, type = "CR1S")), check.attributes = FALSE)
60 })
61
62
63
64 test_that("plm works for Yuki Takahashi's reprex.",{
65 N <- 100
66 id <- rep(1:N, 2)
67 gid <- rep(1:(N/2), 4)
68 Trt <- rep(c(0,1), each = N)
69 a <- rep(rnorm(N, mean=0, sd=0.005), 2)
70 gp <- rep(rnorm(N / 2, mean=0, sd=0.0005), 4)
71 u <- rnorm(N * 2, mean=0, sd=0.05)
72 Ylatent <- -0.05 * Trt + gp + a + u
73
74 Data <- data.frame(
75 Y = ifelse(Ylatent > 0, 1, 0),
76 id, gid, Trt
77 )
78
79 fe_fit <- plm(formula = Y ~ Trt, data = Data,
80 model = "within", index = "id",
81 effect = "individual")
82
83
84 implicit <- vcovCR(fe_fit, type = "CR2")
85 explicit <- vcovCR(fe_fit, cluster=Data$id, type = "CR2")
86
87 expect_equal(implicit, explicit)
88 expect_s3_class(vcovCR(fe_fit, cluster=Data$gid, type = "CR2"), "vcovCR")
89
90 })
91
92
93
94 test_that("Clustering works for various ways of specifying unit and time indices in plm.", {
95
96 data("Grunfeld", package = "plm")
97 Grunfeld$cluster <- sample(LETTERS[1:10], size = nrow(Grunfeld), replace=TRUE)
98 rearrange <- mapply(function(s,b) seq(s, nrow(Grunfeld), b), 1:10, 11:20)
99 rearrange <- unique(unlist(rearrange))
100 rearrange <- c(rearrange, setdiff(1:200, rearrange))
101 Grunfeld_scramble <- Grunfeld[rearrange,]
102 Grunfeld_pdata <- pdata.frame(Grunfeld_scramble, index = c("firm","year"))
103
104 plm_pdata <- plm(inv ~ value + capital, data = Grunfeld_pdata, model="within")
105 plm_numeric <- plm(inv ~ value + capital, data = Grunfeld, index = 10, model="within")
106 plm_noindex <- plm(inv ~ value + capital, data = Grunfeld_scramble, model="within")
107 plm_oneindex <- plm(inv ~ value + capital, data = Grunfeld_scramble, index = "firm", model="within")
108 plm_twoindex <- plm(inv ~ value + capital, data = Grunfeld_scramble, index = c("firm","year"), model="within")
109
110 CR_types <- paste0("CR",0:3)
111
112 # auto clustering
113 vcovCRs <- function(model, types) lapply(types, function(x) vcovCR(model, type = x))
114 CR_pdata <- vcovCRs(plm_pdata, CR_types)
115 expect_equivalent(CR_pdata, vcovCRs(plm_numeric, CR_types))
116 expect_equivalent(CR_pdata, vcovCRs(plm_noindex, CR_types))
117 expect_equivalent(CR_pdata, vcovCRs(plm_oneindex, CR_types))
118 expect_equivalent(CR_pdata, vcovCRs(plm_twoindex, CR_types))
119
120 # manual clustering on firm
121 vcovCRs <- function(model, types, cluster) lapply(types, function(x) vcovCR(model, type = x, cluster = cluster))
122 expect_equivalent(CR_pdata, vcovCRs(plm_numeric, CR_types, cluster = Grunfeld$firm))
123 expect_equivalent(CR_pdata, vcovCRs(plm_noindex, CR_types, cluster = Grunfeld_scramble$firm))
124 expect_equivalent(CR_pdata, vcovCRs(plm_oneindex, CR_types, cluster = Grunfeld_scramble$firm))
125 expect_equivalent(CR_pdata, vcovCRs(plm_twoindex, CR_types, cluster = Grunfeld_scramble$firm))
126
127 # manual clustering on arbitrary id
128 CR_pdata <- vcovCRs(plm_pdata, CR_types, cluster = Grunfeld_pdata$cluster)
129 expect_equivalent(CR_pdata, vcovCRs(plm_numeric, CR_types, cluster = Grunfeld$cluster))
130 expect_equivalent(CR_pdata, vcovCRs(plm_noindex, CR_types, cluster = Grunfeld_scramble$cluster))
131 expect_equivalent(CR_pdata, vcovCRs(plm_oneindex, CR_types, cluster = Grunfeld_scramble$cluster))
132 expect_equivalent(CR_pdata, vcovCRs(plm_twoindex, CR_types, cluster = Grunfeld_scramble$cluster))
133
134 })
135
136
137 test_that("findCluster works for plm objects.", {
138
139 pool_individual <- plm(log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp,
140 data = Produc, index = c("state","year","region"),
141 effect = "individual", model = "pooling")
142 index <- attr(model.frame(pool_individual),"index")
143
144 expect_equal(findCluster.plm(pool_individual), index$state)
145 expect_equal(findCluster.plm(pool_individual, "individual"), index$state)
146 expect_equal(findCluster.plm(pool_individual, "time"), index$year)
147 expect_equal(findCluster.plm(pool_individual, "group"), index$region)
148 expect_equal(findCluster.plm(pool_individual, "state"), index$state)
149 expect_equal(findCluster.plm(pool_individual, "year"), index$year)
150 expect_equal(findCluster.plm(pool_individual, "region"), index$region)
151 expect_equal(findCluster.plm(pool_individual, Produc$region), index$region)
152
153 within_individual <- plm(log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp,
154 data = Produc, index = c("state","year","region"),
155 effect = "individual", model = "within")
156 expect_equal(findCluster.plm(within_individual), index$state)
157 expect_equal(findCluster.plm(within_individual, "individual"), index$state)
158 expect_equal(findCluster.plm(within_individual, "time"), index$year)
159 expect_equal(findCluster.plm(within_individual, "group"), index$region)
160 expect_equal(findCluster.plm(within_individual, "state"), index$state)
161 expect_equal(findCluster.plm(within_individual, "year"), index$year)
162 expect_equal(findCluster.plm(within_individual, "region"), index$region)
163 expect_equal(findCluster.plm(within_individual, Produc$region), index$region)
164
165 between_individual <- plm(log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp,
166 data = Produc, index = c("state","year","region"),
167 effect = "individual", model = "between")
168
169 expect_equal(findCluster.plm(between_individual), index$state)
170 expect_equal(findCluster.plm(between_individual, "individual"), index$state)
171 expect_equal(findCluster.plm(between_individual, "time"), index$year)
172 expect_equal(findCluster.plm(between_individual, "group"), index$region)
173 expect_equal(findCluster.plm(between_individual, "state"), index$state)
174 expect_equal(findCluster.plm(between_individual, "year"), index$year)
175 expect_equal(findCluster.plm(between_individual, "region"), index$region)
176 expect_equal(findCluster.plm(between_individual, Produc$region), index$region)
177
178 RE_individual <- plm(log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp,
179 data = Produc, index = c("state","year","region"),
180 effect = "individual", model = "random")
181
182 expect_equal(findCluster.plm(RE_individual), index$state)
183 expect_equal(findCluster.plm(RE_individual, "individual"), index$state)
184 expect_error(findCluster.plm(RE_individual, "time"))
185 expect_equal(findCluster.plm(RE_individual, "group"), index$region)
186 expect_equal(findCluster.plm(RE_individual, "state"), index$state)
187 expect_error(findCluster.plm(RE_individual, "year"))
188 expect_equal(findCluster.plm(RE_individual, "region"), index$region)
189 expect_equal(findCluster.plm(RE_individual, Produc$region), index$region)
190
191 pool_time <- plm(log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp,
192 data = Produc, index = c("state","year","region"),
193 effect = "time", model = "pooling")
194 expect_equal(findCluster.plm(pool_time), index$year)
195 expect_equal(findCluster.plm(pool_time, "individual"), index$state)
196 expect_equal(findCluster.plm(pool_time, "time"), index$year)
197 expect_equal(findCluster.plm(pool_time, "group"), index$region)
198 expect_equal(findCluster.plm(pool_time, "state"), index$state)
199 expect_equal(findCluster.plm(pool_time, "year"), index$year)
200 expect_equal(findCluster.plm(pool_time, "region"), index$region)
201 expect_equal(findCluster.plm(pool_time, Produc$region), index$region)
202
203 within_time <- plm(log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp,
204 data = Produc, index = c("state","year","region"),
205 effect = "time", model = "within")
206 expect_equal(findCluster.plm(within_time), index$year)
207 expect_equal(findCluster.plm(within_time, "individual"), index$state)
208 expect_equal(findCluster.plm(within_time, "time"), index$year)
209 expect_equal(findCluster.plm(within_time, "group"), index$region)
210 expect_equal(findCluster.plm(within_time, "state"), index$state)
211 expect_equal(findCluster.plm(within_time, "year"), index$year)
212 expect_equal(findCluster.plm(within_time, "region"), index$region)
213 expect_equal(findCluster.plm(within_time, Produc$region), index$region)
214
215 between_time <- plm(log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp,
216 data = Produc, index = c("state","year","region"),
217 effect = "time", model = "between")
218 expect_equal(findCluster.plm(between_time), index$year)
219 expect_equal(findCluster.plm(between_time, "individual"), index$state)
220 expect_equal(findCluster.plm(between_time, "time"), index$year)
221 expect_equal(findCluster.plm(between_time, "group"), index$region)
222 expect_equal(findCluster.plm(between_time, "state"), index$state)
223 expect_equal(findCluster.plm(between_time, "year"), index$year)
224 expect_equal(findCluster.plm(between_time, "region"), index$region)
225 expect_equal(findCluster.plm(between_time, Produc$region), index$region)
226
227 RE_time <- plm(log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp,
228 data = Produc, index = c("state","year","region"),
229 effect = "time", model = "random")
230 expect_equal(findCluster.plm(RE_time), index$year)
231 expect_error(findCluster.plm(RE_time, "individual"))
232 expect_equal(findCluster.plm(RE_time, "time"), index$year)
233 expect_error(findCluster.plm(RE_time, "group"))
234 expect_error(findCluster.plm(RE_time, "state"))
235 expect_equal(findCluster.plm(RE_time, "year"), index$year)
236 expect_error(findCluster.plm(RE_time, "region"))
237 expect_error(findCluster.plm(RE_time, Produc$region))
238
239 pool_twoways <- plm(log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp,
240 data = Produc, index = c("state","year","region"),
241 effect = "twoways", model = "pooling")
242 expect_error(findCluster.plm(pool_twoways))
243 expect_equal(findCluster.plm(pool_twoways, "individual"), index$state)
244 expect_equal(findCluster.plm(pool_twoways, "time"), index$year)
245 expect_equal(findCluster.plm(pool_twoways, "group"), index$region)
246 expect_equal(findCluster.plm(pool_twoways, "state"), index$state)
247 expect_equal(findCluster.plm(pool_twoways, "year"), index$year)
248 expect_equal(findCluster.plm(pool_twoways, "region"), index$region)
249 expect_equal(findCluster.plm(pool_twoways, Produc$region), index$region)
250
251 within_twoways <- plm(log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp,
252 data = Produc, index = c("state","year","region"),
253 effect = "twoways", model = "within")
254 expect_error(findCluster.plm(within_twoways))
255 expect_equal(findCluster.plm(within_twoways, "individual"), index$state)
256 expect_equal(findCluster.plm(within_twoways, "time"), index$year)
257 expect_equal(findCluster.plm(within_twoways, "group"), index$region)
258 expect_equal(findCluster.plm(within_twoways, "state"), index$state)
259 expect_equal(findCluster.plm(within_twoways, "year"), index$year)
260 expect_equal(findCluster.plm(within_twoways, "region"), index$region)
261 expect_equal(findCluster.plm(within_twoways, Produc$region), index$region)
262
263 RE_twoways <- plm(log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp,
264 data = Produc, index = c("state","year","region"),
265 effect = "twoways", model = "random")
266 expect_error(findCluster.plm(RE_twoways))
267 expect_error(findCluster.plm(RE_twoways, "individual"))
268 expect_error(findCluster.plm(RE_twoways, "time"))
269 expect_error(findCluster.plm(RE_twoways, "group"))
270 expect_error(findCluster.plm(RE_twoways, "state"))
271 expect_error(findCluster.plm(RE_twoways, "year"))
272 expect_error(findCluster.plm(RE_twoways, "region"))
273 expect_error(findCluster.plm(RE_twoways, Produc$region))
274
275 RE_nested <- plm(log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp,
276 data = Produc, index = c("state","year","region"),
277 effect = "nested", model = "random")
278 expect_equal(findCluster.plm(RE_nested), index$region)
279 expect_error(findCluster.plm(RE_nested, "individual"))
280 expect_error(findCluster.plm(RE_nested, "time"))
281 expect_equal(findCluster.plm(RE_nested, "group"), index$region)
282 expect_error(findCluster.plm(RE_nested, "state"))
283 expect_error(findCluster.plm(RE_nested, "year"))
284 expect_equal(findCluster.plm(RE_nested, "region"), index$region)
285 expect_equal(findCluster.plm(RE_nested, Produc$region), index$region)
286
287 })
288
289