Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Using "x" from the ""x" real change made" output of -replace-


From   [email protected]
To   [email protected]
Subject   Re: st: Using "x" from the ""x" real change made" output of -replace-
Date   Mon, 3 Dec 2012 21:51:00 -0500

Thank you Mr. Cox,

it's good to know that my use of -count- and r(N) to complement my use of
-replace- is not too "inelegant" after all.

> I doubt that anyone would want to try to follow exactly what you are
> doing here without much more explanation.
>
> But this question reminds me of a discussion several years ago on
> Statalist, although I won't try to search for "replace" as a keyword
> in the archives.
>
> In principle, it would be very useful if -replace- were a r-class
> command so that you could just inspect say r(change) to see if it were
> 0.
>
> Or so it might seem.
>
> Any way, changing -replace- to a r-class command would wreak havoc
> among programs and do files across Stata, to say nothing of other
> issues.
>
> And -replace- does not do that now, so speculation helps you not at all.
>
> So, in general I know of no substitute for comparing the absolute or
> relative difference of latest with previous.
>
> Depending on what kind of iteration is being used, convergence might
> be flagged by something other than exact zero change.
>
> Nick
>
> Maxime Roy asked
>
>> How do I go about using "x" from the ""x" real change made" output of
>> -replace-?
>> I am using Stata12 for Windows.
>>
>> I am using an iterative procedure and I would like to stop iterating
>> when
>> there are no more real changes happening through -replace-.
>>
>> Here is a code that would work if "r(change)" after replace would return
>> "x" from the ""x" real change made" output of replace.
>>
>> gen ID3 = ID2
>> local change = 1
>> while `change' > 0{
>> sort ID t2
>> by ID: replace ID3=ID3[1]
>> local change1 = r(change)
>> sort ID2 t2
>> by ID2: replace ID3=ID3[1]
>> local change2 = r(change)
>> replace ID2=ID3
>> local change = `change1' + `change2'
>> }
>>
>> Based on the help file of generate and replace, there does not seem to
>> be
>> a direct way of obtaining "x" from the ""x" real change made" output.
>> Does
>> a statalist user have a suggestion?
>>
>> The following works, but I am seeking a more elegant/fast solution:
>>
>> gen ID3 = ID2
>> local change = 1
>> while `change' > 0{
>> sort ID t2
>> by ID: gen diff=1 if ID3!=ID3[1]
>> count if diff==1
>> local change1 = r(N)
>> by ID: replace ID3=ID3[1]
>> sort ID2 t2
>> by ID2: gen diff2=1 if ID3!=ID3[1]
>> count if diff2==1
>> local change2 = r(N)
>> by ID2: replace ID3=ID3[1]
>> replace ID2=ID3
>> drop diff diff2
>> local change = `change1' + `change2'
>> }
>>
>> Thank you
>>
>> - Maxime Roy
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/faqs/resources/statalist-faq/
> *   http://www.ats.ucla.edu/stat/stata/
>
>


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index