【发布时间】:2015-11-16 23:54:57
【问题描述】:
我想使用包含复杂调查样本设计的另一列(连续变量)的分位数来计算新列。这个想法是在数据框中创建一个新变量,该变量指示每个观察值属于哪个分位数组
这是我在不包含示例设计的情况下执行该想法的方式,因此您可以了解我的目标。
# Load Data
data(api)
# Convert data to data.table format (mostly to increase speed of the process)
apiclus1 <- as.data.table(apiclus1)
# Create deciles variable
apiclus1[, decile:=cut(api00,
breaks=quantile(api00,
probs=seq(0, 1, by=0.1), na.rm=T),
include.lowest= TRUE, labels=1:10)]
我尝试使用 survey 包中的 svyquantile,但我无法解决这个问题。此代码不会将分位数组作为我可以输入新变量的输出返回。对此有什么想法吗?
# Load Package
library(survey)
# create survey design
dclus1 <- svydesign(id=~dnum, weights=~pw, data=apiclus1, fpc=~fpc)
# What I've tried to do
svyquantile(~api00, design = dclus1, quantiles = seq(0, 1, by=0.1), method = "linear", ties="rounded")
【问题讨论】: