Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: assigning calculated values to other groups (was: Data problem again)


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: assigning calculated values to other groups (was: Data problem again)
Date   Fri, 14 Nov 2003 15:04:04 -0000

Ernest Berkhout replied to Lok Wong 

> >I have "area,education,gender,wage"in my dataset...Now I 
> want to calculate 
> >the mean and GINI coefficient of only male wage by area and 
> >education...and assign these values to females in the same 
> area-education 
> >group..How could I do that?
> 
> First, let me point out that you might learn something from 
> the collection 
> of FAQs on
> http://www.stata.com/support/faqs/
> 
> In a quick scan, I think that this one might be of interest to you 
> http://www.stata.com/support/faqs/data/weighted.html but 
> ofcourse thare may 
> be other as well.
> 
> Apart from that, your questions sounds like the following 
> approach to me:
> 
> egen group=(area education)
> levels group
> foreach x of local group  {
>          summ wage if sex=="male" [aw=weight]
>          replace wage = r(mean)
> }

In addition -- and note that Ernest's suggestion 
is schematic, and not workable code -- there 
is a general "copying down" technique useful 
in many problems. 

Lok Wong wants to calculate for _males_ and to 
copy the results to _females_. An analogue of 
that with the auto data is to calculate 
for foreign cars, and copy to domestic. 

. bysort rep78 : egen meanmpg = mean(mpg) if foreign 
(52 missing values generated)

. sort rep78 meanmpg 

. list  rep78 meanmpg foreign 

     +-----------------------------+
     | rep78    meanmpg    foreign |
     |-----------------------------|
  1. |     1          .   Domestic |
  2. |     1          .   Domestic |
  3. |     2          .   Domestic |
  4. |     2          .   Domestic |
  5. |     2          .   Domestic |
     |-----------------------------|
  6. |     2          .   Domestic |
  7. |     2          .   Domestic |
  8. |     2          .   Domestic |
  9. |     2          .   Domestic |
 10. |     2          .   Domestic |
     |-----------------------------|
 11. |     3   23.33333    Foreign |
 12. |     3   23.33333    Foreign |
 13. |     3   23.33333    Foreign |
 14. |     3          .   Domestic |
 15. |     3          .   Domestic |
     |-----------------------------|
 16. |     3          .   Domestic |
 17. |     3          .   Domestic |
 18. |     3          .   Domestic |
 19. |     3          .   Domestic |
 20. |     3          .   Domestic |
     |-----------------------------|
 21. |     3          .   Domestic |
 22. |     3          .   Domestic |
 23. |     3          .   Domestic |
 24. |     3          .   Domestic |
 25. |     3          .   Domestic |
     |-----------------------------|
 26. |     3          .   Domestic |
 27. |     3          .   Domestic |
 28. |     3          .   Domestic |
 29. |     3          .   Domestic |
 30. |     3          .   Domestic |
     |-----------------------------|
 31. |     3          .   Domestic |
 32. |     3          .   Domestic |
 33. |     3          .   Domestic |
 34. |     3          .   Domestic |
 35. |     3          .   Domestic |
     |-----------------------------|
 36. |     3          .   Domestic |
 37. |     3          .   Domestic |
 38. |     3          .   Domestic |
 39. |     3          .   Domestic |
 40. |     3          .   Domestic |
     |-----------------------------|
 41. |     4   24.88889    Foreign |
 42. |     4   24.88889    Foreign |
 43. |     4   24.88889    Foreign |
 44. |     4   24.88889    Foreign |
 45. |     4   24.88889    Foreign |
     |-----------------------------|
 46. |     4   24.88889    Foreign |
 47. |     4   24.88889    Foreign |
 48. |     4   24.88889    Foreign |
 49. |     4   24.88889    Foreign |
 50. |     4          .   Domestic |
     |-----------------------------|
 51. |     4          .   Domestic |
 52. |     4          .   Domestic |
 53. |     4          .   Domestic |
 54. |     4          .   Domestic |
 55. |     4          .   Domestic |
     |-----------------------------|
 56. |     4          .   Domestic |
 57. |     4          .   Domestic |
 58. |     4          .   Domestic |
 59. |     5   26.33333    Foreign |
 60. |     5   26.33333    Foreign |
     |-----------------------------|
 61. |     5   26.33333    Foreign |
 62. |     5   26.33333    Foreign |
 63. |     5   26.33333    Foreign |
 64. |     5   26.33333    Foreign |
 65. |     5   26.33333    Foreign |
     |-----------------------------|
 66. |     5   26.33333    Foreign |
 67. |     5   26.33333    Foreign |
 68. |     5          .   Domestic |
 69. |     5          .   Domestic |
 70. |     .         14    Foreign |
     |-----------------------------|
 71. |     .          .   Domestic |
 72. |     .          .   Domestic |
 73. |     .          .   Domestic |
 74. |     .          .   Domestic |
     +-----------------------------+

