Description Usage Arguments Details Value Examples
View source: R/utils_simstudy.R
Inject pre-existing data into a defData from simstudy.
1 | injectData(dtDefs, dtData)
|
dtDefs |
|
dtData |
data.frame or data.table to inject. |
Sometimes we need to inject data into a defData from simstudy.
This is particularly true when using correlated data which cannot be
manipulated with defData unless the double notation .. in
formula. This function is particularly useful when using correlated
data and avoid the tricks required when working with such data.
data with a defData together.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | # create data definition with ..x notation
defs <- simstudy::defData(varname = "x0", dist = "normal",
formula = 0, variance = 1)
defs <- simstudy::defData(defs, varname = "x1", dist = "nonrandom",
formula = "..x1")
defs <- simstudy::defData(defs, varname = "x2", dist = "nonrandom",
formula = "..x2")
defs <- simstudy::defData(defs, varname = "x3", dist = "nonrandom",
formula = "..x3")
defs <- simstudy::defData(defs, varname = "mu", dist = "nonrandom",
formula = "x0 + x1 + x2 + x3")
defs <- simstudy::defData(defs, varname = "sigma", dist = "exponential",
formula = 1)
defs <- simstudy::defData(defs, varname = "y", dist = "normal",
formula = "mu", variance = "sigma^2")
# correlation matrix used to generate correlated data fro x1, x2 and x3
Rho <- simstudy::genCorMat(nvars = 3, cors = 1:3 * 0.25)
# generate correlated data for x1, x2 and x3
dtCor <- simstudy::genCorData(n = 100L, mu = 0:2, sigma = 1:3 * 0.5,
corMatrix = Rho, cnames = c("x1", "x2", "x3"))
# inject data into the definitions
out <- injectData(dtDefs = defs, dtData = dtCor)
stopifnot(identical(names(out), c("id", defs$varname)))
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.