-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorespecs.ado
140 lines (126 loc) · 4.07 KB
/
storespecs.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
version 13.1 /* That's right... I'm still on Stata 13... */
cap program drop storespecs
program storespecs
syntax varlist(min=1 numeric), [SPEC_name(string) BInary DEDUPlicate] file(string)
///////////////////////////////////////////////////////////////////////////////
// Description:
// ------------
// Stores coefficients and other post-regression meta data (standard errors,
// confidence limits, t-statistics, p-values, and specification data.
//
// Syntax:
// -------
// storespecs varlist, spec_name(groups) file(filename)
// MWE:
// ----
// sysuse auto.dta
// reg price weight length
// storespecs weight, spec_name(length) file(_file)
// reg price weight length mpg
// storespecs weight, spec_name(length mpg) file(_file)
// use _file, clear
//
// BS example:
// -----------
// sysuse auto.dta
// forval i = 1/5 {
// bsample
// reg price weight length
// storespecs weight, file(_file)
// }
//
// Remarks:
// --------
// . If file does not exist, a new one will be created. BUT if file already
// exists, then it will be appended even if new addition is a duplicate
// of existing data. To deduplicate, use the 'deduplicate' option.
//
// . Deduplicate option ignores spec_id, and will re-generate an arbitrary
// running ID after dropping duplicates by specification groups:
// storespecs weight, spec_name(length mpg) dedup file(_file)
//////////////////////////////////////////////////////////////////////////////
local n_varlist: word count `varlist'
if `n_varlist'==1 {
local var `varlist'
local multivar 0
}
else if `n_varlist'>1 {
local multivar 1
}
if "`binary'"!="" { /* if yes, add 1. prefix to correctly retrieve results */
local estprefix = "1."
}
else {
local estprefix ""
}
tempfile temp
save "`temp'", replace
cap use "`file'.dta", clear
if _rc!=0 {
clear
gen int spec_id=.
if `n_varlist'==1 {
gen double beta=.
gen double se=.
gen double u95=.
gen double l95=.
gen double u90=.
gen double l90=.
gen double tstat=.
gen double pval=.
}
else if `n_varlist'>1 {
foreach var in `varlist' {
gen double beta_`var'=.
gen double se_`var'=.
gen double u95_`var'=.
gen double l95_`var'=.
gen double u90_`var'=.
gen double l90_`var'=.
gen double tstat_`var'=.
gen double pval_`var'=.
}
}
}
local n = _N+1
set obs `n'
replace spec_id = `n' if _n==`n'
if `n_varlist' == 1 { /* `var' macro already defined above */
* retreive estimates
replace beta=_b[`estprefix'`var'] if spec_id==`n'
replace se=_se[`estprefix'`var'] if spec_id==`n'
replace tstat=beta/se if spec_id==`n'
replace pval=2*ttail(e(df_r), abs(tstat))
* compute bounds
replace u95=beta+invt(e(df_r),0.975)*se if spec_id==`n'
replace l95=beta-invt(e(df_r),0.975)*se if spec_id==`n'
replace u90=beta+invt(e(df_r),0.95)*se if spec_id==`n'
replace l90=beta-invt(e(df_r),0.95)*se if spec_id==`n'
}
else if `n_varlist'> 1 { /* if more than 1 var, use var name as suffix */
foreach var in `varlist' {
* retreive estimates
replace beta_`var'=_b[`estprefix'`var'] if spec_id==`n'
replace se_`var'=_se[`estprefix'`var'] if spec_id==`n'
replace tstat_`var'=beta_`var'/se_`var' if spec_id==`n'
replace pval_`var'=2*ttail(e(df_r), abs(tstat_`var'))
* compute bounds
replace u95_`var'=beta_`var'+invt(e(df_r),0.975)*se_`var' if spec_id==`n'
replace l95_`var'=beta_`var'-invt(e(df_r),0.975)*se_`var' if spec_id==`n'
replace u90_`var'=beta_`var'+invt(e(df_r),0.95)*se_`var' if spec_id==`n'
replace l90_`var'=beta_`var'-invt(e(df_r),0.95)*se_`var' if spec_id==`n'
}
}
foreach group in `spec_name' {
cap gen byte `group' = 1 if spec_id==`n'
if _rc!=0 { /* error will only happen after the first time `group' is generated, every other time is error */
replace `group' = 1 if spec_id==`n'
}
}
if "`deduplicate'"!="" { /* remove duplicates. `spec_name' macro is actually a list of var to check duplicates for */
duplicates drop `spec_name', force
replace spec_id = _n
}
save "`file'.dta", replace
use `temp', clear
end