Hello everybody,
I am wondering what I have to change to be able to use if conditions for the following "programmed command":
program iqr_ethn, rclass
version 16
syntax varlist(numeric) [if] [=exp]
* Count occurrences for each unique value
egen frequencies = count(`varlist') if `varlist'<., by(`varlist')
* Compute Q1, Q3, IQR, and the threshold
qui sum frequencies if `varlist' <. , detail
local Q1 = r(p25)
local Q3 = r(p75)
local IQR = `Q3' - `Q1'
local threshold = `Q3' + 3*`IQR'
* Assign 1 to outliers (i.e. the ingroup) and 0 otherwise
replace ingroup = 0 if ingroup==. & `varlist'<.
replace ingroup = 1 if ingroup<. & frequencies > `threshold' & `varlist'<.
if `threshold' == `Q3' {
replace ingroup = 1 if ingroup == 0 & frequencies >= `Q3' & `varlist'<.
}
tab frequencies
sum frequencies, detail
drop frequencies
tab `varlist' ingroup if `varlist'<.
end
The Variable on which I want to apply the command is the following:
Many thanks in advance for helping me!!!
I am wondering what I have to change to be able to use if conditions for the following "programmed command":
program iqr_ethn, rclass
version 16
syntax varlist(numeric) [if] [=exp]
* Count occurrences for each unique value
egen frequencies = count(`varlist') if `varlist'<., by(`varlist')
* Compute Q1, Q3, IQR, and the threshold
qui sum frequencies if `varlist' <. , detail
local Q1 = r(p25)
local Q3 = r(p75)
local IQR = `Q3' - `Q1'
local threshold = `Q3' + 3*`IQR'
* Assign 1 to outliers (i.e. the ingroup) and 0 otherwise
replace ingroup = 0 if ingroup==. & `varlist'<.
replace ingroup = 1 if ingroup<. & frequencies > `threshold' & `varlist'<.
if `threshold' == `Q3' {
replace ingroup = 1 if ingroup == 0 & frequencies >= `Q3' & `varlist'<.
}
tab frequencies
sum frequencies, detail
drop frequencies
tab `varlist' ingroup if `varlist'<.
end
The Variable on which I want to apply the command is the following:
Code:
* Example generated by -dataex-. For more info, type help dataex clear input double ETHNIC 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 80 end label values ETHNIC ETHNIC label def ETHNIC 80 "South+Latin America", modify
Comment