DeclareDesign Community

Generating new variable conditional on values of others


#1

I’m wondering how I can generate a variable that is equal to some value when other variables are equal to specific values. For example, take the panel data generating process below:

panels <- fabricate(
  countries = add_level(N = 150, country_fe = runif(N, 1, 10)),
  years = add_level(N = 25, year_shock = runif(N, 1, 10), nest = FALSE),
  obs = cross_levels(
    by = join(countries, years),
    tr = rbinom(N,1,0.5),
    new_variable = country_fe + year_shock + rnorm(N, 0, 2)
  )
)

I want to generate a variable “Post” that is equal to 1 when tr == 1 and when years == 25, but is zero otherwise. I can do this after the fabricate call but I was wondering if there is a way to do that within the fabricate call. Thanks.


#2

fabricate creates variables in order, so you can uses the tr and years variables after they are created. Note that years is an id variable, so it’s character. This ought to work:

panels <- fabricate(
  countries = add_level(N = 150, country_fe = runif(N, 1, 10)),
  years = add_level(N = 25, year_shock = runif(N, 1, 10), nest = FALSE),
  obs = cross_levels(
    by = join(countries, years),
    tr = rbinom(N,1,0.5),
    new_variable = country_fe + year_shock + rnorm(N, 0, 2),
    Post = ifelse(tr==1 & years == '25', 1, 0)
  )
)

#3

Thanks Neal

I had one follow-up related to the panel data setting. How would I generate a time-invariant variable that is at the country-year level? An example would be:

panels <- fabricate(
  countries = add_level(N = 150, country_fe = runif(N, 1, 10)),
  years = add_level(N = 25, year_shock = runif(N, 1, 10), nest = FALSE),
  obs = cross_levels(
    by = join(countries, years),
    tr = rbinom(N,1,0.5),
    Ui = runif(N,1,10)
    new_variable = country_fe + year_shock + rnorm(N, 0, 2),
    Post = ifelse(tr==1 & years == '25', 1, 0)
  )
)

Where I want Ui to be generated according to some distribution at years = 1 and maintain that value throughout the time series. Thanks.

EDIT:
I actually managed to solve this by doing the following:

panels <- fabricate(
  countries = add_level(N = 150, country_fe = runif(N, 1, 10)),
  years = add_level(N = 25, year_shock = runif(N, 1, 10), nest = FALSE),
  obs = cross_levels(
    by = join(countries, years),
    tr = rbinom(N,1,0.5),
    Ui = runif(N,1,10),
    Ui = rep(Ui[1:150],25),
    new_variable = country_fe + year_shock*Ui + rnorm(N, 0, 2),
    Post = ifelse(tr==1 & years == '25', 1, 0)
  )
)

Let me know if there is a better way to go about this!