SampleControl-class {simFrame} | R Documentation |
Class "SampleControl"
Description
Class for controlling the setup of samples.
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 isFALSE
(which is the default value), groups are sampled directly. If grouping variable is specified and this isTRUE
, 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 tosrs
). 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 tofun
.k
:Object of class
"numeric"
; a single positive integer giving the number of samples to be set up.
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
.
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 slotdesign
.setDesign
signature(x = "SampleControl")
: set slotdesign
.getGrouping
signature(x = "SampleControl")
: get slotgrouping
.setGrouping
signature(x = "SampleControl")
: set slotgrouping
.getCollect
signature(x = "SampleControl")
: get slotcollect
.setCollect
signature(x = "SampleControl")
: set slotcollect
.getFun
signature(x = "SampleControl")
: get slotfun
.setFun
signature(x = "SampleControl")
: set slotfun
.getSize
signature(x = "SampleControl")
: get slotsize
.setSize
signature(x = "SampleControl")
: set slotsize
.getProb
signature(x = "SampleControl")
: get slotprob
.setProb
signature(x = "SampleControl")
: set slotprob
.getDots
signature(x = "SampleControl")
: get slotdots
.setDots
signature(x = "SampleControl")
: set slotdots
.
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.
Note
The slots grouping
and fun
were named group
and
method
, respectively, prior to version 0.2. Renaming the slots was
necessary since accessor and mutator functions were introduced in this
version and functions named getGroup
, getMethod
and
setMethod
already exist.
Author(s)
Andreas Alfons
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. doi: 10.18637/jss.v037.i03.
See Also
"VirtualSampleControl"
,
"TwoStageControl"
, "SampleSetup"
,
setup
, draw
Examples
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)