Generate longitudinal data with staggered treatment

generateDidData(
  i,
  t,
  hdfe = TRUE,
  control = TRUE,
  attrition.rate = 0,
  treatment = ~d * (k + 1),
  ig = ~i%%2
)

Arguments

i

Numeric. Number of units

t

Numeric. Number of periods

hdfe

Logical. Should high dimensional fixed effect be added?

control

Logical. Should continuous control be added?

attrition.rate

Numerical. Default is 0. Probability of missing observation.

treatment

one sided formula, default is ~ d * (k + 1). See details.

ig

an optional one sided formula, default is ~ i %% 2. Will be evaluated to define a new variable which can be used in the treatment formula. By default, ig is ~ i %% 2, that is a random unit invariant dummy variable.

Value

a data.table with columns

i

Unit identifier.

t

Calendar time.

g

Time of treatment.

a

Individual fixed effect. ~N(0,1)

b

Time fixed effect. ~N(0,1)

x1

A continuous, normally distributed control. ~N(0,2)

hdfe

A random effect with {i}/50 modalities. ~ N(0,1)

e

Residual. ~ N(0,2)

hdfefactor

A fixed effect of size {i}/50.

k

Relative time to treatment.

d

Dummy variable defined as k >= 0

h

Randomly generated variable at the level of the cohort.

y

Outcome.

true_effect

True average treatment effect on the treated.

Treatment effect formula

By default the treatment effect is ~ d * (k + 1) where d is an internal dummy equal to one if observation is treated and k is the relative time to event. User can define its own formula to create complex treatment and test the model effectiveness.

User has access to the following hidden variables:

  • h: a cohort-wise randomly generated number

  • g: Cohort identifier

  • a: Individual fixed effect

  • b: Time fixed effect

  • t: Calendar time

  • ig: a random unit invariant value in 0, 1, 2. Can be used to define heterogeneous effects.

Default outcome

The default data generating process is:

y ~ a + b + hdfe + 2*x1 + d * (k + 1) + e

Variable description is available in previous section.

Examples

dt <- generateDidData(100,10)
head(dt)
#>   i t g         a          b        x1       hdfe         e hdfefactor  k d
#> 1 1 1 8 0.1721817 -0.3056207  2.123998 -2.1635740 -1.590280          1 -7 0
#> 2 1 2 8 0.1721817  2.2117695  2.285390 -0.7425911 -3.132516          2 -6 0
#> 3 1 3 8 0.1721817 -1.0416684  2.247678 -0.7425911  2.427465          2 -5 0
#> 4 1 4 8 0.1721817 -1.1465239 -0.794003 -0.7425911  3.007620          2 -4 0
#> 5 1 5 8 0.1721817 -1.6753273 -1.646522 -2.1635740 -2.338850          1 -3 0
#> 6 1 6 8 0.1721817  1.5259387 -1.157769 -0.7425911  1.226754          2 -2 0
#>           h          y ig trueffect
#> 1 0.5043136  0.3607031  1         0
#> 2 0.5043136  3.0796240  1         0
#> 3 0.5043136  5.3107426  1         0
#> 4 0.5043136 -0.2973190  1         0
#> 5 0.5043136 -9.2986142  1         0
#> 6 0.5043136 -0.1332549  1         0