Search code examples
rsurvey

Unweighted svytable


I am using the survey package in R. I am working with survey data and using the svydesign() and update() functions to manipulate the dataset (create new variables, etc).

Here is how I am getting the weighted crosstabs:

## Build svytable
Drinks_Sex <- svytable(~ Sex + Drinks, design=x)
## Cell Totals
round(addmargins(Drinks_Sex),0)

##         Drinks
## Sex          0     1   Sum
##   Female  6501   213  6714
##   Male    5254   157  5411
##   Sum    11755   370 12125

Is there a way for me to get the unweighted crosstabs using the survey package? I know how to get the unweighted crosstabs using base R on the original dataset, but the problem is that doing so would not allow me to analyze any variables using update().

Alternately: is there any way for me to propagate the work I've done using update() into the original dataset (in csv format) so I can work it using base R?


Solution

  • update is the same function as transform. if you want to just replace-all your update functions with transform functions and at the same time replace your survey.design with a data.frame then it will work.

    library(survey)
    data(api)
    dclus1<-svydesign(id=~dnum, weights=~pw, data=apiclus1, fpc=~fpc)
    
    
    # variables in the data.frame object inside the design are here: dclus1$variables
    # so
    head( dclus1$variables )
    # is the same as
    head( apiclus1 )
    
    # make whatever new variable
    dclus1 <- update( dclus1 , new_col = target * 3 )
    # equivalent to
    # apiclus1 <- transform( apiclus1 , new_col = target * 3 )
    # so long as you are working with a data.frame object instead of a survey.design
    
    # just access the `variables` attribute of the survey.design object
    # as if it were a data.frame object.  note: this will not work on some very complicated designs (possibly will not work on database-backed or multiply-imputed or calibrated designs)
    
    # single unweighted table
    table( dclus1$variables[ , 'new_col' ] )
    
    # crosstabs
    table( dclus1$variables[ , c( 'awards' , 'new_col' ) ] )