## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
set.seed(343)

## ----setup--------------------------------------------------------------------
library(fabricatr)

## -----------------------------------------------------------------------------
survey <- fabricate(
  N = 100,
  age = round(runif(N, 18, 80)),
  income = 20 + 0.5 * age + rnorm(N, sd = 10)
)
head(survey)

## -----------------------------------------------------------------------------
nested <- fabricate(
  villages = add_level(N = 5, village_wealth = rnorm(N)),
  citizens = nest_level(N = 4, income = village_wealth + rnorm(N))
)
head(nested)

## -----------------------------------------------------------------------------
experiment <- fabricate(
  N = 100,
  U = rnorm(N),
  potential_outcomes(Y ~ 0.5 * Z + U)
)
head(experiment)

## -----------------------------------------------------------------------------
observed <- fabricate(
  data = experiment,
  Z = rbinom(N, 1, 0.5),
  Y = reveal_outcomes(Y ~ Z)
)
head(observed)

## -----------------------------------------------------------------------------
fabricate(
  N = 6,
  x = rnorm(N),
  voted = draw_binary(latent = x, link = "logit")
)

## -----------------------------------------------------------------------------
clustered <- fabricate(
  villages = add_level(N = 100),
  citizens = nest_level(N = 20, trust = draw_normal_icc(clusters = villages, ICC = 0.3))
)
icc_share <- summary(lm(trust ~ villages, data = clustered))$adj.r.squared
icc_share

## -----------------------------------------------------------------------------
panel <- fabricate(
  units = declare_level(N = 2000),
  periods = declare_level(N = 6),
  obs = cross_levels(
    .by = c("units", "periods"),
    shock = draw_normal_ar(clusters = units, time = periods, rho = 0.7)
  )
)
head(panel)

## -----------------------------------------------------------------------------
wide <- tapply(panel$shock, list(panel$units, panel$periods), sum)
round(cor(wide)[1, ], 2)

## -----------------------------------------------------------------------------
boot <- resample_data(nested, N = c(villages = 3, citizens = 2))
boot

## -----------------------------------------------------------------------------
resample_data(nested, N = c(villages = 3, citizens = ALL))

