-
Notifications
You must be signed in to change notification settings - Fork 0
/
enwei.ado
222 lines (160 loc) · 3.99 KB
/
enwei.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
program enwei, rclass sortpreserve
version 12.0
syntax varlist(min=1) , Order(numlist) [GENerate(string) DIMension(string) REPlace Biase(numlist) ]
local varN : word count `varlist'
local numN : word count `order'
if `varN' != `numN'{
di ""
di as err "The number of varibles is not equal the number of orders"
}
else{
local N=`varN'
capture sum
local M=r(N)
}
local missvar=""
local varN : word count `varlist'
forvalue i=1/`varN'{
local Var:word `i' of `varlist'
capture misstable summarize `Var' ,showzeros all
if r(N_eq_dot) != 0 | r(N_gt_dot) != 0{
local missvar `missvar' `Var'
}
}
local missvarN : word count `missvar'
if `missvarN' >0{
dis ""
di as err "Warning: These variables have missing values "
dis "`missvar'"
}
mkmat `varlist' ,matrix(raw)
if "`biase'" != ""{
local bia=`biase'
}
else{
local bia=1/(`M'*10000)
}
mat data_s=J(`M',1,.)
forvalue i=1(1)`N'{
local var:word `i' of `varlist'
local k:word `i' of `order'
mat v`i'=raw[1...,`i']
mata:v = st_matrix("v`i'")
if `k' != 0{
mata:sv = (v - min(v)*J(rows(v), 1, 1)) / (max(v)-min(v)) + `bia'*J(rows(v), 1, 1)
mata:st_matrix("sv", sv)
mat `var'_s=sv
}
else{
mata:sv = (max(v)*J(rows(v), 1, 1) - v) / (max(v)-min(v)) + `bia'*J(rows(v), 1, 1)
mata:st_matrix("sv", sv)
mat `var'_s=sv
}
mat data_s=[data_s,`var'_s]
}
mat data_s=data_s[1...,2...]
mata:data_s = st_matrix("data_s")
mat p=J(`M',1,.)
forvalue i=1(1)`N'{
mat v`i'=data_s[1...,`i']
mata:v = st_matrix("v`i'")
mata:ps = v / colsum(v)
mata:st_matrix("ps", ps)
mat p=[p,ps]
}
mat p=p[1...,2...]
mata:p = st_matrix("p")
mata:p_lnp = p :* ln(p)
mata:E = -1/ln(`M') * colsum(p_lnp)
mata:D = J(1,`N', 1) - E
mata:W = D/rowsum(D)
mata:Index=data_s*W'
matrix OrderM = J(1,`N',.)
forvalues i=1/`N'{
matrix OrderM[1,`i']=`: word `i' of `order''
}
/*Display*/
matrix colnames OrderM = `varlist'
matrix rownames OrderM = "direction"
mata:st_matrix("E", E)
matrix colnames E = `varlist'
matrix rownames E = "E"
mata:st_matrix("D", D)
matrix colnames D = `varlist'
matrix rownames D = "D"
mata:st_matrix("W", W)
matrix colnames W = `varlist'
matrix rownames W = "W"
mata:st_matrix("Index", Index)
dis ""
dis as text "Order"
dis as text "non-zero means Positive;0 means negative"
matlist OrderM
dis ""
dis "Entropy value"
dis as text "E"
matlist E ,format(%9.3f)
dis ""
dis "Information entropy redundancy"
dis as text "D"
matlist D ,format(%9.3f)
dis ""
dis "weight"
dis as text "W"
matlist W ,format(%9.3f)
//generate variables
if "`generate'" != ""{
matrix colnames Index = "`generate'"
if "`replace'" != "" {
cap drop `generate'
svmat Index ,names(col)
label var `generate' "Score"
}
else{
svmat Index ,names(col)
label var `generate' "Score"
}
}
else{
matrix colnames Index = "Entropy"
if "`replace'" != "" {
cap drop Entropy
svmat Index ,names(col)
label var Entropy "Score"
}
else{
svmat Index ,names(col)
label var Entropy "Score"
}
}
if "`dimension'" != ""{
mat DIM=J(`M',1,.)
forvalue i=1(1)`N'{
//scalar w`i'=W[1...,`i']
mat D`i'=W[1...,`i'] * data_s[1...,`i']
mat DIM=[DIM,D`i']
}
mat DIM=DIM[1...,2...]
local dname=""
forvalue i=1(1)`N'{
local var:word `i' of `varlist'
local dname `dname' `dimension'_`var'
}
if "`replace'" != "" {
cap drop `dname'
}
matrix colnames DIM = `dname'
svmat DIM ,names(col)
forvalue i=1(1)`N'{
local var:word `i' of `varlist'
label var `dimension'_`var' "score of `var'"
}
}
// store results in rclass
return matrix OrderM = OrderM
return matrix E = E
return matrix D = D
return matrix W = W
return matrix Index = Index
cap return matrix DIM = DIM
end