Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


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

st: Re: Snowball sampling


From   "Joseph Coveney" <stajc2@gmail.com>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: Re: Snowball sampling
Date   Wed, 17 Apr 2013 21:00:50 +0900

Ray Hawkins wrote:

I posted a similar question a while ago, but have a related question. My
data looks the following (without 'keep'), a network data. To generate
'keep', I ran the following code. However, this code covers the first two
steps (up to giveid 11) of the whole network starting giveid=17 -> 2 -> 11
-> 18 -> 32 -> 40 -> 16. That is, keep=1 only for giveid=17,2,11. I would
like to keep all 7 giveid that are connected. The problem is that I don't
know how many steps I need to go to keep all connected id. Or, even if I
know, the code will be very complicated to go to 50 or 100 steps, for
example. Is there any way I can keep all those connected id starting from a
seed id? If Stata cannot handle this problem, then maybe I need to use a
social network software? Thank you.

--------------------------------------------------------------------------------

You can do something like that below.  I just put it together hurriedly and,
although it works for your example, I haven't spent any time testing or refining
it.  More efficient and more robust approaches would undoubtedly occur to
someone upon reflection.  Regardless, the point is that Stata can handle this
problem; it's not inevitable that you'd need to resort to social network
software.

Joseph Coveney

version 12.1

clear *
set more off

input int giveid int recid keep
2	11	1
3	15	0
6	10	0
11	18	1
11	190	1
16	187	0
17	1	1
17	2	1
17	5	1
17	23	1
18	32	0
19	782	0
32	17	0
32	21	0
32	23	0
32	37	0
32	40	0
32	68	0
33	111	0
40	16	0
40	20	0
40	70	0
40	92	0
41	15	0
41	22	0
41	23	0
41	27	0
end
drop keep

program define keepem, rclass
	version 12.1
	syntax varlist(min=2 max=2 numeric), Criterion(integer) Keep(name)
	
	capture confirm variable `keep'
	if _rc generate byte `keep' = 0

	gettoken recid giveid : varlist

	quietly levelsof `recid' if `giveid' == `criterion', local(keepers)
	quietly foreach keeper of local keepers {
		replace `keep' = 1 if `giveid' == `keeper'
	}
	return local keepers `keepers'
end

*
* Start at 17 and go from there
*
local keepers 17
while "`keepers'" != "" {
	foreach keeper of local keepers {
		keepem recid giveid, c(`keeper') keep(keep)
		local returned_keepers `r(keepers)'
		local new_keepers : list new_keepers | returned_keepers
	}
	local done_keepers : list keepers | done_keepers
	local keepers : list new_keepers - done_keepers
}

quietly keep if keep
drop keep
contract giveid, freq(recipient_tally)

list, noobs separator(0) abbreviate(20)

exit

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