未加权的svytable

时间:2016-02-18 00:12:53

标签: r survey

我正在使用R中的survey包。我正在使用调查数据并使用svydesign()update()函数来操作数据集(创建新变量等)。< / p>

以下是我获取加权交叉表的方法:

## 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

有没有办法让我使用survey包获得未加权交叉表?我知道如何在原始数据集上使用基数R来获取未加权的交叉表,但问题是这样做不允许我使用update()分析任何变量。

或者:我有什么方法可以将我使用update()完成的工作传播到原始数据集中(采用csv格式),这样我就可以使用基本R来处理它了吗?

1 个答案:

答案 0 :(得分:3)

updatetransform功能相同。如果您只想使用update函数替换所有transform函数,同时将survey.design替换为data.frame,那么它将会有效。

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' ) ] )