-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspearman2.ado
executable file
·91 lines (65 loc) · 2.03 KB
/
spearman2.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
/***
Title
======
__spearman2__ -- Calculate generalized Spearman rho for a set of predictors and countinous outcome.
Syntax
------
__spearman2__ yvar varlist
- - -
Options:
> __yvar__ : Continuous variable
> __varlist__ : Variable list.
- - -
Description
-----------
__spearman2__ calculates the generalized Spearman's rho of the _yvar_ with each of the variables in _varlist_.
***/
*capture program drop spearman2
program spearman2
* REQUIRES egenmore
syntax varlist(fv) [if] [in]
display `"`varlist'"'
quietly{
preserve
local nvars : word count `varlist'
tokenize `"`varlist'"'
egen _spearman2_ranky = rank(`1')
generate _spearman2_varname = " "
replace _spearman2_varname = "Y = "+"`1'" in 1
label variable _spearman2_varname "Variable"
generate _spearman2_rho2 = .
label variable _spearman2_rho2 "Adjusted rho^2"
generate _spearman2_N = .
label variable _spearman2_N "N"
generate _spearman2_r2 = .
label variable _spearman2_r2 "rho^2"
forvalues i=2/`nvars' {
display "``i''"
display "Got here"
if substr("``i''",1,2) == "i."{
quietly regress _spearman2_ranky ``i'' `if' `in'
}
else {
egen _rank_``i'' = rank(``i'')
quietly regress _spearman2_ranky _rank_``i'' c._rank_``i''#c._rank_``i'' `if' `in'
}
replace _spearman2_varname = "``i''" in `i'
local r2adj = (1 - (1 - e(r2)) * (e(N) - 1) / e(df_r))
replace _spearman2_rho2 = `r2adj' in `i'
replace _spearman2_N = e(N) in `i'
replace _spearman2_r2 = e(r2) in `i'
}
keep in 1/`nvars'
keep _spearman2_*
generate _spearman2_forsort = -1*_spearman2_rho2
sort _spearman2_forsort in 2/`=_N'
generate _spearman2_row = `=_N' - _n
}
tabdisp _spearman2_varname, cell(_spearman2_rho2 _spearman2_r2 _spearman2_N)
quietly egen _spearman2_plotaxis = axis(_spearman2_forsort), label(_spearman2_varname)
graph dot (asis) _spearman2_rho2 in 2/`=_N', ///
over(_spearman2_plotaxis) ///
title(Potential Predictive Power) ///
ytitle(Spearman's {&rho}{superscript:2} (adjusted))
restore
end