forked from remlapmot/mrrobust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mrmodalplot.ado
107 lines (98 loc) · 2.95 KB
/
mrmodalplot.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
*! 0.1.0 2aug2017 Tom Palmer
program mrmodalplot
version 9
syntax varlist(min=4 max=4) [if] [in] [, ///
WEIGHTed NOME PHI(numlist) seed(string) reps(integer 1000) ///
lp(string) lc(string) lw(string) *]
if "`phi'" == "" {
local phi .25 .5 1
}
local nphi : word count `phi'
if "`seed'" != "" {
local seedopt seed(`seed')
}
else {
local seedopt
}
if "`lp'" != "" {
local nlp : word count `lp'
if `nlp' != `nphi' {
di as err "lp() must have same number of elements as phi()"
exit 198
}
if `nphi' > 1 {
forvalues i=1/`nphi' {
local lpcmd`i' lp(`: word `i' of `lp'')
}
}
else {
local lpcmd1 lp(`lp')
}
}
if "`lc'" != "" {
local nlc : word count `lc'
if `nlc' != `nphi' {
di as err "lc() must have same number of elements as phi()"
exit 198
}
if `nphi' > 1 {
forvalues i=1/`nphi' {
local lccmd`i' lc(`: word `i' of `lc'')
}
}
else {
local lccmd1 lc(`lc')
}
}
if "`lw'" != "" {
local nlw : word count `lw'
if `nlw' != `nphi' {
di as err "lw() must have same number of elements as phi()"
exit 198
}
if `nphi' > 1 {
forvalues i=1/`nphi' {
local lwcmd`i' lw(`: word `i' of `lw'')
}
}
else {
local lwcmd1 lw(`lw')
}
}
** fit mrmodal for each value of phi
local i = 1
foreach p of numlist `phi' {
mrmodal `varlist' `if'`in', ///
`weighted' `nome' phi(`p') `seedopt' reps(`reps')
mata mrmodal_densityiv_`i' = mrmodal_densityiv
mata mrmodal_g_`i' = mrmodal_g
local i = `i' + 1
}
** plot of densities
preserve
drop _all
qui set obs 512
forvalues i=1/`nphi' {
getmata (density`i')=mrmodal_densityiv_`i', double
getmata (g`i')=mrmodal_g_`i', double
if `i' == 1 {
local legordercmd `"1 "{&phi} = `: word 1 of `phi''""'
twoway line density`i' g`i', `lpcmd`i'' ///
`lccmd`i'' ///
`lwcmd`i'' ///
xtitle(IV estimates) ///
ytitle(Density) ///
legend(on order(`legordercmd') rows(1)) `options'
}
else {
local legordercmd `"`legordercmd' `i' "{&phi} = `: word `i' of `phi''""'
addplot : line density`i' g`i', ///
`lpcmd`i'' ///
`lccmd`i'' ///
`lwcmd`i'' ///
legend(on order(`legordercmd') rows(1)) `options'
}
}
restore
end
exit