There are several different ways to calculate percentiles or quantiles. The best way in to discussion I know is this paper (but it's not exhaustive)
jstor.org/stable/2684934
I don't have an answer on which method is best.
Beyond that, and by the way, binning in Stata isn't guaranteed to produce equal bin frequencies,
If you want to match what an Excel function does, you would need to consult its documentation to find out which recipe it is using and find matching code in Stata or write your own.
In Stata 19.50, the quantile() function in Mata offers a wide range of recipes.
You don't need 20 commands for that mapping. Here is one of several other methods illustrated.
So, that needs 1 command, not 20.
That said, if you really want 20 bins, you don't have to go via percentiles at all. Here is a silly example with serious code.
By default, equal values receive the same rank (and in due course end in the same bin). mpg is recorded as integers, with several ties, and the numbers in each of 20 bins vary from 2 to 9. The problem may be less serious with your kind of data.
For more discussion, see (e.g.)
Links to those papers:
https://www.stata-journal.com/articl...article=pr0054 (esp. Section 4)
https://www.stata-journal.com/articl...article=dm0095 (esp. Section 6)
Thanks for the link, but you need to give a page or other precise reference to where the percentile or binning rule is discussed.
jstor.org/stable/2684934
I don't have an answer on which method is best.
Beyond that, and by the way, binning in Stata isn't guaranteed to produce equal bin frequencies,
If you want to match what an Excel function does, you would need to consult its documentation to find out which recipe it is using and find matching code in Stata or write your own.
In Stata 19.50, the quantile() function in Mata offers a wide range of recipes.
You don't need 20 commands for that mapping. Here is one of several other methods illustrated.
Code:
. clear
. set obs 100
Number of observations (_N) was 0, now 100.
. gen y = _n
. gsort -y
. gen x = 21 - ceil(y/5)
. l y x in 1/20, sep(5)
+---------+
| y x |
|---------|
1. | 100 1 |
2. | 99 1 |
3. | 98 1 |
4. | 97 1 |
5. | 96 1 |
|---------|
6. | 95 2 |
7. | 94 2 |
8. | 93 2 |
9. | 92 2 |
10. | 91 2 |
|---------|
11. | 90 3
12. | 89 3 |
13. | 88 3 |
14. | 87 3 |
15. | 86 3 |
|---------|
16. | 85 4 |
17. | 84 4 |
18. | 83 4 |
19. | 82 4 |
20. | 81 4 |
+---------+
.
That said, if you really want 20 bins, you don't have to go via percentiles at all. Here is a silly example with serious code.
By default, equal values receive the same rank (and in due course end in the same bin). mpg is recorded as integers, with several ties, and the numbers in each of 20 bins vary from 2 to 9. The problem may be less serious with your kind of data.
Code:
. sysuse auto, clear
(1978 automobile data)
. egen rank = rank(mpg)
. gen bin20 = ceil(20 * rank/74)
. tabstat mpg, s(n min max) by(bin20)
Summary for variables: mpg
Group variable: bin20
bin20 | N Min Max
---------+------------------------------
1 | 2 12 12
2 | 6 14 14
3 | 2 15 15
4 | 4 16 16
5 | 4 17 17
7 | 9 18 18
9 | 8 19 19
10 | 3 20 20
12 | 5 21 21
13 | 5 22 22
14 | 3 23 23
15 | 4 24 24
16 | 5 25 25
17 | 3 26 26
18 | 3 28 28
19 | 4 29 31
20 | 4 34 41
---------+------------------------------
Total | 74 12 41
----------------------------------------
Code:
SJ-12-4 pr0054 . . . . . . . . . . Speaking Stata: Matrices as look-up tables
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
Q4/12 SJ 12(4):748--758 (no commands)
illustrates the use of matrices as look-up tables
SJ-18-3 dm0095 . . . . . . . . . . . Speaking Stata: From rounding to binning
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
Q3/18 SJ 18(3):741--754 (no commands)
basic review of how to bin variables in Stata, meaning how to
divide their range or support into disjoint intervals
https://www.stata-journal.com/articl...article=pr0054 (esp. Section 4)
https://www.stata-journal.com/articl...article=dm0095 (esp. Section 6)
Thanks for the link, but you need to give a page or other precise reference to where the percentile or binning rule is discussed.

Comment