-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnormalise.ado
51 lines (51 loc) · 1.42 KB
/
normalise.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
capture program drop normalise
program normalise
//--- Normalise with svy weights, optionally within groups
syntax varlist, postfix(string) [by(varlist)] [svy] [subpop(string)] [mean(real 0)] [sd(real 1)]
/* to do:
- option to preserve means and sds
/- do not use svy if option not selected
*/
local svyprefix
local meanif
local if
if strpos(`"`subpop'"', "if ") == 1 {
local if `subpop'
}
else if `"`subpop'"' != `""' {
local if if `subpop' & !missing(`subpop')
}
if "`svy'" == "" {
local meanif `if'
}
else {
local svyprefix svy, subpop(`subpop'):
}
*display `"svy: `svy', if: `if', meanif: `meanif', svyprefix: `svyprefix', by: `by'"'
if `"`by'"' == `""' {
foreach v in `varlist' {
`svyprefix' mean `v' `meanif'
matrix M = r(table)
matrix N = e(_N)
gen `v'`postfix' = `mean' + `sd' * (`v' - M[1, 1]) / (M[2, 1] * sqrt(N[1, 1])) `if'
}
}
else {
tempvar group
egen `group' = group(`by')
quietly sum `group'
local groups = r(max)
foreach v in `varlist' {
`svyprefix' mean `v' `meanif', over(`group', nolabel)
local labels = e(over_labels)
matrix M = r(table)
matrix N = e(_N)
matrix S = J(`groups', 2, .)
forvalues i=1/`: word count `labels'' {
display "Label `i': `: word `i' of `labels''"
matrix S[`: word `i' of `labels'', 1] = M[1, `i'], M[2, `i'] * sqrt(N[1, `i'])
}
gen `v'`postfix' = `mean' + `sd' * (`v' - S[`group', 1]) / S[`group', 2] `if'
}
}
end