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: table command returns different values every time
From
Rijo John <[email protected]>
To
stata <[email protected]>
Subject
Re: st: table command returns different values every time
Date
Thu, 12 Dec 2013 17:40:59 +0530
Well, I figured out where the problem is happening, but unable to see why?
I have these 3 sets of codes somewhere before the earlier code I
posted. These codes merely display some frequencies. My program runs
fine even without these codes. But, when this code is present the
final numbers I am getting from my earlier code [reposted at the end]
changes every time, though very slightly. But when this codes are
commented out, the results from my earlier code gives consistent
figures for every run. What is wrong with these codes below? id and
sex are the only common variables between both the codes. I am unable
to attach the dataset data set here as it runs into several MBs.
*displays the number of unique households by sector in the dataset
egen tag=tag(sector hhid)
egen N = total(tag), by(sector)
bysort sector : gen freq = _N
tabdisp sector, c(N freq)
drop tag N freq
*displays the number of unique persons by sector in the dataset
egen tag=tag(sector id)
egen N = total(tag), by(sector)
bysort sector : gen freq = _N
tabdisp sector, c(N freq)
drop tag N freq
*displays the number of unique persons by sex in the dataset
egen tag=tag(sex id)
egen N = total(tag), by(sex)
bysort sex : gen freq = _N
tabdisp sex, c(N freq)
drop tag N freq
*earlier code that gives inconsistent results every time when the
above codes are present in the program:
<---Code Begins --->
local fruit "Apple Apricot Bananas Mango Orange"
local num "1 2 3 4 5"
local n : word count `fruit'
forvalues i=1/`n'{
sort id, stable
local a : word `i' of `fruit'
local b : word `i' of `num'
gen `a'=0
replace `a'=var1 if fruit==`b'
table state sex if ag==2 [pw=mult], c(mean `a')
}
<---Code Ends --->
On Thu, Dec 12, 2013 at 11:14 AM, Richard Williams
<[email protected]> wrote:
> I am with Nick -- I do not see any reason why you should be having problems.
> My theory is that there is something going on in the earlier code we aren't
> seeing. Are you, for example, generating a random variable, or drawing a
> random subsample? If so, you might need to set the seed first, e.g. start
> your code off with
>
> set seed 123456
>
>
> At 11:07 AM 12/11/2013, Nick Cox wrote:
>>
>> There are at least two problems in understanding this.
>>
>> First, and obviously, we don't have your data to check this (and you
>> don't provide an alternative reproducible example).
>>
>> Second, and more of a challenge, you are calling up -table- to show
>> some means in cells of a cross-classification. I may be missing
>> something, but there should be no way that depends on the sort order
>> of your data. Also, as you imply, that should be reproducible any way.
>>
>> Any chance of seeing an example we can reproduce?
>> Nick
>> [email protected]
>>
>>
>> On 11 December 2013 14:36, Rijo John <[email protected]> wrote:
>> > Dear Statalist,
>> >
>> > I am running the following code to generate a certain table. Every
>> > time restart stata and run the code it returns results wherein some of
>> > the values are slightly different compared to the previous run. I am
>> > talking about differences at 2nd, 3rd or higher decimals. What can I
>> > do to make this stable? I think the difference is happening may be
>> > because the sorting order is changing every time I restart stata. It
>> > doesn't change within the same session. If I don't give the sort
>> > command in between it still changes every time I run the program.
>> >
>> > <---Code Begins --->
>> > local fruit "Apple Apricot Bananas Mango Orange"
>> > local num "1 2 3 4 5"
>> > local n : word count `fruit'
>> > forvalues i=1/`n'{
>> > sort id, stable
>> > local a : word `i' of `fruit'
>> > local b : word `i' of `num'
>> > gen `a'=0
>> > replace `a'=var1 if fruit==`b'
>> > table State sex if ag==2 [pw=mult], c(mean `a')
>> > }
>> > <---Code Ends --->
>> > *
>> > * 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/
>
>
> -------------------------------------------
> Richard Williams, Notre Dame Dept of Sociology
> OFFICE: (574)631-6668, (574)631-6463
> HOME: (574)289-5227
> EMAIL: [email protected]
> WWW: http://www.nd.edu/~rwilliam
>
>
> *
> * 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/