-
Notifications
You must be signed in to change notification settings - Fork 0
/
wcorr.ado
129 lines (108 loc) · 4.22 KB
/
wcorr.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
/* -*- coding: utf-8 -*- */
// wcorr program
capture program drop wcorr
program wcorr, rclass
version 12
syntax varlist(min=1 max=99 fv ts) [using] [if] [in], ///
[ /// // options
sort /// // Sort variables by name
spearman /// // Display spearman correlations
format(string asis) /// // Format for stats in table
modelwidth(int 6) /// // Width of the models in STATA output
label /// // Use variable labels or variable names?
debug /// // output debug information
* ]
quietly count `if'
if r(N) == 0 {
display "No observations found!"
exit
}
local noisily quietly
if "`debug'" != "" local noisily noisily
/* Format the inputs and sets up local variables. */
tokenize varlist
fvunab varlist: `varlist'
local varlist: list uniq varlist
if "`sort'" != "" local varlist: list sort varlist
if `"`statslabels'"' == `""' local statslabels `"`stats'"'
if `"`format'"'==`""' {
local format "%-5.2f"
local mwidth
}
local nvars : word count `varlist'
/*
Add variables to list for re-labeling with (i) in front.
If Spearman is requested, calculate temp vars which are the rank(var),
because the correlation of ranks == Spearman.
*/
local i=0
local max_str_len = 1
local eql
/* This only allows for a maximum of 99 variables. Which is sane. */
if `nvars' > 9 local space " "
foreach v in `varlist' {
local i = `i'+1
/* Check if variable is valid */
tempvar var_`i'
if "`spearman'" != "" {
capture egen `var_`i'' = rank(`v')
}
else {
capture gen `var_`i'' = `v'
}
/* If _rc != 0, then we hit an error in the variable
(probably a factor variable), so ignore it */
if _rc {
local i = `i'-1
continue
}
/* If we're greater than 9, drop the added space in front of ( i) */
if `i' > 9 local space ""
/* Make new varlist with temp-variable names in it */
local newvarlist `" `newvarlist' `var_`i'' "'
/* Label those tem variables in estout below, using:
"tempvarname" "(i) oldvarname" */
if "`label'" != "" ///
local lab: variable label `v'
if ("`label'" == "") | ("`lab'" == "") ///
local lab "`v'"
local full_lab "(`space'`i') `lab'"
local varlab `"`varlab' "`var_`i''" "`full_lab'" "'
/* Add (i) to the equation label string along the top of the table */
local eql `"`eql' "(`i')" "'
/* Track the length of the longest label (and add 4 below) */
local i_str_len = strlen("`full_lab'")
if `i_str_len' > `max_str_len' local max_str_len `i_str_len'
}
/* Run correlation matrix */
capture estpost correlate `newvarlist' `if' `in', matrix listwise
if _rc xi: estpost correlate `newvarlist' `if' `in', matrix listwise
/* Output results */
`noisily' display `"esttab, unstack not nonumbers nomtitle noobs compress /// "'
`noisily' display `" cells(b(fmt(`format') star)) /// "'
`noisily' display `" star(* 0.01) /// "'
`noisily' display `" eqlabels(`eql') /// "'
`noisily' display `" collabels(none) /// "'
`noisily' display `" varwidth(`max_str_len') /// "'
`noisily' display `" modelwidth(`modelwidth') /// "'
`noisily' display `" varlabels(`varlab') `options' "'
esttab ., unstack not nonumbers nomtitle noobs compress ///
cells(b(fmt(`format') star)) ///
star(* 0.01) ///
eqlabels(`eql') ///
collabels(none) ///
varwidth(`max_str_len') ///
modelwidth(`modelwidth') ///
varlabels(`varlab') `options'
if `"`using'"' != `""' ///
esttab . `using', ///
unstack not nonumbers nomtitle noobs compress nogaps ///
cells(b(fmt(`format') star)) ///
star(* 0.01) ///
eqlabels(`eql') ///
collabels(none) ///
varwidth(`max_str_len') ///
modelwidth(`modelwidth') ///
varlabels(`varlab') `options'
end
// end wcorr program