Statalist The Stata Listserver


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

RE: st: identify strata with one distinct value of psu


From   "Brent Fulton" <[email protected]>
To   <[email protected]>
Subject   RE: st: identify strata with one distinct value of psu
Date   Mon, 18 Dec 2006 13:25:18 -0800

Austin, your options work and I modified the first option (see below).

I agree the -generate- option on -svydes- should say something to the
effect, with my added parentheticals:
(For observations with complete data), stores a variable that identifies
strata with a single sampling unit for a given sampling stage. (If an
observation has missing data, then the variable is 0.)

Given the above, I think the following is okay since notok is 0 for: a)
observations with complete data that are not in a stratum with one sampling
unit; and b) observations with incomplete data, which will be dropped

svydes $y $x, gen(notok)
svy, subpop(if notok==0): reg $y $x

The SEs of the above were the same as the SEs for this model:
svy, subpop(ok): reg $y $x

Long term, I think I'll take your suggestion to impute the missing values.

-Brent



-----Original Message-----
From: [email protected]
[mailto:[email protected]]On Behalf Of Austin Nichols
Sent: Monday, December 18, 2006 11:21 AM
To: [email protected]; Brent Fulton
Subject: Re: st: identify strata with one distinct value of psu


All--
Sorry, for "$y $x" read "loglead sizp" in my example, or try:

webuse nhanes2, clear
replace sizp=. if psu==2 & str>10
global y loglead
global x sizp
svydes $y $x, gen(notok)
tab stra notok
egen not=max(notok), by(strat)
g ok=1-not
tab stra ok
qui reg $y $x if !mi(weight,psu,strata)
g new_psu=psu if e(sample)
egen sd_psu=sd(new_psu), by(strata)
qui levelsof strata if sd_psu>0, local(ok)
di "`ok'"
local oklist: subinstr loc ok " " ",", all
di "`oklist'"
g sok=inlist(strata,`oklist')
tab ok sok
svy, subpop(ok): reg $y $x


PS. Brent, you can also
 set trace on
 set traced 1
to see how locals and globals are evaluated by Stata... a very useful
pair of commands for many purposes, in fact.
*
*   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/



-----Original Message-----
From: [email protected]
[mailto:[email protected]]On Behalf Of Austin Nichols
Sent: Monday, December 18, 2006 11:13 AM
To: [email protected]; Brent Fulton
Subject: st: identify strata with one distinct value of psu


Brent--

There are a number of points here, so I will respond in a public forum.
First, my code is wrong when it computes a count of nonmissing psu by
stratum (which does *not* identify strata with a single distinct value
for psu, so the answer to my question "does that work?" is a decided
No).  Second, it seems to me that the -generate- option of official
Stata -svydes- does not work as advertised in the help file--can
someone verify and comment?  Third, I don't think that restricting the
regression using the subpop option should eliminate the problem--I
think you have to collapse strata and make a new strata_id variable
and issue a -svyset- command to tell Stata what you've done.  Finally,
I don't see any problem with spaces or commas in the locals--perhaps
you can use display commands and verify that they are being defined
correctly?

Here are two ways of identifying strata with one distinct value of
psu, both of which are actually tested and seem to work:

webuse nhanes2, clear
replace sizp=. if psu==2 & str>10
svydes loglead sizp, gen(notok)
tab stra notok
egen not=max(notok), by(strat)
g ok=1-not
tab stra ok
qui reg $y $x if !mi(weight,psu,strata)
g new_psu=psu if e(sample)
egen sd_psu=sd(new_psu), by(strata)
qui levelsof strata if sd_psu>0, local(ok)
di "`ok'"
local oklist: subinstr loc ok " " ",", all
di "`oklist'"
g sok=inlist(strata,`oklist')
tab ok sok
svy, subpop(ok): reg $y $x


On 12/18/06, Brent Fulton <[email protected]> wrote:
> Hi Austin,
>
> Thank you for your help. I am having problems translating local(ok) so it
> can be used in inlist. When I used your code below, all observations are
> evaluated as true in the subpop command.
>
> I see subinstr is supposed to replace spaces with commas, but it doesn't
> seem to be doing that. I ran di `ok' and it produces a set of levels with
no
> spaces in between each level. When I run di `oklist', it produces a set of
> levels separated by spaces (not commas).
>
> I then tried:
> levelsof strata if n_psu>1, local(ok) separate(,) /* which separates the
> levels with commas */
> And then tried using `ok' in inlist, but had the same problem as above.
>
> Lastly, I tried using the function r(levels), which is outputted from
> levelsof, but that also didn't work--same problem as above.
>
> Any thoughts?
>
> I think the general problem is that levelsof outputs the levels using one
> type of syntax while the local macro stores it with a different syntax.
This
> has always been a frustration of mine using Stata--it's hard to figure out
> the object that the information is stored in as well as the format of the
> object. Any general advice would be appreciated.
>
> Thanks,
> Brent
>
>
>
> -----Original Message-----
> From: [email protected]
> [mailto:[email protected]]On Behalf Of Austin Nichols
> Sent: Sunday, December 17, 2006 2:24 AM
> To: [email protected]
> Subject: Re: st: programming: automate identification of strata with one
> psu
>
>
> Brent--
>
> Ignoring more "statistical" solutions such as multiple imputation or
> estimating conservative SEs by ignoring strata, suppose you quietly
> regress without -svy- then calculate n of PSUs per stratum if
> e(sample) then issue a -levelsof- strata if the n exceeds 1, e.g.
>
> qui reg y $x if !mi(weight,psu,strata)
> g u_psu=1 if e(sample)
> egen n_psu=count(u_psu), by(strata)
> levelsof strata if n_psu>1, local(ok)
> local oklist: subinstr loc ok " " ",", all
> drop n_psu u_psu
> svy, subpop(inlist(strata,`oklist')): reg y $x
>
> Does that work?
*
*   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/

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