Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

drawing a stratified sample in R

Tags:

r

survey

sampling

Designing my stratified sample

library(survey)
design <- svydesign(id=~1,strata=~Category,  data=billa, fpc=~fpc)

So far so good, but how can I draw now a sample in the same way I was able for simple sampling?

set.seed(67359)  
samplerows <- sort(sample(x=1:N, size=n.pre$n))
like image 680
Roland Kofler Avatar asked Dec 12 '22 08:12

Roland Kofler


1 Answers

If you have a stratified design, then I believe you can sample randomly within each stratum. Here is a short algorithm to do proportional sampling in each stratum, using ddply:

library(plyr)
set.seed(1)
dat <- data.frame(
    id = 1:100,
    Category = sample(LETTERS[1:3], 100, replace=TRUE, prob=c(0.2, 0.3, 0.5))
)

sampleOne <- function(id, fraction=0.1){
  sort(sample(id, round(length(id)*fraction)))
}

ddply(dat, .(Category), summarize, sampleID=sampleOne(id, fraction=0.2))

   Category sampleID
1         A       21
2         A       29
3         A       72
4         B       13
5         B       20
6         B       42
7         B       58
8         B       82
9         B      100
10        C        1
11        C       11
12        C       14
13        C       33
14        C       38
15        C       40
16        C       63
17        C       64
18        C       71
19        C       92
like image 200
Andrie Avatar answered Jan 02 '23 00:01

Andrie