Useful hack to visualizing intermediate steps

One thing I really liked from the Dorothy Bishop guide is that she keeps a running df1 to help visualize the process. I find this super useful for both beginners and as a comprehension check for code.

For those unfamiliar, basically what she does is something like:

population <- declare_population(
  N    = N,
  u_t1 = rnorm(N)*sd_1,
  u_t2 = rnorm(N, rho * u_t1, sqrt(1 - rho^2))*sd_2

df1 <- population()


potential_outcomes <- declare_potential_outcomes(
  Y_t1_Z_0 = u_t1,
  Y_t1_Z_1 = u_t1,
  Y_t2_Z_0 = u_t2,
  Y_t2_Z_1 = u_t2 + ate)

df1 <- potential_outcomes(df1)

… and so forth keeping a running df1 the whole way through.

I wanted to do something similar with the %>% but I struggled until I found out about Immediately-Invoked Function Expressions.

Here I write a the function:

prep_population <- function(population){

Which I can then use as an intermediary into visualizing my working design Dorothy Bishop style e.g.,

population %>%
  prep_population %>%

This serves the same purpose as keeping the running df1, but I find this easier to write. Anyway this has been really useful for me, hopefully someone else finds it to be useful too!


I often do something similar, like this:

If you’ve got declarations like:

population <- declare_population(N = 100, u = rnorm(N))
potential_outcomes <- declare_potential_outcomes(Y_Z_0 = 0,
                                                 Y_Z_1 = 1 + u)
assignment <- declare_assignment(m = 50)

You can do:

population() %>%
  potential_outcomes %>%
  assignment %>%

the population() call works the same way as your prep_population approach!

thanks for posting – using declarations in pipelines is awesome!

1 Like