-
Notifications
You must be signed in to change notification settings - Fork 1
/
bpbounds.ado
143 lines (123 loc) · 3.07 KB
/
bpbounds.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
*! 1.0.2 Tom Palmer 4aug2011
program bpbounds, rclass
version 9.0
_iv_parse `0' // parses `0' of form: lhs exog (endog = inst)
local lhs `s(lhs)'
local endog `s(endog)'
local exog `s(exog)'
local inst `s(inst)'
local 0 `s(zero)'
if "`exog'" != "" {
di as err "No exogenous variables allowed"
error 103
}
if wordcount("`endog'") != 1 {
di as err "Only one endogenous variable allowed"
error 103
}
if wordcount("`inst'") != 1 {
di as err "Only one instrumental variable allowed"
error 103
}
syntax [if] [in] [fweight/] [, fmt(string)]
if "`fmt'" == "" {
local fmt %5.4f
}
* if, in, expand fweights
if "`if'`in'`exp'" != "" {
preserve
if "`if'`in'" != "" {
qui keep `if' `in'
}
if "`exp'" != "" {
qui expand `exp'
}
}
bpbounds_trivariate `endog' `lhs' `inst', fmt(`fmt')
if "`if'`in'`exp'" != "" {
restore
}
ret add
end
program bpbounds_trivariate, rclass
syntax varlist(min=3 max=3 numeric)[, fmt(string)]
tokenize `varlist'
local x `1'
local y `2'
local z `3'
* check x y are 0,1 and z is 0,1 or 0,1,2
qui foreach var of varlist `x' `y' {
levelsof `var', local(levels)
if "`levels'" != "0 1" {
di as err "`var' not coded 0,1"
error 197
}
}
qui levelsof `z', local(levels)
if "`levels'" != "0 1" {
if "`levels'" != "0 1 2" {
di as err "`z' not coded as 0,1 or 0,1,2"
error 197
}
}
tempname nzcat
sca `nzcat' = wordcount("`levels'")
if `nzcat' == 2 {
* notation: P(y=0,x=0|z=0)=p000
tempname n000 n100 n010 n110 n001 n101 n011 n111
qui count if `z' == 0 & `x' == 0 & `y' == 0
sca `n000' = r(N)
qui count if `z' == 0 & `x' == 0 & `y' == 1
sca `n100' = r(N)
qui count if `z' == 0 & `x' == 1 & `y' == 0
sca `n010' = r(N)
qui count if `z' == 0 & `x' == 1 & `y' == 1
sca `n110' = r(N)
qui count if `z' == 1 & `x' == 0 & `y' == 0
sca `n001' = r(N)
qui count if `z' == 1 & `x' == 0 & `y' == 1
sca `n101' = r(N)
qui count if `z' == 1 & `x' == 1 & `y' == 0
sca `n011' = r(N)
qui count if `z' == 1 & `x' == 1 & `y' == 1
sca `n111' = r(N)
bpboundsi `n000' `n100' `n010' `n110' `n001' `n101' `n011' `n111', ///
fmt(`fmt')
ret add
}
else if `nzcat' == 3 {
* notation: P(y=0,x=0|z=0)=p000
tempname n000 n100 n010 n110 ///
n001 n101 n011 n111 ///
n002 n102 n012 n112
qui count if `z' == 0 & `x' == 0 & `y' == 0
sca `n000' = r(N)
qui count if `z' == 0 & `x' == 0 & `y' == 1
sca `n100' = r(N)
qui count if `z' == 0 & `x' == 1 & `y' == 0
sca `n010' = r(N)
qui count if `z' == 0 & `x' == 1 & `y' == 1
sca `n110' = r(N)
qui count if `z' == 1 & `x' == 0 & `y' == 0
sca `n001' = r(N)
qui count if `z' == 1 & `x' == 0 & `y' == 1
sca `n101' = r(N)
qui count if `z' == 1 & `x' == 1 & `y' == 0
sca `n011' = r(N)
qui count if `z' == 1 & `x' == 1 & `y' == 1
sca `n111' = r(N)
qui count if `z' == 2 & `x' == 0 & `y' == 0
sca `n002' = r(N)
qui count if `z' == 2 & `x' == 0 & `y' == 1
sca `n102' = r(N)
qui count if `z' == 2 & `x' == 1 & `y' == 0
sca `n012' = r(N)
qui count if `z' == 2 & `x' == 1 & `y' == 1
sca `n112' = r(N)
bpboundsi `n000' `n100' `n010' `n110' ///
`n001' `n101' `n011' `n111' ///
`n002' `n102' `n012' `n112', ///
fmt(`fmt')
ret add
}
end