[R] "survey" package -- doesn't appear to match svy

Anthony Damico ajdamico at gmail.com
Tue Oct 28 08:42:13 CET 2014


could you provide a minimal reproducible example?  perhaps use ?dput.


in general the survey package matches all other languages
http://journal.r-project.org/archive/2009-2/RJournal_2009-2_Damico.pdf


here's an example of a minimal reproducible example that does match
http://www.ats.ucla.edu/stat/stata/faq/svy_stata_subpop.htm


library(foreign)
library(survey)
x <- read.dta( "
http://www.ats.ucla.edu/stat/stata/seminars/svy_stata_intro/strsrs.dta" )
y <- svydesign( ~ 1 , strat = ~strat , data = x , weights = ~ pw , fpc =
~fpc )
svytable( ~ yr_rnd , y )
z <- subset( y , yr_rnd == 1 )
svymean( ~ ell , z )



there are lots of examples of R matching official published statistics
(often computed with stata) here--
https://github.com/ajdamico/usgsd/search?utf8=%E2%9C%93&q=replication





On Mon, Oct 27, 2014 at 11:11 AM, Stephen Amrock <stephen.amrock at gmail.com>
wrote:

> Hi,
>
> I'm new to R and have encountered two issues in coding using the "survey"
> package:
>
> (1) Code from *svytable* using "survey" package does not correspond to
> Stata estimates from *svy: tab*. I call
>
>     svyd.nation <- svydesign(ids = ~1, probs = ~wt_national, strata =
> ~stratum, data=nats.sub)
>     svytable(formula = ~wpev, design = svyd.nation, Ntotal = 100)
>
> where the equivalent Stata would be:
>
>     svyset [pw=wt_national], strata(stratum)
>     svy: tab wpev
>
> These are inverse probability weights but Stata and R give different %s for
> the tabulations. I know from published results that the Stata code is
> correct.  Any ideas as to what I've done incorrectly in R?
>
> (2) Alternative weights---which I've verified in R have the same
> distribution as in Stata---produce a strange "inf" output.
>
>     svyd.st <- svydesign(ids = ~1, probs = ~wt_state, strata = ~stratum,
> data=nats.sub)
>     svytable(~wpev, design = svyd.st, Ntotal=100)
>
> This produces the following output:
>
>     wpev
>     0 1
>
> The code
>     svytable(~wpev, svyd.st)
>
> produces the output:
>
>     wpev
>     0   1
>     Inf Inf
>
> Why are these alternative weights -- which work in Stata when resetting
> svyset -- not working in R?
>
> Any insights would be much appreciated!! Many thanks!
> - S
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list