dplyrsurveyweighted

How to include missing values in svytable in R to match Stata’s tab command?


Sure, here's a Stack Overflow question you could use:


I'm trying to replicate a Stata command in R using the survey package but I'm not getting the same results. In Stata, I use the following command to get weighted counts including missing values:

tab fp_message_radio [iweight=wt], missing

enter image description here

In R, I have created the survey design object and used svytable to get weighted counts for fp_message_radio:

# Create the survey design object
DHSdesign <- svydesign(ids = ~1, data = IRdata, weights = ~wt)

# Use svytable to get weighted counts for fp_message_radio
svytable(~fp_message_radio, DHSdesign)

However, this does not include the missing values. How can I modify my R code to include missing values and make it identical to the Stata command?


Solution

  • from the ?svytable help page, it looks like ... parameters get passed to the xtabs function, so would the addNA parameter from that function produce the results you expect? i've also included two lines to show the overall total.. thanks!!

    library(survey)
    data(api)
    x <- apiclus1
    x[ 1 , 'stype' ] <- NA
    x[ 2 , 'sch.wide' ] <- NA
    dclus1<-svydesign(id=~dnum, weights=~pw, data=x, fpc=~fpc)
    svytable(~sch.wide+stype, dclus1)
    svytable(~sch.wide+stype, dclus1,addNA=TRUE)
    
    dclus1 <- update( dclus1 , one = 1 )
    svytable( ~ one , dclus1 )