-
Notifications
You must be signed in to change notification settings - Fork 17
/
edadistro.ado
executable file
·200 lines (142 loc) · 7.02 KB
/
edadistro.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
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
********************************************************************************
* Description of the Program - *
* EDA subroutine used to create distribution assumption plots *
* *
* Program Output - *
* Creates series of distribution quality/assumption graph GPHs and PDFs *
* as well as entries in the LaTeX document *
* *
* Lines - *
* 199 *
* *
********************************************************************************
*! edadistro
*! v 0.0.1
*! 01may2019
// Drop program from memory if already loaded
cap prog drop edadistro
// Define program
prog def edadistro
// Version used to interpret code
version 14
// Syntax structure for edabar subroutine
syntax varlist(min=1) [if] [in], root(string asis) ///
[DISTROPlotopts(string asis) ///
scheme(passthru) ///
keepgph noUNIvariate]
// Mark only the observations to use
marksample touse, strok novarlist
// Check implementation for univariate vs bivariate graphs
if "`univariate'" != "nounivariate" {
loc symplotlab "Symmetry Plot for "
loc quantilelab "Quantiles of Uniform Distribution vs Ordered values of "
loc qnormlab "Normal Distribution vs Quantiles of "
loc pnormlab "Standardized Normal Probability Plot for "
// Add subheading to the LaTeX file
file write doc "\subsubsection{Distribution Diagnostic Plots}" _n
// Loop over continuous variables
foreach v of var `varlist' {
// Get LaTeX sanitized version of the variable name
texclean "`v'", r
// Store the variable name in vref
loc vref `r(clntex)'
// Get LaTeX sanitized version of the variable label
texclean `"`: var l `v''"'
// Store the label in vlab
loc vlab `r(clntex)'
// Loop over the simpler plot types
foreach plottype in symplot quantile qnorm pnorm {
// Create symmetry plot
`plottype' `v' if `touse', `scheme' ti("``plottype'lab'`v'") ///
note("Created on: `c(current_date)' at: `c(current_time)'")
// Export graph to pdf
qui: gr export `"`root'/graphs/`plottype'`v'.pdf"', as(pdf) replace
// Check option to keep the GPH file on disk
if "`keepgph'" != "" {
// Delete Stata GPH file from disk
qui: gr save `"`root'/graphs/`plottype'`v'.gph"', replace
} // End IF Block for null keep graph option
// Add the graph to the LaTeX file
file write doc "\begin{figure}[h!]" _n
file write doc `"\caption{``plottype'lab'`vlab' \label{fig:`plottype'`vref'}}"' _n
file write doc `"\includegraphics[width=\textwidth]{`plottype'`v'.pdf}"' _n
file write doc "\end{figure} \newpage\clearpage" _n
} // End Loop over simple plot types
// Create quantiles of variable vs chi-squared distro
qchi `v' if `touse', `scheme' `: word 1 of `distroplotopts'' ///
ti("Quantiles of `v' vs. " "Quantiles of {&Chi}{superscript:2}") ///
note("Created on: `c(current_date)' at: `c(current_time)'")
// Export the graph to pdf
qui: gr export `"`root'/graphs/qchi`v'.pdf"', as(pdf) replace
// Create X^2 Probability plots
pchi `v' if `touse', `scheme' `: word 2 of `distroplotopts'' ///
ti("{&Chi}{superscript:2} Probability Plots for `v'") ///
note("Created on: `c(current_date)' at: `c(current_time)'")
// Export to pdf
qui: gr export `"`root'/graphs/pchi`v'.pdf"', as(pdf) replace
// Add the graph to the LaTeX file
file write doc "\begin{figure}[h!]" _n
file write doc `"\caption{Chi-Squared Quantiles v. `vlab' \label{fig:qchi`vref'}}"' _n
file write doc `"\includegraphics[width=\textwidth]{qchi`v'.pdf}"' _n
file write doc "\end{figure} \newpage\clearpage" _n
// Add the graph to the LaTeX file
file write doc "\begin{figure}[h!]" _n
file write doc `"\caption{Chi-Squared Probability `vlab' \label{fig:pchi`vref'}}"' _n
file write doc `"\includegraphics[width=\textwidth]{pchi`v'.pdf}"' _n
file write doc "\end{figure} \newpage\clearpage" _n
// Check option to keep the GPH file on disk
if "`keepgph'" != "" {
// Delete Stata GPH file from disk
qui: gr save `"`root'/graphs/qchi`v'.gph"', replace
// Delete Stata GPH file from disk
qui: gr save `"`root'/graphs/pchi`v'.gph"', replace
} // End IF Block for null keep graph option
} // End Loop over continuous variables
} // End IF Block for univariate case
// If univariate does = nounivariate
else {
// Add section header to LaTeX file
file write doc "\subsubsection{Quantile Quantile Plots}" _n
// Generate list of all pairwise combination of continuous variables
tuples `varlist', asis min(2) max(2) cvp
// Generate scatter plots for all pairwise combinations of continuous variables
forv i = 1/`ntuples' {
// Use the first element of the tuple for the x-axis
loc x : word 1 of `tuple`i''
// Use the second element of the tuple for the y-axis
loc y : word 2 of `tuple`i''
// Create qqplot
qqplot `y' `x' if `touse', `scheme' ///
note("Created on: `c(current_date)' at: `c(current_time)'")
// Export to pdf
qui: gr export `"`root'/graphs/qqplot-`y'-`x'.pdf"', as(pdf) replace
// Get LaTeX sanitized x variable name
texclean `"`x'"', r
// Store cleaned x variable name in clnx
loc clnx `r(clntex)'
// Get LaTeX sanitized y variable name
texclean `"`y'"', r
// Store cleaned y variable name in clny
loc clny `r(clntex)'
// Get clean version of y var label
texclean `"`: var l `y''"'
// Store the label in the local macro ylab
loc ylab `r(clntex)'
// Get clean version of x var label
texclean `"`: var l `x''"'
// Store the label in the local macro xlab
loc xlab `r(clntex)'
// Check if user wants to keep the GPH files
if "`keepgph'" != "" {
// Define local macro with syntax to remove file
qui: gr save `"`root'/graphs/qqplot-`y'-`x'.gph"', replace
} // End IF Block to remove .gph files
// Add the graph to the LaTeX file
file write doc "\begin{figure}[h!]" _n
file write doc `"\caption{Quantiles of `ylab' vs Quantiles of `xlab' \label{fig:qq`clny'`clnx'}}"' _n
file write doc `"\includegraphics[width=\textwidth]{qqplot-`y'-`x'.pdf}"' _n
file write doc "\end{figure} \newpage\clearpage" _n
} // End Loop over continuous variable permutations
} // End ELSE Block for bivariate distribution graphs
// End of program definition
end