Learn R Programming

simFrame (version 0.5.4)

SampleControl-class: Class "SampleControl"

Description

Class for controlling the setup of samples.

Arguments

Objects from the Class

Objects can be created by calls of the form new("SampleControl", …) or SampleControl(…).

Slots

design:

Object of class "BasicVector" specifying variables (columns) to be used for stratified sampling.

grouping:

Object of class "BasicVector" specifying a grouping variable (column) to be used for sampling whole groups rather than individual observations.

collect:

Object of class "logical"; if a grouping variable is specified and this is FALSE (which is the default value), groups are sampled directly. If grouping variable is specified and this is TRUE, individuals are sampled in a first step. In a second step, all individuals that belong to the same group as any of the sampled individuals are collected and added to the sample. If no grouping variable is specified, this is ignored.

fun:

Object of class "function" to be used for sampling (defaults to srs). It should return a vector containing the indices of the sampled items (observations or groups).

size:

Object of class "OptNumeric"; an optional non-negative integer giving the number of items (observations or groups) to sample. In case of stratified sampling, a vector of non-negative integers, each giving the number of items to sample from the corresponding stratum, may be supplied.

prob:

Object of class "OptBasicVector"; an optional numeric vector giving the probability weights, or a character string or logical vector specifying a variable (column) that contains the probability weights.

dots:

Object of class "list" containing additional arguments to be passed to fun.

k:

Object of class "numeric"; a single positive integer giving the number of samples to be set up.

Extends

Class "'>VirtualSampleControl", directly. Class "'>OptSampleControl", by class "VirtualSampleControl", distance 2.

Accessor and mutator methods

In addition to the accessor and mutator methods for the slots inherited from "'>VirtualSampleControl", the following are available:

getDesign

signature(x = "SampleControl"): get slot design.

setDesign

signature(x = "SampleControl"): set slot design.

getGrouping

signature(x = "SampleControl"): get slot grouping.

setGrouping

signature(x = "SampleControl"): set slot grouping.

getCollect

signature(x = "SampleControl"): get slot collect.

setCollect

signature(x = "SampleControl"): set slot collect.

getFun

signature(x = "SampleControl"): get slot fun.

setFun

signature(x = "SampleControl"): set slot fun.

getSize

signature(x = "SampleControl"): get slot size.

setSize

signature(x = "SampleControl"): set slot size.

getProb

signature(x = "SampleControl"): get slot prob.

setProb

signature(x = "SampleControl"): set slot prob.

getDots

signature(x = "SampleControl"): get slot dots.

setDots

signature(x = "SampleControl"): set slot dots.

Methods

In addition to the methods inherited from "'>VirtualSampleControl", the following are available:

clusterSetup

signature(cl = "ANY", x = "data.frame", control = "SampleControl"): set up multiple samples on a cluster.

setup

signature(x = "data.frame", control = "SampleControl"): set up multiple samples.

show

signature(object = "SampleControl"): print the object on the R console.

UML class diagram

A slightly simplified UML class diagram of the framework can be found in Figure 1 of the package vignette An Object-Oriented Framework for Statistical Simulation: The R Package simFrame. Use vignette("simFrame-intro") to view this vignette.

Details

There are some restrictions on the argument names of the function supplied to fun. If it needs population data as input, the corresponding argument should be called x and should expect a data.frame. If the sampling method only needs the population size as input, the argument should be called N. Note that fun is not expected to have both x and N as arguments, and that the latter is much faster for stratified sampling or group sampling. Furthermore, if the function has arguments for sample size and probability weights, they should be called size and prob, respectively. Note that a function with prob as its only argument is perfectly valid (for probability proportional to size sampling). Further arguments of fun may be supplied as a list via the slot dots.

References

Alfons, A., Templ, M. and Filzmoser, P. (2010) An Object-Oriented Framework for Statistical Simulation: The R Package simFrame. Journal of Statistical Software, 37(3), 1--36. 10.18637/jss.v037.i03.

See Also

"'>VirtualSampleControl", "'>TwoStageControl", "'>SampleSetup", setup, draw

Examples

Run this code
# NOT RUN {
data(eusilcP)

## simple random sampling
srsc <- SampleControl(size = 20)
draw(eusilcP[, c("id", "eqIncome")], srsc)

## group sampling
gsc <- SampleControl(grouping = "hid", size = 10)
draw(eusilcP[, c("hid", "hid", "eqIncome")], gsc)

## stratified simple random sampling
ssrsc <- SampleControl(design = "region",
    size = c(2, 5, 5, 3, 4, 5, 3, 5, 2))
draw(eusilcP[, c("id", "region", "eqIncome")], ssrsc)

## stratified group sampling
sgsc <- SampleControl(design = "region", grouping = "hid",
    size = c(2, 5, 5, 3, 4, 5, 3, 5, 2))
draw(eusilcP[, c("hid", "id", "region", "eqIncome")], sgsc)
# }

Run the code above in your browser using DataLab