Difference-in-differences analysis in R I have two groups, a treatment group and a comparison group.
Each group is measured on a variable 'A' 6 times before the treatment was given, and 6 times after.
I read that using a difference-in-difference analysis would be the way to analyze this data, as it's longitudinal data without a proper randomized control trial.
But DID is typically done with just one value before and after treatment - in this case, do I sum up all 6 values into one, to be the value before treatment?
I know how to calculate the DID estimator by hand, but how do I figure out its significance in R (I believe it's supposed to be done using regression)?
ie. is this correct?
lm(value ~ group + pre_post_flag + group*pre_post_flag, data=df)

 A: The DID analysis measures the average treatment effect, by specifically looking at the magnitude of the change in the treatment groups versus the magnitude of the change in the control groups. So, if you have 6 observations from each group for before and after, then it is completely fine to use difference-in-difference as long as you hope to estimate the average treatment effect. Note that this implicitly assumes that all observations in the treatment group have the same treatment effect (and all of the observations in the control group have the same control effect), and the variance between these across observations is due to random error.
Your implementation looks correct if group is a binary variable showing whether an observation is a control or a treatment observation, and pre_post_flag is a binary variable indicating whether the observation was taken before the experiment or after. The DID variable you want to look at is the coefficient in front of the group*pre_post_flag, since that is equivalent to (where g = group and pp = pre_post_flag):
$$\beta_3 = \beta_3 + \beta_2 + \beta_1 + \beta_0 - \beta_2 - \beta_0 - \beta_1 - \beta_0 + \beta_0 = (y|g = 1, pp = 1) - (y|g = 0, pp = 1) - (y|g = 1, pp = 0) + (y|g = 0, pp = 0) = [(y|g = 1, pp = 1) - (y|g = 0, pp = 1)] - [(y|g = 1, pp = 0) - (y|g = 0, pp = 0)]$$
Additionally, the p-value of the $\beta_3$ coefficient from this regression shows the significance level of the difference. If the DID is close to zero (minimal treatment effect), then the results will be insignificant.
