Maciej
Maciej

Reputation: 3303

Sampling package - strange behaviour of strata function

In function strata I declare numer of units to be drawn from each stratum and after executing it I get strange result. Numer of units is different from which I declared.

Here is example:

library(sampling)
data(swissmunicipalities)
st=strata(swissmunicipalities,stratanames=c("REG"),size=c(30,20,45,15,20,11,44),method="srswor") 

Result:

> table(st$REG)
 1   2   3  4   5    6  7 
20 15 45 30 20 11 44 

Should be:

30,20,45,15,20,11,44

I would be very grateful for anwsers.

Upvotes: 0

Views: 1650

Answers (1)

dickoa
dickoa

Reputation: 18437

if you read the help page of the strata function, you have :

size: vector of stratum sample sizes (in the order in which the
      strata are given in the input data set).

Let's check this order in which numbers are in the REG variable

require(sampling)
data(swissmunicipalities)
swiss <- swissmunicipalities
unique(swiss$REG)
## [1] 4 1 3 2 5 6 7

As you can see they are not naturally orderered, so you have two options.

First options, write the size in same order as the original data.

size <- c(15, 30, 45, 20, 20, 11, 44)
st <- strata(swiss, stratanames = "REG", size = size, method = "srswor")
table(st$REG)
##  1  2  3  4  5  6  7 
## 30 20 45 15 20 11 44

Second option, order the data first and keep you size as you first wrote it

swiss <- swiss[order(swiss$REG), ]
st <- strata(swiss, stratanames = "REG", size = c(30, 20, 45, 15, 20, 11, 44), 
method = "srswor")
table(st$REG)
##  1  2  3  4  5  6  7 
## 30 20 45 15 20 11 44

Upvotes: 1

Related Questions