Calculation component

Share:

Description

One of the components which can be added to a sim_setup. These functions can be used for adding new variables to the data.

Usage

1
2
3
4
5
sim_comp_pop(simSetup, fun = comp_var(), by = "")

sim_comp_sample(simSetup, fun = comp_var(), by = "")

sim_comp_agg(simSetup, fun = comp_var(), by = "")

Arguments

simSetup

a sim_setup.

fun

a function, see details.

by

names of variables as character; identifying groups for which fun is applied.

Details

Potentially you can define a function for computation yourself. Take care that it only has one argument, named dat, and returns a data.frame. Use comp_var for simple data manipulation. Functions added with sim_comp_pop are applied before sampling; sim_comp_sample after sampling. Functions added with sim_comp_agg after aggregation.

See Also

comp_var, sim_gen, sim_agg, sim_sample, sim_comp_N, sim_comp_n, sim_comp_popMean, sim_comp_popVar

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# Standard behavior
sim_base() %>% sim_gen_x() %>% sim_comp_N()

# Custom data modifications
## Add predicted values of a linear model
library(saeSim)

comp_lm <- function(dat) {
  dat$linearPredictor <- predict(lm(y ~ x, data = dat))
  dat
}

sim_base_lm() %>% sim_comp_pop(comp_lm)

# or if applied after sampling
sim_base_lm() %>% sim_sample() %>% sim_comp_pop(comp_lm)