Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Seeking explanation of the internal procedures of -replace-


From   Jesper Kjr Hansen <jkh-stata@msecon.eu>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: Seeking explanation of the internal procedures of -replace-
Date   Thu, 24 May 2007 05:31:22 +0200

I am working on a simulation procedure, which randomly permutes
all variables in a varlist before each run (optionally permutes
groups of variables together).

Given original variable dtsim20, I would have thought that the
the two following command sequences would yield the same result,
because my understanding of -replace- was that it works on a
temp copy of the variable, which will not be replaced before
the very end of the -replace- procedure.

[see the full, but very basic example at the end of this post]

1. The "correct" command sequence(?)
. clonevar _dtsim20 = dtsim20
. replace  _dtsim20 = dtsim20[dtrank] if tag 

2. Incorrect, because of [some sort of circular ref.]?
. clonevar wrong = dtsim20
. replace  wrong = wrong[dtrank] if tag

Question:
Could someone please explain exactly why (2) doesn't work?

I guess that I will go with (1), unless someone has a more
clever idea? The only reason I'm asking is that I now have
to do quite a lot of clone/replace/drop/rename in my procedure.

Thanks in advance.


/Jesper K. Hansen

---- begin simple example ----
. sort dtsim20

. mark tag if indexnot(dtsim20,"?") != 0

. set seed 123586

. egen long dtrank = rank(uniform()) if tag , unique

. clonevar _dtsim20 = dtsim20

. replace  _dtsim20 = dtsim20[dtrank] if tag
(12 real changes made)

. clonevar wrong = dtsim20

. replace  wrong = wrong[dtrank] if tag
(7 real changes made)

. list dtsim20 _dtsim20 wrong dtrank tag

     +-------------------------------------------+
     | dtsim20   _dtsim20   wrong   dtrank   tag |
     |-------------------------------------------|
  1. |       +          -       -       17     1 |
  2. |       +          -       -       15     1 |
  3. |       +          +       +        3     1 |
  4. |       +          -       -       23     1 |
  5. |       +          -       -       19     1 |
     |-------------------------------------------|
  6. |       +          +       -        4     1 |
  7. |       +          -       -       28     1 |
  8. |       +          -       -       12     1 |
  9. |       -          -       -       18     1 |
 10. |       -          +       -        2     1 |
     |-------------------------------------------|
 11. |       -          -       -       30     1 |
 12. |       -          -       -        9     1 |
 13. |       -          +       -        5     1 |
 14. |       -          -       -       24     1 |
 15. |       -          -       -       16     1 |
     |-------------------------------------------|
 16. |       -          -       -       20     1 |
 17. |       -          -       -       27     1 |
 18. |       -          -       -       22     1 |
 19. |       -          -       -       21     1 |
 20. |       -          -       -       14     1 |
     |-------------------------------------------|
 21. |       -          +       -        7     1 |
 22. |       -          -       -       25     1 |
 23. |       -          -       -       26     1 |
 24. |       -          -       -       13     1 |
 25. |       -          +       -        6     1 |
     |-------------------------------------------|
 26. |       -          +       -        8     1 |
 27. |       -          -       -       10     1 |
 28. |       -          -       -       29     1 |
 29. |       -          +       -        1     1 |
 30. |       -          -       -       11     1 |
     +-------------------------------------------+

---- end simple example ----


-- Jesper Kjr Hansen
mailto:jkh-stata@msecon.eu


*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index