Within-subjects design plus interaction effect

Hi everyone. I need some help with the Declare Design code. I am trying to investigate a within-subjects experimental design with two treatments (T1 and T2). Covariate W determines whether participants respond to T2 or not. I am running an interaction effect between Z and W. I have tried various solutions, but none of them seem to work. This is my latest try:

nrespondents <- 100
bZ <- 0    # A parameter to model the difference between t1 and t2
bW <- 0
bWZ <- 0.5

within_subjects <- 
  declare_model(respondent_treatment = add_level(N = 2, Z = c(0,1)),
                                      respondents = add_level(N = nrespondents, u=rnorm(N), W=draw_binary(prob=.5,N=nrespondents), nest = FALSE),
                                      observations = cross_levels(
                                        by = join(Z,respondents),
                                        Y = bZ*Z + bW*W + bWZ*W*Z + u
                                      )) + 
  declare_inquiry(ate = mean(Y_Z_1 - Y_Z_0), interaction=bWZ) +     # we are interested in the effect of Z on Y1
  declare_estimator(Y ~ Z, model = lm_robust, cluster = respondents, label = "within") + 
  declare_estimator(Y ~ Z*W, model = lm_robust, cluster = respondents, label = "with interaction", inquiry = c("ate", "interaction"))

If I diagnose this design i get this error: Error: Error in step 2 (inquiry):
Error in mean(Y_Z_1 - Y_Z_0): object ‘Y_Z_1’ not found

I appreciate any feedback on this.

Your design is not creating the potential outcome variables Y_Z_0 and Y_Z_1 as columns, instead you only generate assignments Z=0 and Z=1 on different rows. The inquiry step should run fine if you modify the design to create the potential outcomes ( in “wide” format).

Thanks for your quick answer. I see how this works, but i don’t really see the logic of this. To do a linear regression on within-subject data you would always need to have the data in a long format, right? I have seen this within-subjects design on the website: Pointers on a within subjects design. Here the long format is essentially created in the declare_estimator part. This example is easy to implement of course, but I am not sure how to do it including an interaction effect. But wouldn’t it be easier to simply change the declare_inquiry command here, but to what?

Thanks for your time. I really love declaredesign.