fabricate helps you simulate a dataset before you collect it. You can either start with your own data and add simulated variables to it (by passing data to fabricate()) or start from scratch by defining N. Create hierarchical data with multiple levels of data such as citizens within cities within states using add_level() or modify existing hierarchical data using modify_level(). You can use any R function to create each variable. Use cross_levels() and link_levels() to make more complex designs such as panel or cross-classified data.

fabricate(..., data = NULL, N = NULL, ID_label = NULL)

add_level(N = NULL, ..., nest = TRUE)

modify_level(..., by = NULL)

nest_level(N = NULL, ...)

## Arguments

... Variable or level-generating arguments, such as my_var = rnorm(N). For fabricate, you may also pass add_level() or modify_level() arguments, which define a level of a multi-level dataset. See examples. (optional) user-provided data that forms the basis of the fabrication, e.g. you can add variables to existing data. Provide either N or data (N is the number of rows of the data if data is provided). If data and N are not provided, fabricatr will try to interpret the first un-named argument as either data or N based on type. (optional) number of units to draw. If provided as fabricate(N = 5), this determines the number of units in the single-level data. If provided in add_level, e.g. fabricate(cities = add_level(N = 5)), N determines the number of units in a specific level of a hierarchical dataset. (optional) variable name for ID variable, e.g. citizen_ID. Set to NA to suppress the creation of an ID variable. (Default TRUE) Boolean determining whether data in an add_level() call will be nested under the current working data frame or create a separate hierarchy of levels. See our vignette for cross-classified, non-nested data for details. (optional) quoted name of variable modify_level uses to split-modify-combine data by.

data.frame

## Details

We also provide several built-in options to easily create variables, including draw_binary, draw_count, draw_likert, and intra-cluster correlated variables draw_binary_icc and draw_normal_icc

link_levels

## Examples



# Draw a single-level dataset with a covariate
building_df <- fabricate(
N = 100,
height_ft = runif(N, 300, 800)
)
#> 1 001  544.2171
#> 2 002  659.9207
#> 3 003  737.1589
#> 4 004  730.2748
#> 5 005  622.8225
#> 6 006  682.1437
building_modified <- fabricate(
data = building_df,
rent = rnorm(N, mean = height_ft * 100, sd = height_ft * 30)
)

# Draw a two-level hierarchical dataset
# containing cities within regions
multi_level_df <- fabricate(
cities = add_level(N = 2, pollution = rnorm(N, mean = 5)))
#> 1       1     01  5.495848
#> 2       1     02  6.299751
#> 3       2     03  3.384014
#> 4       2     04  3.749632
#> 5       3     05  6.582132
#> 6       3     06  6.252529
company_df <- fabricate(
data = building_df,
)

# at levels which are already present in the existing data.
# Note: do not provide N when adding variables to an existing level
fabricate(
data = multi_level_df,
regions = modify_level(watershed = sample(c(0, 1), N, replace = TRUE)),
cities = modify_level(runoff = rnorm(N))
)#>    regions cities pollution watershed     runoff
#> 1        1     01  5.495848         0 -0.6614365
#> 2        1     02  6.299751         1  1.3385867
#> 3        2     03  3.384014         1 -1.1178179
#> 4        2     04  3.749632         0  0.6847335
#> 5        3     05  6.582132         1 -0.6105563
#> 6        3     06  6.252529         0 -0.3493068
#> 7        4     07  4.790604         1  1.1339693
#> 8        4     08  4.360966         1  1.5337566
#> 9        5     09  5.398903         1  1.1066693
#> 10       5     10  6.341974         1  0.3440617
# fabricatr can add variables that are higher-level summaries of lower-level
# variables via a split-modify-combine logic and the \code{by} argument

multi_level_df <-
fabricate(
regions = add_level(N = 5, elevation = rnorm(N)),
cities = add_level(N = 2, pollution = rnorm(N, mean = 5)),
cities = modify_level(by = "regions", regional_pollution = mean(pollution))
)

# fabricatr can also make panel or cross-classified data. For more
student_quality = ps_quality + 3*ss_quality + rnorm(N)))