Statalist


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

RE: st: RE: reinstate data after -contract- and -reshape-


From   "Martin Weiss" <martin.weiss1@gmx.de>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: RE: reinstate data after -contract- and -reshape-
Date   Sat, 28 Nov 2009 22:02:54 +0100

<>

Ok, if you do want to keep tabs on Stata, but I am telling you, it is not
necessary...


HTH
Martin


-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Michael McCulloch
Sent: Samstag, 28. November 2009 21:59
To: statalist@hsphsun2.harvard.edu
Subject: Re: st: RE: reinstate data after -contract- and -reshape-

Thanks!
I modified my example to confirm restoration of the original dataset,  
using -cf-:

*** begin
clear all
set seed 1
set obs 1000
gen id=_n
gen month=1 + int(12*uniform())
gen year=2008 + int(2*uniform())
gen subject_type=1 + int(3*uniform())
la def lab_subjects 1 "no reply" 2 "screened" 3 "sampled", modify
la val subject_type lab_subjects
g m=(year-1960)*12+month-1
format m %tmm_CY
la var m "Calendar month"
levelsof m, loc(vs)
foreach v of loc vs {
la def m `v' "`:di `:format m' `v''", modify
}
la val m m
by m, sort: gen freq = _N		// sort by the variable of interest
and get  
the frequencies
by m: gen cumfreq = _N if _n == 1	// get the cumulative frequencies
replace cumfreq = sum(cumfreq)
tabdisp m, cell(freq cumfreq)
preserve
save original, replace
contract m subject_type, z f(one)
reshape wide one, j(subject_type) i(m)
g noreply=max(0,one1)
g screened=max(0,one1)+max(0,one2)
g sampled=max(0,one1)+max(0,one2)+max(0,one3)
sc sampled screened noreply m, recast(bar) xla(576/599,angle(90))  
name(s)
restore
cf _all using original  // returns nothing if datasets are identical
***




On Nov 28, 2009, at 12:39 PM, Martin Weiss wrote:

>
> <>
>
> Stata is very reliable, so yes, it works the way it is supposed to.  
> A good
> source is of course http://www.stata.com/help.cgi?preserve and the  
> manual
> entry on -preserve-. You can also dump your data into a -tempfile-, of
> course.
>
> Stata 11 also has a -snapshot- feature
> (http://www.stata.com/help.cgi?snapshot), although I have not  
> explored this
> myself yet :-)
>
>
> HTH
> Martin
>
>
> -----Original Message-----
> From: owner-statalist@hsphsun2.harvard.edu
> [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Michael  
> McCulloch
> Sent: Samstag, 28. November 2009 21:35
> To: statalist@hsphsun2.harvard.edu
> Subject: Re: st: RE: reinstate data after -contract- and -reshape-
>
> Thanks Martin, from the UCLA tutorial site
> (http://www.ats.ucla.edu/stat/Stata/faq/stata2hlm_dofile.htm
> ), I gather that it's as simple as:
>
> ***
> preserve
> whatever you want to do here...
> restore
> ***
>
> Is that reliably the case?
> Michael
>
>
>
> On Nov 28, 2009, at 12:28 PM, Martin Weiss wrote:
>
>>
>> <>
>> The "advanced syntax" of -reshape- let`s you undo the previous -
>> reshape- in
>> a straightforward manner, but -contract- can only be undone via -
>> expand-.
>> Have you had a look at -preserve- and -restore-, though?
>>
>>
>> HTH
>> Martin
>>
>> -----Original Message-----
>> From: owner-statalist@hsphsun2.harvard.edu
>> [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Michael
>> McCulloch
>> Sent: Samstag, 28. November 2009 21:21
>> To: Statalist Statalist
>> Subject: st: reinstate data after -contract- and -reshape-
>>
>> Dear Statalist members,
>> With the help of Nick Cox and Martin Weiss, I've created the  
>> following
>> data simulation for a graph to track research subject accrual.
>> My question is how to reinstate the original dataset after the  -
>> contract- and -reshape- commands. I know it's possible to -reshape-,
>> but not sure how to "reverse" the options. I don't think it's  
>> possible
>> to "-uncontract'", so I'm thinking some version of -restore-??
>>
>> My understanding of those two commands is:
>> 	-contract- makes a summary dataset of frequencies and percentages
>> that will be reported in the graph, and
>> 	-reshape- puts them in wide form so they can be -stacked-
>>
>> ****
>> * Austin Nichols suggests:
>> clear all
>> set seed 1
>> set obs 1000
>> gen id=_n
>> gen month=1 + int(12*uniform())
>> gen year=2008 + int(2*uniform())
>> gen subject_type=1 + int(3*uniform())
>> la def lab_subjects 1 "no reply" 2 "screened" 3 "sampled", modify
>> la val subject_type lab_subjects
>> g m=(year-1960)*12+month-1
>> format m %tmm_CY
>> la var m "Calendar month"
>> levelsof m, loc(vs)
>> foreach v of loc vs {
>> la def m `v' "`:di `:format m' `v''", modify
>> }
>> la val m m
>> by m, sort: gen freq = _N		// sort by the variable of interest
>> and get
>> the frequencies
>> by m: gen cumfreq = _N if _n == 1	// get the cumulative frequencies
>> replace cumfreq = sum(cumfreq)
>> tabdisp m, cell(freq cumfreq)
>> contract m subject_type, z f(one)
>> reshape wide one, j(subject_type) i(m)
>> g noreply=max(0,one1)
>> g screened=max(0,one1)+max(0,one2)
>> g sampled=max(0,one1)+max(0,one2)+max(0,one3)
>> sc sampled screened noreply m, recast(bar) xla(576/599,angle(90))
>> name(s)
>> ****
>>
>>
>>
>> Best wishes,
>>
>> Michael McCulloch
>> Pine Street Foundation
>> 124 Pine Street
>> San Anselmo, CA 94960-2674
>> tel:	415-407-1357
>> fax: 	206-338-2391
>>
>>
>> *
>> *   For searches and help try:
>> *   http://www.stata.com/help.cgi?search
>> *   http://www.stata.com/support/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/statalist/faq
>> *   http://www.ats.ucla.edu/stat/stata/
>
>
> Best wishes,
>
> Michael McCulloch
> Pine Street Foundation
> 124 Pine Street
> San Anselmo, CA 94960-2674
> tel:	415-407-1357
> fax: 	206-338-2391
>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/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/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/


Best wishes,

Michael McCulloch
Pine Street Foundation
124 Pine Street
San Anselmo, CA 94960-2674
tel:	415-407-1357
fax: 	206-338-2391


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/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/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