-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimulation.do
313 lines (274 loc) · 7.51 KB
/
simulation.do
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
clear all
*------- PROGRAMS USED IN SECTION 4 -------*
*--- mcsim ---*
capture program drop mcsim
program define mcsim, rclass
syntax [anything] [, xn(string) ///
beta(string) intercept(string) p(string) NOBS(integer 100)]
clear
set obs `nobs'
foreach g of numlist 1/`xn' {
gen x`g' = runiform() < `p'
}
local model ""
foreach i of numlist 1/`xn' {
local model "`model' + `beta' * x`i'"
}
gen logitp = `intercept' `model'
gen py = invlogit(logitp)
gen y = runiform() < py
tab y
drop py logitp
contract x* y, f(fw)
capture noi logit y x* [fw = fw], or noheader nolog asis
mat coef = e(b)
return scalar beta1 = coef[1,1]
return scalar beta2 = coef[1,2]
return scalar beta3 = coef[1,3]
return scalar beta4 = coef[1,4]
return scalar beta5 = coef[1,5]
return scalar beta6 = coef[1,6]
return scalar beta7 = coef[1,7]
return scalar beta8 = coef[1,8]
return scalar beta9 = coef[1,9]
return scalar beta10 = coef[1,10]
return scalar converged = e(converged)
penlogit y x* [fw = fw], nprior(x1 0 4 x2 0 4 x3 0 4 ///
x4 0 4 x5 0 4 x6 0 4 ///
x7 0 4 x8 0 4 x9 0 4 x10 0 4) or nolist
mat coefp = e(b)
return scalar betapn1 = coefp[1,1]
return scalar betapn2 = coefp[1,2]
return scalar betapn3 = coefp[1,3]
return scalar betapn4 = coefp[1,4]
return scalar betapn5 = coefp[1,5]
return scalar betapn6 = coefp[1,6]
return scalar betapn7 = coefp[1,7]
return scalar betapn8 = coefp[1,8]
return scalar betapn9 = coefp[1,9]
return scalar betapn10 = coefp[1,10]
return scalar convergedpn = e(converged)
penlogit y x* [fw = fw], nprior(x1 ln(2) 1 x2 ln(2) 1 x3 ln(2) 1 ///
x4 ln(2) 1 x5 ln(2) 1 x6 ln(2) 1 ///
x7 ln(2) 1 x8 ln(2) 1 x9 ln(2) 1 x10 ln(2) 1) or nolist
mat coefp = e(b)
return scalar betapi1 = coefp[1,1]
return scalar betapi2 = coefp[1,2]
return scalar betapi3 = coefp[1,3]
return scalar betapi4 = coefp[1,4]
return scalar betapi5 = coefp[1,5]
return scalar betapi6 = coefp[1,6]
return scalar betapi7 = coefp[1,7]
return scalar betapi8 = coefp[1,8]
return scalar betapi9 = coefp[1,9]
return scalar betapi10 = coefp[1,10]
return scalar convergedpi = e(converged)
end
*------*
*--- report ---*
capture program drop report
program define report
foreach v of varlist beta* {
gen or`v' = exp(`v')
}
tab1 converged convergedpn convergedpi
tabstat orbeta1-orbeta10 if converged , s(p50 p5 p95 n) format(%4.1f)
tabstat orbetapn1-orbetapn10 if convergedpn, s(p50 p5 p95 n) format(%4.1f)
tabstat orbetapi1-orbetapi10 if convergedpi, s(p50 p5 p95 n) format(%4.1f)
end
*------*
*--- intercept_bisect ---*
capture program drop intercept_bisect
program intercept_bisect, rclass
args or p
local x1 = -25
local x2 = +10
while abs(`x1' - `x2') > 1e-3 {
local t = (`x1' + `x2') / 2
local a = binomialp(10,0,.5)*invlogit(`t') + ///
binomialp(10,1,.5)*invlogit((`t'+1*log(`or'))) + ///
binomialp(10,2,.5)*invlogit((`t'+2*log(`or'))) + ///
binomialp(10,3,.5)*invlogit((`t'+3*log(`or'))) + ///
binomialp(10,4,.5)*invlogit((`t'+4*log(`or'))) + ///
binomialp(10,5,.5)*invlogit((`t'+5*log(`or'))) + ///
binomialp(10,6,.5)*invlogit((`t'+6*log(`or'))) + ///
binomialp(10,7,.5)*invlogit((`t'+7*log(`or'))) + ///
binomialp(10,8,.5)*invlogit((`t'+8*log(`or'))) + ///
binomialp(10,9,.5)*invlogit((`t'+9*log(`or'))) + ///
binomialp(10,10,.5)*invlogit((`t'+10*log(`or')))
local diff = `p' - `a'
if `diff' > 0 local x1 = `t'
else local x2 = `t'
}
return scalar intercept = round(`t',.01)
end
*------*
exit
*------- SIMULATIONS -------*
cls
log using simulation, smcl replace name(sim)
*n=500, beta=log(4)
intercept_bisect 4 .05
local intercept = r(intercept)
simulate ///
beta1 = r(beta1) ///
beta2 = r(beta2) ///
beta3 = r(beta3) ///
beta4 = r(beta4) ///
beta5 = r(beta5) ///
beta6 = r(beta6) ///
beta7 = r(beta7) ///
beta8 = r(beta8) ///
beta9 = r(beta9) ///
beta10 = r(beta10) ///
converged = r(converged) ///
betapn1 = r(betapn1) ///
betapn2 = r(betapn2) ///
betapn3 = r(betapn3) ///
betapn4 = r(betapn4) ///
betapn5 = r(betapn5) ///
betapn6 = r(betapn6) ///
betapn7 = r(betapn7) ///
betapn8 = r(betapn8) ///
betapn9 = r(betapn9) ///
betapn10 = r(betapn10) ///
convergedpn = r(convergedpn) ///
betapi1 = r(betapi1) ///
betapi2 = r(betapi2) ///
betapi3 = r(betapi3) ///
betapi4 = r(betapi4) ///
betapi5 = r(betapi5) ///
betapi6 = r(betapi6) ///
betapi7 = r(betapi7) ///
betapi8 = r(betapi8) ///
betapi9 = r(betapi9) ///
betapi10 = r(betapi10) ///
convergedpi = r(convergedpi) ///
, ///
reps(1000) seed(4321) saving(s1, replace) nodots: mcsim , xn(10) beta(log(4)) p(.5) ///
intercept(`intercept') nobs(500)
report
*n=500, beta=log(10)
intercept_bisect 10 .05
local intercept = r(intercept)
simulate ///
beta1 = r(beta1) ///
beta2 = r(beta2) ///
beta3 = r(beta3) ///
beta4 = r(beta4) ///
beta5 = r(beta5) ///
beta6 = r(beta6) ///
beta7 = r(beta7) ///
beta8 = r(beta8) ///
beta9 = r(beta9) ///
beta10 = r(beta10) ///
converged = r(converged) ///
betapn1 = r(betapn1) ///
betapn2 = r(betapn2) ///
betapn3 = r(betapn3) ///
betapn4 = r(betapn4) ///
betapn5 = r(betapn5) ///
betapn6 = r(betapn6) ///
betapn7 = r(betapn7) ///
betapn8 = r(betapn8) ///
betapn9 = r(betapn9) ///
betapn10 = r(betapn10) ///
convergedpn = r(convergedpn) ///
betapi1 = r(betapi1) ///
betapi2 = r(betapi2) ///
betapi3 = r(betapi3) ///
betapi4 = r(betapi4) ///
betapi5 = r(betapi5) ///
betapi6 = r(betapi6) ///
betapi7 = r(betapi7) ///
betapi8 = r(betapi8) ///
betapi9 = r(betapi9) ///
betapi10 = r(betapi10) ///
convergedpi = r(convergedpi) ///
, ///
reps(1000) seed(4321) saving(s2, replace) nodots: mcsim , xn(10) beta(log(10)) p(.5) ///
intercept(`intercept') nobs(500)
report
*n=5000, beta=log(4)
intercept_bisect 4 .005
local intercept = r(intercept)
simulate ///
beta1 = r(beta1) ///
beta2 = r(beta2) ///
beta3 = r(beta3) ///
beta4 = r(beta4) ///
beta5 = r(beta5) ///
beta6 = r(beta6) ///
beta7 = r(beta7) ///
beta8 = r(beta8) ///
beta9 = r(beta9) ///
beta10 = r(beta10) ///
converged = r(converged) ///
betapn1 = r(betapn1) ///
betapn2 = r(betapn2) ///
betapn3 = r(betapn3) ///
betapn4 = r(betapn4) ///
betapn5 = r(betapn5) ///
betapn6 = r(betapn6) ///
betapn7 = r(betapn7) ///
betapn8 = r(betapn8) ///
betapn9 = r(betapn9) ///
betapn10 = r(betapn10) ///
convergedpn = r(convergedpn) ///
betapi1 = r(betapi1) ///
betapi2 = r(betapi2) ///
betapi3 = r(betapi3) ///
betapi4 = r(betapi4) ///
betapi5 = r(betapi5) ///
betapi6 = r(betapi6) ///
betapi7 = r(betapi7) ///
betapi8 = r(betapi8) ///
betapi9 = r(betapi9) ///
betapi10 = r(betapi10) ///
convergedpi = r(convergedpi) ///
, ///
reps(1000) seed(4321) saving(s3, replace) nodots: mcsim , xn(10) beta(log(4)) p(.5) ///
intercept(`intercept') nobs(5000)
report
*n=5000, beta=log(10)
intercept_bisect 10 .005
local intercept = r(intercept)
simulate ///
beta1 = r(beta1) ///
beta2 = r(beta2) ///
beta3 = r(beta3) ///
beta4 = r(beta4) ///
beta5 = r(beta5) ///
beta6 = r(beta6) ///
beta7 = r(beta7) ///
beta8 = r(beta8) ///
beta9 = r(beta9) ///
beta10 = r(beta10) ///
converged = r(converged) ///
betapn1 = r(betapn1) ///
betapn2 = r(betapn2) ///
betapn3 = r(betapn3) ///
betapn4 = r(betapn4) ///
betapn5 = r(betapn5) ///
betapn6 = r(betapn6) ///
betapn7 = r(betapn7) ///
betapn8 = r(betapn8) ///
betapn9 = r(betapn9) ///
betapn10 = r(betapn10) ///
convergedpn = r(convergedpn) ///
betapi1 = r(betapi1) ///
betapi2 = r(betapi2) ///
betapi3 = r(betapi3) ///
betapi4 = r(betapi4) ///
betapi5 = r(betapi5) ///
betapi6 = r(betapi6) ///
betapi7 = r(betapi7) ///
betapi8 = r(betapi8) ///
betapi9 = r(betapi9) ///
betapi10 = r(betapi10) ///
convergedpi = r(convergedpi) ///
, ///
reps(1000) seed(4321) saving(s4, replace) nodots: mcsim , xn(10) beta(log(10)) p(.5) ///
intercept(`intercept') nobs(5000)
report
log close sim