I've got a dataset similar to this:

pat_id   epis Care Type
1       1722650 Acute Care
1       1723120 Rehabilitation care
2       1584309 Acute Care
2       1585705 Rehabilitation care
3       1726487 GEM
3       1664031 Acute Care
3       1726488 Acute Care
3       1726489 Rehabilitation care

Each patient has multiple "episodes/care types". I want to sample 50 patients but also approximately proportional to the "Care Type" from the population (say, 50% acute care, 30% rehabilitation care, 20% GEM).

One way I thought of doing is to split the data set into say "acute", "rehab" and "gem" then sample 25 from "acute", 15 from "rehab" and 10 from "gem". But there would be an overlap using this approach "e.g. Patient 1375 would be in gem, acute and rehab.

Is there a R package that would handle this sort of sampling easily?


Yeah, the sampling package handles this, you can do cluster sampling or stratified or a few others: http://cran.r-project.org/web/packages/sampling/sampling.pdf

It can then also handle a lot of the special variance estimation techniques you'll have to do for any metric you calculate from the complex design. However, I prefer Lumley's survey package for that.


I think no extra package is needed for the task, just use the basic sample function, e.g.:

Get sample from the first group:

sample <- sample(data[data$"Care Type" == "Acute Care",], size = 25)

Get the choosen IDs out of the orig. dataset (making a backup could be a good idea before that):

data <- data[setdiff(data$pat_id, sample_pat_id),]

Get sample from second group in the rest of the dataset and concatenate to sample:

sample <- rbind(sample, sample(data[(data$"Care Type" == "Acute Care"),], size = 25)

Repeat for each segment:

data <- data[setdiff(data$pat_id, sample_pat_id),]
sample <- rbind(sample, sample(data[(data$"Care Type" == "?"),], size = ?)

Sorry, not tested, but I think the point can be seen. And also: I am sure the above code could be improved and minified.


What I would do is provide prob argument with weights for each data point based on number of levels in your variable. Example:

df <- data.frame(oks = sample(100),
        grp = c(rep("trt1", times = 30), rep("trt2", times = 70)))

> head(df)
  oks  grp
1  40 trt1
2  29 trt1
3  12 trt1
4  25 trt1
5  19 trt1
6  45 trt1


> (df.prob <- table(df$grp))

trt1 trt2 
  30   70 

You pass a vector of probabilities to sample. You can sort your data.frame by your desired variable (and use the adaptation of solution provided here), or you could assign weights to individual rows based on the level of the treatment (not presented here, but shouldn't be too hard to recode).

df[sample(x = df$oks, size = 30, prob = rep(df.prob/nrow(df), df.prob)), ] # / by nrow(df) to get appropriate weight per treatment

This is the approximate ratio you're looking for, right?

> table(df[sample(x = df$oks, size = 30, prob = rep(df.prob/nrow(df), df.prob)), ]$grp)

trt1 trt2 
  12   18 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy