-
Notifications
You must be signed in to change notification settings - Fork 17
/
edascat.ado
executable file
·288 lines (192 loc) · 8.75 KB
/
edascat.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
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
********************************************************************************
* Description of the Program - *
* EDA subroutine used to create scatter plots *
* *
* Program Output - *
* Creates series of scatterplots GPHs and PDFs as well as entries in the *
* LaTeX document *
* *
* Lines - *
* 287 *
* *
********************************************************************************
*! edascat
*! v 0.0.1
*! 01may2019
// Drop program from memory if already loaded
cap prog drop edascat
// Define program
prog def edascat
// Version used to interpret code
version 14
// Syntax structure for edabar subroutine
syntax varlist(min=1) [if] [in], root(string asis) ///
[ LFIT LFIT2(string asis) ///
QFIT QFIT2(string asis) ///
LOWess LOWess2(string asis) ///
FPFIT FPFIT2(string asis) ///
LFITCi LFITCi2(string asis) ///
QFITCi QFITCi2(string asis) ///
FPFITCi FPFITCi2(string asis) ///
scheme(passthru) keepgph ///
byvars(varlist) byseq ]
// Mark only the observations to use
marksample touse, strok novarlist
// If byvars weren't passed
if `"`byvars'"' == "" {
// Add section header to LaTeX file
file write doc "\subsubsection{Scatterplots}" _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''
// Check for linear fit
if "`lfit'" != "" {
// Check for lfit overlay options
if "`lfit2'" != "" {
// Add comma before options
loc lfit2 , `lfit2'
} // End If Block for linear fit options
// Local with lfit option syntax
loc grlfit lfit `y' `x' if `touse'`lfit2' ||
// Legend label
loc lfitlab "Linear"
} // End of IF Block for linear fit overlay
// Check for lowess smoothed fit
if "`lowess'" != "" {
// Check for lowess overlay options
if "`lowess2'" != "" {
// Add comma before options
loc lowess2 , `lowess2'
} // End If Block for linear fit options
// Local with lowess option syntax
loc grlowess lowess `y' `x' if `touse'`lowess2' ||
// Legend label
loc lowesslab "Loess Smoother"
} // End of IF Block for lowess smoothed fit
// Check for quadratic fit
if "`qfit'" != "" {
// Check for qfit overlay options
if "`qfit2'" != "" {
// Add comma before options
loc qfit2 , `qfit2'
} // End If Block for linear fit options
// Local with qfit option syntax
loc grqfit qfit `y' `x' if `touse'`qfit2' ||
// Legend label
loc qfitlab "Quadratic"
} // End of IF Block for quadratic fit overlay
// Check for fractional polynomial fit
if "`fpfit'" != "" {
// Check for fpfit overlay options
if "`fpfit2'" != "" {
// Add comma before options
loc fpfit2 , `fpfit2'
} // End If Block for linear fit options
// Local with fpfit option syntax
loc grfpfit fpfit `y' `x' if `touse'`fpfit2' ||
// Legend label
loc fpfitlab "Fractional Polynomial"
} // End of IF Block for fractional polynomial fit
// Check for linear fit with confidence intervals
if "`lfitci'" != "" {
// Check for lfitci overlay options
if "`lfitci2'" != "" {
// Add comma before options
loc lfitci2 , `lfitci2'
} // End If Block for linear fit options
// Local with lfitci option syntax
loc grlfitci lfitci `y' `x' if `touse'`lfitci2' ||
// Label for linear ci overlay
loc lfitcilab "Linear Fit w/CIs"
} // End of IF Block for linear fit with confidence intervals overlay
// Check for quadratic fit with confidence intervals
if "`qfitci'" != "" {
// Check for qfitci overlay options
if "`qfitci2'" != "" {
// Add comma before options
loc qfitci2 , `qfitci2'
} // End If Block for linear fit options
// Local with qfitci option syntax
loc grqfitci qfitci `y' `x' if `touse'`qfitci2' ||
// Label for linear ci overlay
loc lfitcilab "Quadratic Fit w/CIs"
} // End of IF Block for quadratic fit with confidence intervals overlay
// Check for fractional polynomial fit with confidence intervals
if "`fpfitci'" != "" {
// Check for qfitci overlay options
if "`fpfitci2'" != "" {
// Add comma before options
loc fpfitci2 , `fpfitci2'
} // End If Block for linear fit options
// Local with qfitci option syntax
loc grfpfitci fpfitci `y' `x' if `touse' `fpfitci2' ||
// Label for linear ci overlay
loc fpfitcilab "Fractional Polynomial w/CIs"
} // End of IF Block for fractional polynomial fit with confidence intervals
// Local macro to use for indexing legending graphs
loc scattercounter = 1
// Loop over the overlay options
foreach ov in lfit lowess qfit fpfit lfitci qfitci fpfitci {
// End IF Block to check for legend labels
if `"``ov'lab'"' != "" {
// Increment the counter id
loc scattercounter = `scattercounter' + 1
// Append the labels for the legend
loc legendlabels `legendlabels' label(`scattercounter' ``ov'lab')
} // End IF Block
} // End Loop over overlay options
// Add scatter points as the last element of the legend labels
loc legendlabels label(`= `scattercounter' + 1' "Scatter Points")
// Append the labels for the legend
loc legendlabels `legendlabels' label(`scattercounter' ``ov'lab')
// If there are 4 or less total legend entries use a single row
if `scattercounter' <= 4 {
// Syntax to enforce single row of legend keys
loc leg legend(rows(1) symy(1.85) symx(1.85) `legendlabels')
} // End IF Block for <= 4 graph elements
// If there are more elements to place in the legend
else {
// Reduce the size of the labels
loc leg legend(rows(2) symy(1.85) symx(1.85) `legendlabels' size(small))
} // End ELSE Block for many legend entries
// Generate the scatterplot
tw `grlfitci' `grqfitci' `grfpfitci' `grlfit' `grlowess' `grqfit' ///
`grfpfit' scatter `y' `x' if `touse', `scheme' `leg' ///
xti(`: char `x'[title]') yti(`: char `y'[title]') ///
ti("Joint Distribution of `y' and `x'") ///
note("Created on: `c(current_date)' at: `c(current_time)'")
// Export the scatterplot as a .png file
qui: gr export `"`root'/graphs/scatter-`y'-`x'.pdf"', as(pdf) replace
// Check if user wants to keep the GPH files
if "`keepgph'" != "" {
// Define local macro with syntax to remove file
qui: gr save `"`root'/graphs/scatter-`y'-`x'.gph"', replace
} // End IF Block to remove .gph files
// Get LaTeX cleaned y variable label
texclean `"`: var l `y''"'
// Store the y variable label in yti
loc yti `r(clntex)'
// Get LaTeX cleaned x variable label
texclean `"`: var l `x''"'
// Store the x variable label in xti
loc xti `r(clntex)'
// Add the scatterplot to the LaTeX document
file write doc "\begin{figure}[h!]" _n
file write doc `"\caption{Scatterplot of `yti' \& `xti' \label{fig:scatter`i'}}"' _n
file write doc `"\includegraphics[width=\textwidth]{scatter-`y'-`x'.pdf}"' _n
file write doc "\end{figure} \newpage\clearpage" _n
} // End Loop over scatter plot permutations
} // End IF Block for no byvars argument
// sequential by graphs
else if `"`byvars'"' != "" & "`byseq'" != "" {
} // End ELSEIF Block for sequential by graphs
// Lattice graphs
else if `"`byvars'"' != "" & "`byseq'" == "" {
} // End ELSE IF Block for Lattice style graphs
// End Program definition
end