fabricatr

This function allows the user to create data structures that are paneled or cross-classified: where one level of observation draws simultaneously from two or many source levels. Common examples of panels include country-year data which have country-level and year-level characteristics.

cross_levels(by = NULL, ...)

link_levels(N = NULL, by = NULL, ...)

Arguments

by

The result of a call to join() which specifies how the cross-classified data will be created

...

A variable or series of variables to add to the resulting data frame after the cross-classified data is created.

N

The number of observations in the resulting data frame. If N is NULL or not provided, the join will be an "outer product" -- merging each row of each provided data frame with each other data frame to make a full panel.

Value

data.frame

Details

By specifying the appropriate arguments in join() within the function call, it is possible to induce correlation in cross-classified data.

Examples

# Generate full panel data panel <- fabricate( countries = add_level(N = 20, country_shock = runif(N, 1, 10)), years = add_level(N = 20, year_shock = runif(N, 1, 10), nest=FALSE), obs = cross_levels(by=join(countries, years), GDP_it = country_shock + year_shock) ) # Include an "N" argument to allow for cross-classified # data. students <- fabricate( primary_school = add_level(N = 20, ps_quality = runif(N, 1, 10)), secondary_school = add_level(N = 15, ss_quality = runif(N, 1, 10), nest=FALSE), students = link_levels(N = 500, by = join(primary_school, secondary_school)) ) head(students)
#> primary_school ps_quality secondary_school ss_quality students #> 1 11 1.629510 14 5.847698 001 #> 2 11 1.629510 07 4.895093 002 #> 3 05 9.372101 05 8.978955 003 #> 4 17 6.766124 05 8.978955 004 #> 5 19 7.692592 14 5.847698 005 #> 6 09 9.849402 15 2.161146 006
# Induce a correlation structure in cross-classified data by providing # rho. students <- fabricate( primary_school = add_level(N = 20, ps_quality = runif(N, 1, 10)), secondary_school = add_level(N = 15, ss_quality = runif(N, 1, 10), nest=FALSE), students = link_levels(N = 500, by = join(ps_quality, ss_quality, rho = 0.5)) )
#> `link_levels()` calls are faster if the `mvnfast` package is installed.
cor(students$ps_quality, students$ss_quality)
#> [1] 0.4404302