*! Version 1.1.0 2022-06-28 *! Author: Dejin Xie (Nanchang University, China) ** Version 1.1.0 2022-06-28, option and "base" added ** Version 1.0.0 2018-11-17, the initial version ****** The Grey Relational Analysis (GRA) ****** capture program drop greyra program define greyra, rclass sortpreserve version 13 syntax varlist (numeric) [if] [in], [ SCore(string) Prefix(string) SIgma(real 0.5) /// UNdes(varlist) By(varlist) General THeta(real 0.5) /// SHift(numlist max=1 min=1 >0 <=1) SMooth(numlist max=1 min=1 >0 <1) /// Norm Mean First Ewm Wcoef(numlist) REc BAse List Keep ] if `c(stata_version)'>=14 { local u "u" } gettoken depvar contvar: varlist local contn : word count `contvar' tokenize `contvar' capture drop GRAID gen GRAID=_n marksample touse, novarlist if "`score'"=="" { local score "GRA_Score" } if "`prefix'"=="" { local prefix "GRA" } if "`by'"~="" { local BY_TIME "bysort `by' (GRAID) :" local TIME_BY ", by(`by')" local unOPT "undes(`undes')" } else { if "`base'"~="" { dis as err "The option {it:{ul:ba}se} must be set with option {it:{ul:b}y(varlist)}." exit } } if "`mean'"~="" & "`norm'"~="" { dis as error "option {it:{ul:m}ean} not allowed with {it:{ul:n}orm}." exit(198) } if "`mean'"~="" & "`first'"~="" { dis as error "option {it:{ul:m}ean} not allowed with {it:{ul:f}irst}." exit(198) } if "`norm'"~="" & "`first'"~="" { dis as error "option {it:{ul:n}orm} not allowed with {it:{ul:f}irst}." exit(198) } if (`sigma'<0 | `sigma'>1) { dis as error "The value of option {it:{ul:si}gma(#)} must be between 0 and 1." exit(198) } if (`theta'<0 | `theta'>1) { dis as error "The value of option {it:{ul:th}eta(#)} must be between 0 and 1." exit(198) } if "`ewm'"~="" & "`wcoef'"~="" { dis as error "option {it:{ul:e}wm} not allowed with {it:{ul:w}coef}." exit(198) } if "`wcoef'"~="" { local wfnm : word count `wcoef' if `wfnm'~=`contn' { dis as error "The number of {it:varlist} (except the first variable) and {it:{ul:w}coef} must be the same." exit(198) } } if "`shift'"~="" & "`smooth'"~="" { dis as err "option {it:{ul:sh}ift(#)} not allowed with {it:{ul:sm}ooth(#)}." error 198 } if "`shift'"~="" { local shOPT " shift(`shift')" } if "`smooth'"~="" { local smOPT " smooth(`smooth')" } quietly { capture drop *_XI capture drop `prefix'_* capture drop `score' capture drop *_AGR capture drop *_RGR capture drop *_SGR preserve tempfile myGRAfile keep if `touse' if "`ewm'"~="" { EWM `contvar', keep `unOPT' `byOPT' `shOPT' `smOPT' `base' } *** The Basic Grey Relational Analysis *** tempvar depv_MAXM depv_MINM depv_NRM depv_MEAN depv_SD `BY_TIME' egen `depv_MAXM'=max(`depvar') `BY_TIME' egen `depv_MINM'=min(`depvar') gen `depv_NRM'=(`depvar'-`depv_MINM')/(`depv_MAXM'-`depv_MINM') replace `depv_NRM'=1-`depv_NRM' if `u'strpos(" `undes' "," `depvar' ") if "`shift'"~="" replace `depv_NRM'=`depv_NRM'+`shift' if "`smooth'"~="" replace `depv_NRM'=`smooth'+(1-`smooth')*`depv_NRM' `BY_TIME' egen `depv_MEAN'=mean(`depvar') if ("`mean'"~="" | "`norm'"~="") `BY_TIME' egen `depv_SD'=sd(`depvar') if ("`mean'"~="" | "`norm'"~="") replace `depv_NRM'=`depvar'/`depv_MEAN' if "`mean'"~="" replace `depv_NRM'=-`depv_NRM' if "`mean'"~="" & `u'strpos(" `undes' "," `depvar' ") & "`rec'"=="" replace `depv_NRM'=1/`depv_NRM' if "`mean'"~="" & `u'strpos(" `undes' "," `depvar' ") & "`rec'"~="" replace `depv_NRM'=(`depvar'-`depv_MEAN')/`depv_SD' if "`norm'"~="" replace `depv_NRM'=-`depv_NRM' if "`norm'"~="" & `u'strpos(" `undes' "," `depvar' ") & "`rec'"=="" replace `depv_NRM'=1/`depv_NRM' if "`norm'"~="" & `u'strpos(" `undes' "," `depvar' ") & "`rec'"~="" `BY_TIME' replace `depv_NRM'=`depvar'/`depvar'[1] if "`first'"~="" `BY_TIME' replace `depv_NRM'=-`depv_NRM' if "`first'"~="" & `u'strpos(" `undes' "," `depvar' ") & "`rec'"=="" `BY_TIME' replace `depv_NRM'=1/`depv_NRM' if "`first'"~="" & `u'strpos(" `undes' "," `depvar' ") & "`rec'"~="" forvalues vi=1/`contn' { tempvar ``vi''_MAXM ``vi''_MINM ``vi''_NRM ``vi''_MEAN ``vi''_SD ``vi''_DFF `BY_TIME' egen ```vi''_MAXM'=max(``vi'') `BY_TIME' egen ```vi''_MINM'=min(``vi'') gen ```vi''_NRM'=(``vi''-```vi''_MINM')/(```vi''_MAXM'-```vi''_MINM') replace ```vi''_NRM'=1-```vi''_NRM' if `u'strpos(" `undes' "," ``vi'' ") if "`shift'"~="" replace ```vi''_NRM'=```vi''_NRM'+`shift' if "`smooth'"~="" replace ```vi''_NRM'=`smooth'+(1-`smooth')*```vi''_NRM' `BY_TIME' egen ```vi''_MEAN'=mean(``vi'') if ("`mean'"~="" | "`norm'"~="") `BY_TIME' egen ```vi''_SD'=sd(``vi'') if ("`mean'"~="" | "`norm'"~="") replace ```vi''_NRM'=``vi''/```vi''_MEAN' if "`mean'"~="" replace ```vi''_NRM'=-```vi''_NRM' if "`mean'"~="" & `u'strpos(" `undes' "," ``vi'' ") & "`rec'"=="" replace ```vi''_NRM'=1/```vi''_NRM' if "`mean'"~="" & `u'strpos(" `undes' "," ``vi'' ") & "`rec'"~="" replace ```vi''_NRM'=(``vi''-```vi''_MEAN')/```vi''_SD' if "`norm'"~="" replace ```vi''_NRM'=-```vi''_NRM' if "`norm'"~="" & `u'strpos(" `undes' "," ``vi'' ") & "`rec'"=="" replace ```vi''_NRM'=1/```vi''_NRM' if "`norm'"~="" & `u'strpos(" `undes' "," ``vi'' ") & "`rec'"~="" `BY_TIME' replace ```vi''_NRM'=``vi''/``vi''[1] if "`first'"~="" `BY_TIME' replace ```vi''_NRM'=-```vi''_NRM' if "`first'"~="" & `u'strpos(" `undes' "," ``vi'' ") & "`rec'"=="" `BY_TIME' replace ```vi''_NRM'=1/```vi''_NRM' if "`first'"~="" & `u'strpos(" `undes' "," ``vi'' ") & "`rec'"~="" gen ```vi''_DFF'=abs(```vi''_NRM'-`depv_NRM') capture drop ``vi''_RMAX `BY_TIME' egen ``vi''_RMAX=max(```vi''_DFF') capture drop ``vi''_RMIN `BY_TIME' egen ``vi''_RMIN=min(```vi''_DFF') } tempvar VMAX VMIN egen `VMAX'=rowmax(*_RMAX) egen `VMIN'=rowmin(*_RMIN) drop *_RMIN *_RMAX forvalues vi=1/`contn' { capture drop ``vi''_XI gen ``vi''_XI=(`VMIN'+`sigma'*`VMAX')/(```vi''_DFF'+`sigma'*`VMAX') capture drop ``vi''_WXI if "`ewm'"~="" { gen ``vi''_WXI=``vi''_XI*``vi''_EW } if "`wcoef'"~="" { gen ``vi''_WXI=``vi''_XI*real(word("`wcoef'",`vi')) } capture drop `prefix'_``vi'' `BY_TIME' egen `prefix'_``vi''=mean(``vi''_XI) label var `prefix'_``vi'' "GR coef of ``vi'' to `depvar'" } capture drop `score' if "`ewm'"=="" & "`wcoef'"=="" { egen `score'=rowmean(*_XI) label var `score' "Gray relation degree of each obs" } if "`ewm'"~="" | "`wcoef'"~="" { egen `score'=rowtotal(*_WXI) label var `score' "Gray relation degree of each obs (Weight)" } capture drop *_WXI order *_XI, after(`score') if "`keep'"=="" { drop *_XI } if "`ewm'"~="" { capture drop *_EW capture drop EW_Score } *** The Generalized Grey Relational Analysis *** if "`general'"~="" { tempvar depv_ZERO depv_ZSD depv_FRA depv_FZER depv_FSD depv_FRME `BY_TIME' gen `depv_ZERO'=`depvar'-`depvar'[1] replace `depv_ZERO'=`depvar'-`depv_MEAN' if "`mean'"~="" `BY_TIME' egen `depv_ZSD'=total(`depv_ZERO') `BY_TIME' replace `depv_ZSD'=`depv_ZSD'-0.5*`depv_ZERO'[_N] `BY_TIME' gen `depv_FRA'=`depvar'/`depvar'[1] replace `depv_FRA'=`depvar'/`depv_MEAN' if "`mean'"~="" `BY_TIME' gen `depv_FZER'=`depv_FRA'-`depv_FRA'[1] `BY_TIME' egen `depv_FRME'=mean(`depv_FZER') if "`mean'"~="" replace `depv_FZER'=`depv_FRA'-`depv_FRME' if "`mean'"~="" `BY_TIME' egen `depv_FSD'=total(`depv_FZER') `BY_TIME' replace `depv_FSD'=`depv_FSD'-0.5*`depv_FZER'[_N] forvalues vi=1/`contn' { tempvar ``vi''_ZERO ``vi''_ZSD ``vi''_ZFD ``vi''_ZFS ``vi''_FRA ``vi''_FZER ``vi''_FSD ``vi''_FFD ``vi''_FFS ``vi''_FRME `BY_TIME' gen ```vi''_ZERO'=``vi''-``vi''[1] replace ```vi''_ZERO'=``vi''-```vi''_MEAN' if "`mean'"~="" `BY_TIME' egen ```vi''_ZSD'=total(```vi''_ZERO') `BY_TIME' replace ```vi''_ZSD'=```vi''_ZSD'-0.5*```vi''_ZERO'[_N] `BY_TIME' gen ```vi''_ZFD'=```vi''_ZERO'-`depv_ZERO' `BY_TIME' egen ```vi''_ZFS'=total(```vi''_ZFD') `BY_TIME' replace ```vi''_ZFS'=```vi''_ZFS'-0.5*```vi''_ZFD'[_N] capture drop ``vi''_AGR gen ``vi''_AGR=(1+abs(`depv_ZSD')+abs(```vi''_ZSD'))/(1+abs(`depv_ZSD')+abs(```vi''_ZSD')+abs(```vi''_ZFS')) label var ``vi''_AGR "ABS GR of ``vi'' to `depvar'" `BY_TIME' gen ```vi''_FRA'=``vi''/``vi''[1] replace ```vi''_FRA'=``vi''/```vi''_MEAN' if "`mean'"~="" `BY_TIME' gen ```vi''_FZER'=```vi''_FRA'-```vi''_FRA'[1] `BY_TIME' egen ```vi''_FRME'=mean(```vi''_FRA') if "`mean'"~="" replace ```vi''_FZER'=```vi''_FRA'-```vi''_FRME' if "`mean'"~="" `BY_TIME' egen ```vi''_FSD'=total(```vi''_FZER') `BY_TIME' replace ```vi''_FSD'=```vi''_FSD'-0.5*```vi''_FZER'[_N] `BY_TIME' gen ```vi''_FFD'=```vi''_FZER'-`depv_FZER' `BY_TIME' egen ```vi''_FFS'=total(```vi''_FFD') `BY_TIME' replace ```vi''_FFS'=```vi''_FFS'-0.5*```vi''_FFD'[_N] capture drop ``vi''_RGR gen ``vi''_RGR=(1+abs(`depv_FSD')+abs(```vi''_FSD'))/(1+abs(`depv_FSD')+abs(```vi''_FSD')+abs(```vi''_FFS')) label var ``vi''_RGR "REL GR of ``vi'' to `depvar'" capture drop ``vi''_SGR gen ``vi''_SGR=`theta'*``vi''_AGR+(1-`theta')*``vi''_RGR label var ``vi''_SGR "SYN GR of ``vi'' to `depvar'" } } save `myGRAfile', replace restore merge 1:1 GRAID using `myGRAfile', nogenerate if "`list'"~="" { preserve collapse (mean) `prefix'_* `TIME_BY' noisily dis as green "The grey relational degree of {res:`contvar'} to {res:`depvar'} are as follows: " noisily list restore if "`general'"~="" { preserve collapse (mean) *_AGR if `touse' `TIME_BY' noisily dis noisily dis _dup(90) "{bf:{err:~}}" noisily dis as green "The {bf:{it:absolute}} grey relational degree of {res:`contvar'} to {res:`depvar'} are as follows: " noisily list restore preserve collapse (mean) *_RGR if `touse' `TIME_BY' noisily dis noisily dis as green "The {bf:{it:relative}} grey relational degree of {res:`contvar'} to {res:`depvar'} are as follows: " noisily list restore preserve collapse (mean) *_SGR if `touse' `TIME_BY' noisily dis noisily dis as green "The {bf:{it:synthetic}} grey relational degree of {res:`contvar'} to {res:`depvar'} are as follows: " noisily list restore } } drop GRAID } end