Debian Codegraph @

tests/testthat/test-systemfit.R

raw
1 skip_on_cran()
2
3 skip_if_not_installed("modeltests")
4 library(modeltests)
5
6 skip_if_not_installed("systemfit")
7 suppressPackageStartupMessages(library(systemfit))
8
9 # Testing using systemfit examples ----------------------------------------
10
11 data("Kmenta")
12 eqDemand <- consump ~ price + income
13 eqSupply <- consump ~ price + farmPrice + trend
14 system <- list(demand = eqDemand, supply = eqSupply)
15
16 # OLS estimation
17 fitols <- systemfit(system, data = Kmenta)
18
19 test_that("tidy.systemfit with OLS", {
20 td <- tidy(fitols)
21 check_tidy_output(td)
22 check_dims(td, 7, 7)
23
24 td <- tidy(fitols, conf.level = .99)
25 check_tidy_output(td)
26 check_dims(td, 7, 7)
27 })
28
29 # OLS estimation with 2 restrictions
30 Rrestr <- matrix(0, 2, 7)
31 Rrestr[1, 3] <- 1
32 Rrestr[1, 7] <- -1
33 Rrestr[2, 2] <- -1
34 Rrestr[2, 5] <- 1
35
36 qrestr <- c(0, 0.5)
37
38 fitols2 <- systemfit(
39 system,
40 data = Kmenta,
41 restrict.matrix = Rrestr,
42 restrict.rhs = qrestr
43 )
44
45 test_that("tidy.systemfit with OLS 2 restrictions", {
46 td <- tidy(fitols2)
47 check_tidy_output(td)
48 check_dims(td, 7, 7)
49 })
50
51
52 # OLS estimation with the same 2 restrictions in symbolic form
53 restrict <- c(
54 "demand_income - supply_trend = 0",
55 "- demand_price + supply_price = 0.5"
56 )
57 fitols2b <- systemfit(system, data = Kmenta, restrict.matrix = restrict)
58
59 test_that("tidy.systemfit with OLS 2 restrictions", {
60 td <- tidy(fitols2b)
61 check_tidy_output(td)
62 check_dims(td, 7, 7)
63 })
64
65 # OLS with restrictions on the coefficients by modifying the regressor matrix
66 # with argument restrict.regMat
67 modReg <- matrix(0, 7, 6)
68 colnames(modReg) <- c(
69 "demIntercept",
70 "demPrice",
71 "demIncome",
72 "supIntercept",
73 "supPrice2",
74 "supTrend"
75 )
76
77 modReg[1, "demIntercept"] <- 1
78 modReg[2, "demPrice"] <- 1
79 modReg[3, "demIncome"] <- 1
80 modReg[4, "supIntercept"] <- 1
81 modReg[5, "supPrice2"] <- 1
82 modReg[6, "supPrice2"] <- 1
83 modReg[7, "supTrend"] <- 1
84
85 fitols3 <- systemfit(system, data = Kmenta, restrict.regMat = modReg)
86
87 test_that("tidy.systemfit with OLS 2 restrictions and modifications", {
88 td <- tidy(fitols3)
89 check_tidy_output(td)
90 check_dims(td, 7, 7)
91 })
92
93 # iterated SUR estimation
94 fitsur <- systemfit(system, "SUR", data = Kmenta, maxit = 100)
95
96 test_that("tidy.systemfit with SUR", {
97 td <- tidy(fitsur)
98 check_tidy_output(td)
99 check_dims(td, 7, 7)
100 })
101
102
103 # 2SLS estimation
104 inst <- ~ income + farmPrice + trend
105 fit2sls <- systemfit(system, "2SLS", inst = inst, data = Kmenta)
106 test_that("tidy.systemfit with 2SLS", {
107 td <- tidy(fit2sls)
108 check_tidy_output(td)
109 check_dims(td, 7, 7)
110 })
111
112
113 # 2SLS estimation with different instruments in each equation
114 inst1 <- ~ income + farmPrice
115 inst2 <- ~ income + farmPrice + trend
116
117 instlist <- list(inst1, inst2)
118
119 fit2sls2 <- systemfit(system, "2SLS", inst = instlist, data = Kmenta)
120
121 test_that("tidy.systemfit with 2SLS with different instruments", {
122 td <- tidy(fit2sls2)
123 check_tidy_output(td)
124 check_dims(td, 7, 7)
125 })
126
127 # 3SLS estimation with GMM-3SLS formula
128 inst <- ~ income + farmPrice + trend
129
130 fit3sls <- systemfit(
131 system,
132 "3SLS",
133 inst = inst,
134 data = Kmenta,
135 method3sls = "GMM"
136 )
137
138 test_that("tidy.systemfit with 3SLS", {
139 td <- tidy(fit3sls)
140 check_tidy_output(td)
141 check_dims(td, 7, 7)
142 })