Percentile conditioned on another variable

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Percentile conditioned on another variable



Consider the following toy dataset with two variables:


* Example generated by -dataex-. To install: ssc install dataex
clear
input str10 date float size
"30.06.2018" 15
"29.06.2018" 10
"30.06.2018" 12
"01.07.2018" 13
"29.06.2018" 20
"30.06.2018" 22
"01.07.2018" 9
end



For each date I want to drop the top 1% and bottom 1% of size. So, if there are 100 observations for 30.06.2018 the highest and the lowest size on this date should be dropped.


30.06.2018



I can generate percentiles for a variable but I struggle to do it while conditioning on another variable.





So, what happens with less than 100 observations for each date? Check out egen either way, but watch what it does with different group sizes.
– Nick Cox
Jul 5 at 10:22


egen





Experiment suggests that you will drop the min and max even for sample sizes less than 100. Is that what you want?
– Nick Cox
Jul 5 at 16:21




1 Answer
1



The following code snippet works:


by date: egen one_percentile = pctile(size), p(1)
by date: egen ninetynine_percentile = pctile(size), p(99)
generate outlier = 0
replace outlier = 1 if size <= one_percentile | size >= ninetynine_percentile
drop if outlier == 1



If there are <100 observations for a date, the lowest and highest will be dropped.



However, one can get around this by counting the observations for each date and only dropping the outlier if the count is >= 100.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Better method to check all objects' parameters with a single call

C++ virtual function: Base class function is called instead of derived