-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy path_gkweight.ado
108 lines (107 loc) · 3.79 KB
/
_gkweight.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
* version 1.0 dec 2018 Fernando Rios Avila
* This program is used to created normalized kernel weight.
* Normalized kernel weights are such that k(0)=1. Good for measuring effective number of observations "close" to the pofr
capture program drop _gkweight
program define _gkweight
version 6
syntax newvarname =/exp [if] [in] , bw(real) pofr(real) [kernel(str)]
tempvar touse
quietly {
gen byte `touse' =0
replace `touse'= 1 `if' `in'
tempvar z
gen double `z'=(`exp'-`pofr')/`bw'
if "`kernel'"=="" | "`kernel'"=="gaussian" {
gen `typlist' `varlist' = normalden(`z')/normalden(0) if `touse' == 1
}
else if "`kernel'"=="epan" {
gen `typlist' `varlist' = (1-0.2*`z'^2) if `touse' == 1 & abs(`z')<5^.5
}
else if "`kernel'"=="epan2" {
gen `typlist' `varlist' = (1-`z'^2) if `touse' == 1 & abs(`z')<1
}
else if "`kernel'"=="biweight" {
gen `typlist' `varlist' = (1-`z'^2)^2 if `touse' == 1 & abs(`z')<1
}
else if "`kernel'"=="cosine" {
gen `typlist' `varlist' = (1+cos(2*_pi*`z'))/2 if `touse' == 1 & abs(`z')<0.5
}
else if "`kernel'"=="cosine2" {
gen `typlist' `varlist' = cos(_pi*`z'/2) if `touse' == 1 & abs(`z')<1
}
else if "`kernel'"=="parzen" {
gen `typlist' `varlist' = (1-6*`z'^2+6*abs(`z')^3) if `touse' == 1 & abs(`z')<=0.5
replace `varlist' = 2*(1-abs(`z'))^3 if `touse' == 1 & abs(`z')>0.5 & abs(`z')<=1
}
else if "`kernel'"=="rectan" {
gen `typlist' `varlist' = 1 if `touse' == 1 & abs(`z')<1
}
else if "`kernel'"=="trian" {
gen `typlist' `varlist' = 1-abs(`z') if `touse' == 1 & abs(`z')<1
}
else if "`kernel'"=="logistic" {
gen `typlist' `varlist' = 4*(1/(2+exp(`z')+exp(-`z'))) if `touse' == 1
}
else if "`kernel'"=="tricube" {
gen `typlist' `varlist' = (1-abs(`z')^3)^3 if `touse' == 1 & abs(`z')<1
}
else if "`kernel'"=="triweight" {
gen `typlist' `varlist' = (1-`z'^2)^3 if `touse' == 1 & abs(`z')<1
}
***For discreet Data
** For unordered Data
else if "`kernel'"=="liracine" {
if `bw'>=0 & `bw'<=1 {
gen `typlist' `varlist' = 1 if `touse' == 1 & float(abs(`exp'-`pofr'))==0
replace `varlist' = `bw' if `touse' == 1 & float(abs(`exp'-`pofr'))!=0
}
else {
display "For liracine, Bandwidth needs to be between 0 and 1"
exit
}
}
** For ordered Data
else if "`kernel'"=="liracine2" {
if `bw'>=0 & `bw'<=1 {
gen `typlist' `varlist' = 1 if `touse' == 1 & float(abs(`exp'-`pofr'))==0
replace `varlist' = `bw'^abs(`exp'-`pofr') if `touse' == 1 & float(abs(`exp'-`pofr'))!=0
}
else {
display "For liracine2, Bandwidth needs to be between 0 and 1"
exit
}
}
else if "`kernel'"=="habbena" {
if `bw'>=0 & `bw'<=1 {
gen `typlist' `varlist' = 1 if `touse' == 1 & float(abs(`exp'-`pofr'))==0
replace `varlist' = `bw'^(abs(`exp'-`pofr')^2) if `touse' == 1 & float(abs(`exp'-`pofr'))!=0
}
else {
display "For habbena, Bandwidth needs to be between 0 and 1"
exit
}
}
else if "`kernel'"=="logdis" {
if `bw'>=0 & `bw'<=1 {
gen `typlist' `varlist' = 1 if `touse' == 1 & float(abs(`exp'-`pofr'))==0
replace `varlist' = `bw'^(ln(abs(`exp'-`pofr')+1)) if `touse' == 1 & float(abs(`exp'-`pofr'))!=0
}
else {
display "For logdis, Bandwidth needs to be between 0 and 1"
exit
}
}
else if "`kernel'"=="dtrian" {
if `bw'>=0 & `bw'<=1 {
gen `typlist' `varlist' = 1 if `touse' == 1 & float(abs(`exp'-`pofr'))==0
replace `varlist' = 1-(1-`bw')*abs(`exp'-`pofr') if `touse' == 1 & float(abs(`exp'-`pofr'))!=0
replace `varlist' = 0 if `varlist'<0
}
else {
display "For dtrian, Bandwidth needs to be between 0 and 1"
exit
}
}
replace `varlist' =0 if `varlist'==. & `touse' == 1
}
end