View source: R/distributions.r
| rsample | R Documentation |
This function is a simple wrapper around the sample function, allowing users to directly sample values from a given input vector (with or without replacement and with or without defining selection probabilities) or data.frame like object.
rsample(n, x, replace=FALSE, prob=NULL)
n |
How many draws to make. |
x |
A vector containing one or more elements from which to sample from, or a |
replace |
Either |
prob |
A numeric vector of probability weights for obtaining the elements of the vector being sampled or |
This function is very similar to the rcategorical function, with the main difference being that rsample() directly supports any kind of vector input, not just a few categorical values, but it does not support matrix input in the prob argument. Use rcategorical if the goal is to sample from a categorical distribution with few categories or different probabilities per person and use rsample() for general sampling purposes.
Note that this function is just a wrapper around the sample function, with the only additional functionality being that it also may be used to directly sample from data.frames. It is only meant to conveniently allow sampling within the packages syntax (the original function does not use the n argument, and can thus not be used directly without a wrapper).
Returns a vector of length n with the same type as x if x is a vector and a data.frame with n rows if x is a data.frame.
Robin Denz
library(simDAG)
# without replacement
dag <- empty_dag() +
node("A", type="rsample", x=1:10, replace=FALSE)
data <- sim_from_dag(dag, n_sim=5)
head(data)
# with replacement and selection probabilities
dag <- empty_dag() +
node("X", type="rbernoulli", p=0.5) +
node("A", type="rsample", x=c(1, 2, 3, 4), replace=TRUE,
prob=c(0.1, 0.3, 0.1, 0.5))
data <- sim_from_dag(dag, n_sim=100)
head(data)
# sampling rows from a data.frame object
# NOTE: The node name for the rsample() node will be ignored, because
# a data.frame is supplied to "x". The names of the variables in the
# data are used directly instead.
dag <- empty_dag() +
node("placeholder", type="rsample", x=data) +
node("Y", type="binomial", formula= ~ -2 + A*0.5 + X*-1)
data2 <- sim_from_dag(dag, n_sim=50)
head(data2)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.