-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmivvars.ado
158 lines (134 loc) · 4.14 KB
/
mivvars.ado
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
*! version 1.0.0 13Jul2020
//-----------------------------------------------------------------------------
//
// mivcausal - Testing the hypothesis about the signs of the 2SLS weights
// Extract the list of variables from `anything'
//
//-----------------------------------------------------------------------------
capt program drop mivvars
program mivvars, sclass
version 10.0
syntax anything [if] [in] [, VCE(passthru) ///
Robust ///
CLuster(passthru)]
// Step 1 - Extract the variable names
gettoken vars xvars : anything, parse(" ,[") match(paren)
// Step 2 - Check if there is anything before the parentheses
* Count the number of words in vars
local vn : word count `vars'
* Message for incorrect syntax
local varerrmsg "Error: The list of variables is specified " ///
"incorrectly. The list of variables should be " ///
"'(D = Z1 Z2) X' where D is the endogenous variable, " ///
"Z1 and Z2 are the instrumental variables and X " ///
"is the list of covariates."
* Return error if users put variables before the list of parentheses or
* if more than one set of parentheses is included
if "`paren'" != "(" | strpos("`xvars'", "(") != 0 {
di as error "`varerrmsg'"
exit 498
}
// Step 3 - Check the syntax of the terms inside the parenthesis
* Return error if user does not put an equal sign inside the cluster of
* variables inside the parentheses
if strpos("`vars'", "=") == 0 {
di as error "`varerrmsg'"
exit 498
}
* Check the syntax
scalar zstart = 0
while `vn' != 0 {
gettoken fvar vars : vars, parse(" =") match(paren)
* Return error if the variables "vars" include any parentheses
if "`paren'" == "(" {
di as error "`varerrmsg'"
exit 498
}
if "`fvar'" != "=" & zstart == 0 {
local dvar `dvar' `fvar'
}
else if "`fvar'" == "=" {
scalar zstart = 1
}
else {
* Return error if the user puts more than one equal sign inside
* the cluster of variables inside the parentheses
if strpos("`vars'", "=") != 0 {
di as error "`varerrmsg'"
exit 498
}
local zvars `zvars' `fvar'
}
* Count the remaining number of words
local vn : word count `vars'
}
// Step 4 - Retokenize the variables
local dvar : list retokenize dvar
local zvars : list retokenize zvars
local xvars : list retokenize xvars
// Step 5 - Form the list of the variables explicitly if they include "*"
* Treatment
if strpos("`dvar'", "*") != 0 {
local dvar_final `'
foreach d of varlist `dvar' {
local dvar_final `dvar_final' `d'
}
}
else {
local dvar_final `dvar'
}
* Instruments
if strpos("`zvars'", "*") != 0 {
local zvars_final `'
foreach z of varlist `zvars' {
local zvars_final `zvars_final' `z'
}
}
else {
local zvars_final `zvars'
}
// Step 6 - Count the number of variables dvar and zvars
* Count the number of treatments
local dn : word count `dvar_final'
local zn : word count `zvars_final'
* Return error if the number of variables is incorrect
if `dn' != 1 {
di as error "Error: There has to be one endogenous variable."
exit 498
}
* Count the number of instruments
if `zn' != 2 {
di as error "Error: There have to be two binary instrumental variables."
exit 498
}
// Step 7 - Check if the variables exist in the dataset
foreach x of varlist `dvar_final' `zvars_final' `xvars' {
capture confirm variable `x', exact
if _rc != 0 {
di as error "Error: The variable " `x' " does not exist in " ///
"the dataset."
exit 498
}
}
// Step 8 - Check variance
* Parse the standard errors
_vce_parse, optlist(Robust) argoptlist(CLuster) old : , `vce' ///
`robust' ///
`cluster'
// Step 9 - Assign the return list
* Assign the return list of the variance
if missing(r(vce)) {
sreturn local vce "unadjusted"
}
else {
sreturn local robust `r(robust)'
sreturn local cluster `r(cluster)'
sreturn local vceopt `r(vceopt)'
sreturn local vceargs `r(vceargs)'
sreturn local vce `r(vce)'
}
* Assign the return list of the variables
sreturn local dvar `dvar_final'
sreturn local zvars `zvars_final'
sreturn local xvars `xvars'
end