如何使用“sampling”包在R中创建分层样本?我的数据集有355,000个观测值。代码在最后一行工作正常。下面是我写的代码,但我总是得到以下消息:“sort.list(y)中的错误:'x'必须是'sort.list'的原子'你在列表上调用'sort'吗?”
请不要指向Stackoverflow上的旧消息。我研究了它们,但一直无法使用它们。谢谢。
## lpdata file has 355,000 observations
# Exclude Puerto Rico, Virgin Islands and Guam
sub.lpdata<-subset(lpdata,"STATE" != 'PR' | "STATE" != 'VI' | "STATE" != 'GU')
## Create a 10% sample, stratified by STATE
sort.lpdata<-sub.lpdata[order(sub.lpdata$STATE),]
tab.state<-data.frame(table(sort.lpdata$STATE))
size.strata<-as.vector(round(ceiling(tab.state$Freq)*0.1))
s<-strata(sort.lpdata,stratanames=sort.lpdata$STATE,size=size.strata,method="srswor")}
答案 0 :(得分:5)
stratified = function(df, group, size) {
# USE: * Specify your data frame and grouping variable (as column
# number) as the first two arguments.
# * Decide on your sample size. For a sample proportional to the
# population, enter "size" as a decimal. For an equal number
# of samples from each group, enter "size" as a whole number.
#
# Example 1: Sample 10% of each group from a data frame named "z",
# where the grouping variable is the fourth variable, use:
#
# > stratified(z, 4, .1)
#
# Example 2: Sample 5 observations from each group from a data frame
# named "z"; grouping variable is the third variable:
#
# > stratified(z, 3, 5)
#
require(sampling)
temp = df[order(df[group]),]
if (size < 1) {
size = ceiling(table(temp[group]) * size)
} else if (size >= 1) {
size = rep(size, times=length(table(temp[group])))
}
strat = strata(temp, stratanames = names(temp[group]),
size = size, method = "srswor")
(dsample = getdata(temp, strat))
}
答案 1 :(得分:0)
在不知道分层功能的情况下 - 一些编码可能会做什么:
d <- expand.grid(id = 1:35000, stratum = letters[1:10])
p = 0.1
dsample <- data.frame()
system.time(
for(i in levels(d$stratum)) {
dsub <- subset(d, d$stratum == i)
B = ceiling(nrow(dsub) * p)
dsub <- dsub[sample(1:nrow(dsub), B), ]
dsample <- rbind(dsample, dsub)
}
)
# size per stratum in resulting df is 10 % of original size:
table(dsample$stratum)
HTH, 凯
ps:我的笔记本电脑上的CPU时间是0.09!