cross_levels | R Documentation |
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, ...)
by |
The result of a call to |
... |
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 |
By specifying the appropriate arguments in join_using()
within the
function call, it is possible to induce correlation in cross-classified data.
data.frame
# 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_using(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_using(primary_school, secondary_school))
)
head(students)
# 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_using(ps_quality, ss_quality, rho = 0.5))
)
cor(students$ps_quality, students$ss_quality)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.