5

如何使用“采样”包在 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")}
4

2 回答 2

6

去年我不得不做类似的事情。如果这是你经常做的事情,你可能想要使用下面的函数。此函数可让您指定要从中采样的数据框的名称、哪个变量是 ID 变量、哪个是分层,以及是否要使用“set.seed”。您可以将该函数保存为“stratified.R”之类的内容,并在需要时加载它。见http://news.mrdwab.com/2011/05/20/stratified-random-sampling-in-r-from-a-data-frame/

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))
}
于 2012-03-15T04:56:18.147 回答
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:我的relict笔记本电脑的CPU时间是0.09!

于 2012-03-14T15:21:50.173 回答