Once we've got things in that sort order, 
we can copy down from the non-missings 
to the missings. We do this _within_ 
appropriate groups: 

. bysort rep78 : replace meanmpg = meanmpg[_n-1] if mi(meanmpg) 
(42 real changes made)

. list  rep78 meanmpg foreign 

     +-----------------------------+
     | rep78    meanmpg    foreign |
     |-----------------------------|
  1. |     1          .   Domestic |
  2. |     1          .   Domestic |
  3. |     2          .   Domestic |
  4. |     2          .   Domestic |
  5. |     2          .   Domestic |
     |-----------------------------|
  6. |     2          .   Domestic |
  7. |     2          .   Domestic |
  8. |     2          .   Domestic |
  9. |     2          .   Domestic |
 10. |     2          .   Domestic |
     |-----------------------------|
 11. |     3   23.33333    Foreign |
 12. |     3   23.33333    Foreign |
 13. |     3   23.33333    Foreign |
 14. |     3   23.33333   Domestic |
 15. |     3   23.33333   Domestic |
     |-----------------------------|
 16. |     3   23.33333   Domestic |
 17. |     3   23.33333   Domestic |
 18. |     3   23.33333   Domestic |
 19. |     3   23.33333   Domestic |
 20. |     3   23.33333   Domestic |
     |-----------------------------|
 21. |     3   23.33333   Domestic |
 22. |     3   23.33333   Domestic |
 23. |     3   23.33333   Domestic |
 24. |     3   23.33333   Domestic |
 25. |     3   23.33333   Domestic |
     |-----------------------------|
 26. |     3   23.33333   Domestic |
 27. |     3   23.33333   Domestic |
 28. |     3   23.33333   Domestic |
 29. |     3   23.33333   Domestic |
 30. |     3   23.33333   Domestic |
     |-----------------------------|
 31. |     3   23.33333   Domestic |
 32. |     3   23.33333   Domestic |
 33. |     3   23.33333   Domestic |
 34. |     3   23.33333   Domestic |
 35. |     3   23.33333   Domestic |
     |-----------------------------|
 36. |     3   23.33333   Domestic |
 37. |     3   23.33333   Domestic |
 38. |     3   23.33333   Domestic |
 39. |     3   23.33333   Domestic |
 40. |     3   23.33333   Domestic |
     |-----------------------------|
 41. |     4   24.88889    Foreign |
 42. |     4   24.88889    Foreign |
 43. |     4   24.88889    Foreign |
 44. |     4   24.88889    Foreign |
 45. |     4   24.88889    Foreign |
     |-----------------------------|
 46. |     4   24.88889    Foreign |
 47. |     4   24.88889    Foreign |
 48. |     4   24.88889    Foreign |
 49. |     4   24.88889    Foreign |
 50. |     4   24.88889   Domestic |
     |-----------------------------|
 51. |     4   24.88889   Domestic |
 52. |     4   24.88889   Domestic |
 53. |     4   24.88889   Domestic |
 54. |     4   24.88889   Domestic |
 55. |     4   24.88889   Domestic |
     |-----------------------------|
 56. |     4   24.88889   Domestic |
 57. |     4   24.88889   Domestic |
 58. |     4   24.88889   Domestic |
 59. |     5   26.33333    Foreign |
 60. |     5   26.33333    Foreign |
     |-----------------------------|
 61. |     5   26.33333    Foreign |
 62. |     5   26.33333    Foreign |
 63. |     5   26.33333    Foreign |
 64. |     5   26.33333    Foreign |
 65. |     5   26.33333    Foreign |
     |-----------------------------|
 66. |     5   26.33333    Foreign |
 67. |     5   26.33333    Foreign |
 68. |     5   26.33333   Domestic |
 69. |     5   26.33333   Domestic |
 70. |     .         14    Foreign |
     |-----------------------------|
 71. |     .         14   Domestic |
 72. |     .         14   Domestic |
 73. |     .         14   Domestic |
 74. |     .         14   Domestic |
     +-----------------------------+

More on the principle at 
http://www.stata.com/support/faqs/data/missing.html

Nick 
[email protected] 

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index