添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Dear Statalist,
I am trying to compute percentiles across distinct values of a given variable.
A possible approach, as exemplified in this topic was to write something like this:
Code:
 egen mpg_group = xtile(mpg), by(weight_group) nq(3) 
However, Stata 13 apparently does not recognize anymore this syntax:
Code:
 unknown egen function xtile()
or the alternative:
Code:
. bysort weight_group: gen mpg_group = xtile(mpg)
unknown function xtile()
I thought then to loop over values of weight_group:
Code:
levelsof weight_group, local(levels2)
foreach k of local levels2 {
    xtile mpg_group_`k' = mpg, nq(3) 
but the loop fails to assign percentiles by weight_group as it can be verified by comparing mpg_group_0 with mpg_group_1.
Code:
corr mpg_group_0 mpg_group_1
sort mpg_group* weight_group
order mpg_group* weight_group mpg, last
Any hints on any alternative syntax or possible solution? Thank you very much.
Andre Various different problems here:
1. xtile()
as an egen function was only ever a user-written function downloadable via ssc inst egenmore.
If your Stata 13 doesn't recognise it, that is only because it has not been installed where Stata 13 can see it.
2. xtile() as a Stata function doesn't exist. This remains true in Stata 14,
3. I can't reproduce whatever the problem is here as you don't give data or explain where the dataset comes from. We can't see the result of the correlation.
Downloading egenmore appears be your solution, however. Again, thank you. Egenmore is indeed the solution.
I was replicating my problem using the same data that you used in the post that I linked.
Yes, you're right - my idea was just a quick a way of visually inspect whether percentiles were assigned taking into consideration the values of weight_group.
Andre
levelsof weight_group, local(levels2)
foreach k of local levels2 {
    xtile mpg_group_`k' = mpg, nq(3)
Look closely at what's inside your loop.  You do create different variables for each value of k.  But on the right hand side of the equals sign, everything is the same each time through the loop.  So you ended up with a bunch of new variables, all containing the same thing.  You need to distinguish the iterations of the loop, restricting application to a single different weight_group each time.  
Code:
levelsof weight_group, local(levels2)
foreach k of local levels2 {
    xtile mpg_group_`k' = mpg if weight_group == `k', nq(3)
								Dear Clyde,
Thank you for your explanation. I was not sure myself whether I would need to restrict the command to each k.
Andre While there are many economists, econometricians, and financiers who are active contributors to this Forum, this community is international and multidisciplinary. I am sure I am not the only one here who finds the jargon in this statement impenetrable and have no idea what you mean. You will expand your options for getting a timely and helpful response by using only language that would be understandable to anybody with a college degree and a basic understanding of statistics. Dear Clyde Schechter,
I am writing my master thesis and I would like to create a variable that indicates in which country-specific income tertile the individual is.
I used your above written command:
levelsof country , local(countries)
foreach i of local countries {
xtile countryspectertiles_`i' = income if country == `i', nq(3)
This created a list of variables for the income tertiles for each country. What I would like to do is create a variable indicating in which income tertile the individual is depending on the country he or she resides (so merging this list of variables into one variable).
Could you help me create this variable?
Thank you in advance,
Rita Samudra