# Comparing time series data with multiple pairs of time series, or difference-in-difference with continuous treatment conditions?

My dataset contains time-series for two variables ($$X$$ and $$Y$$) from 2017 to 2020, for each of many different countries. Each country has its own time series for each variable (X_usa, X_india, Y_usa, Y_india, etc). My goal is to determine the general relationship between $$X$$ and $$Y$$.

The difficulty is that I don't know a good method to account for both country-effects, and time-effects. I could do a standard regression analysis, where each observation is a country's mean (over all times) values of $$X$$ and $$Y$$. I could also do a Granger Causality Test, using just two time-series, where each date shows the mean of all the countries' $$X$$ and $$Y$$ values.

The goal is to integrate information from BOTH dimensions. Standard regression, of course, fails to account for temporal effects present in time-series data (see, for instance, the start of this talk). Meanwhile, taking the mean across countries would erase "spikes" or temporal patterns present in just some of the countries in the dataset.

Is there a way to extend statistical tests for time-series (like Granger Causality) to multiple pairs of time-series data?

Or, similarly, is there a way to use difference-in-difference (i.e., countries are treated at different points in time), but with a continuous set of measurements over time for each country, rather than just before/after, and with a continuous treatment variable? (I could, if needed, transform the "treatment," which is the amount of $$X$$, into a single variable for each country, rather than a time-series).

• Welcome! So you observe a subset of countries over a four year period, correct? Do you observe all countries before and after your treatment? That is, does each country have a precise pre-treatment and post-treatment period? Commented Dec 29, 2020 at 1:30
• Thanks! The time-series X is the "treatment" so the quantity of treatment varies over time. So strictly speaking, no, but I can define a threshold: once the total/historical quantity of X_c reaches some amount, consider country c "treated". I could also "bucketize" treatment values - so that each country is in one of several possible treatment "states" (e.g. low/medium/high) at any given time. However, not all countries will reach the threshold at all - so some countries won't see a post-treatment period at all, or would be "low" for the entire time period. Commented Dec 29, 2020 at 1:34
• Why won’t they see a post-treatment period? I would assume you’re observing outcomes on all your entities in all time periods, regardless of whether they meet your predefined threshold or not, right? Commented Dec 29, 2020 at 1:39
• Oh yes, that's correct. I have data on the whole time series, start to end, for every country. My point was just that some countries never get treated at all. They go from 2017 to 2020, with the time series for X remaining 0 or near-0 for the entire time. Sorry for the confusion! Commented Dec 29, 2020 at 1:41
• Yes, that's right. The increase in intensity is, generally speaking, continuous/"smooth", but it gets ramped up pretty rapidly midway through the panel, for treated entities. Commented Dec 29, 2020 at 4:12

I will try and tackle your concerns piecemeal and hopefully you can find a model that suits your needs.

The difficulty is that I don't know a good method to account for both country-effects, and time-effects.

Unless I am misinterpreting the way your data is structured, you should be able to implement a model with both unit and time fixed effects. To state this in language specific to your study, your model would be including country and day fixed effects. In your setting, you observe 220 countries over 4 years ($$T\approx 1,460$$ days), for a near 321,200 country-day ($$N \times T$$) observations. Admittedly, this is a lot of dummies to sort through in your output. Standard software packages offer tools to help suppress the unit and time effects. You could also estimate your model using the within-transformation. I am partial to the latter given the large number of country and day effects.

I could do a standard regression analysis, where each observation is a country's mean (over all times) values of $$X$$ and $$Y$$.

Perhaps.

Averaging all countries over their days $$T$$ to obtain a "country mean" and estimating a standard linear model on the averaged data is a "between-effects" model. You could certainty use this estimator, but you're limiting yourself. You can no longer exploit the longitudinal variation.

I could also do a Granger Causality Test, using just two time-series, where each date shows the mean of all the countries' $$X$$ and $$Y$$ values.

You could easily implement a procedure for testing Granger causality in panel datasets. You do not need to take the average over time $$T$$ to do this.

The goal is to integrate information from BOTH dimensions. Standard regression, of course, fails to account for temporal effects present in time-series data.

It is unclear what you mean by "standard regression" in this statement. I have yet to view the video you referenced so I am not sure what someone else has said about this, but I find no valid reason why you cannot estimate a model with time effects.

Meanwhile, taking the mean across countries would erase "spikes" or temporal patterns present in just some of the countries in the dataset.

Correct.

It isn't necessary, though. If you want to estimate this model, go for it. Just be mindful of what variation you're trying to exploit.

Is there a way to extend statistical tests for time-series (like Granger Causality) to multiple pairs of time-series data?

Sure.

But why can't it work with your dataset as it is currently structured? I assume your dataset is a country-day panel with observations "stacked" on top of each other. For example, you should have separate columns for country and time. By "stacked" I mean the following:

$$\begin{array}{ccc} country & time & x & y \\ \hline India & 1 & 0 & .47 \\ India & 2 & 0 & .93 \\ India & 3 & 0 & .04 \\ India & 4 & 0 & .47 \\ India & 5 & 0 & .21 \\ \vdots & \vdots & \vdots & \vdots \\ India & 1,459 & 0 & .19 \\ India & 1,460 & 0 & .67 \\ \hline USA & 1 & 0 & .12 \\ USA & 2 & 0 & .51 \\ USA & 3 & 0 & .09 \\ USA & 4 & 0 & .75 \\ USA & 5 & 0 & .13 \\ \vdots & \vdots & \vdots & \vdots \\ USA & 1,459 & .41 & .95 \\ USA & 1,460 & .73 & .81 \\ \end{array}$$

The column $$time$$ denotes your days. To appropriately include time effects, you must instantiate a variable to capture all days a country is observed. It models the common shocks to all entities in every time period. It should not represent a "repeating" temporal interval, such as days-per-month (e.g., 1-30) or days-per-year (e.g., 1-365). It should distinguish between day 365 and 366, hence why day 1,460 is available for each country. It might be easier to concatenate year and day together or simply year-month-day (e.g., 2017-01-01). However you do it, you must ensure your time variable is coded in a way that it captures a separate effect for all time periods in your panel. In R, you could use as.factor(time) which 'dummies out' the time effects for you. However, this amounts to the estimation of 1,459 day effects. This could become unwieldy in practice as $$T$$ grows. I would look into more efficient ways of estimating your fixed effects. I highly recommend the plm package in R.

Suppose you aggregated your data up to weekly time units. Including time effects amounts to the inclusion of 207 separate week effects. Now suppose you aggregated your data up to monthly time units. Including time effects amounts to the inclusion of 47 separate month effects. And finally, suppose you aggregated your data up to yearly time units. Including time effects amounts to the inclusion of 3 separate year effects. The purpose of me outlining this is to show you how we include time fixed effects in practice.

I can't recommend an appropriate time unit without knowing more about how, or when, the intervention affects specific countries. You must take into consideration the timing of your treatment. Suppose you're investigating some exposure which impacts a subset of countries in June of 2018, only to end at the conclusion of the third quarter in 2019. In this setting, I wouldn't recommend observing countries in yearly time intervals. Instead, observing outcomes over months or quarters will help capture the precise exposure period.

Or, similarly, is there a way to use difference-in-difference (i.e., countries are treated at different points in time), but with a continuous set of measurements over time for each country, rather than just before/after, and with a continuous treatment variable?

Yes.

Difference-in-differences (DiD) is amenable to continuous treatments. I actually addressed a similar question here. In particular, I showed how continuous treatments can be used in the classical case and in the more generalized setting where the onset of treatment exposure is staggered across treated entities. I also reference a very popular answer here which should also be of interest to you.

In your setting, you're well outside the realm of the classical DiD approach. Different countries enter into treatment at different times. You must proceed with the generalized DiD estimator. You aren't beholden to me or my suggestions, but if you know the precise onset of treatment for all countries, you could investigate the causal effect of the exposure both ways. Here is the generalized DiD specification, which is also commonly referred to as a two-way fixed effects estimator:

$$y_{ct} = \alpha_{c} + \lambda_{t} + \delta D_{ct} + \theta X_{ct} + \epsilon_{ct}$$

where $$\alpha_{c}$$ and $$\lambda_{t}$$ denote fixed effects for countries $$c$$ and days $$t$$ (or weeks, months, quarters), respectively. If you want this to be a DiD estimator, you must include country (unit) and day (time) fixed effects. $$D_{ct}$$ is a dichotomous treatment variable. It equals 1 for treated countries and only when they enter into a posttreatment period, 0 otherwise. Any country never exposed to treatment is coded 0 for the entire observation period. For example, suppose India is a "control" country. If $$x$$ represented our dichotomous treatment variable, it would be equal 0 in all 1,460 days. To be clear, $$D_{ct}$$ is not delineating a precise treatment group. Rather, it is 'turning on' (i.e., shifting from 0 to 1) for countries exposed to the treatment and only during the days when treatment is actually in effect. $$\delta$$ is your causal estimand.

Your equation is also amendable to a continuous exposure variable. The equation still takes the following form:

$$y_{ct} = \alpha_{c} + \lambda_{t} + \delta M_{ct} + \theta X_{ct} + \epsilon_{ct}$$

where $$M_{ct}$$ is a measure of the "bite" or dosage of treatment in the days after treatment commences. To be clear, only the subset of treated countries should experience a 'jump' in intensity. Thus, incorporating $$M_{ct}$$ means replacing any country-day combination equal to unity with the precise dosage observed in that country. Again, the variable $$M_{ct}$$ should reflect reality as closely as possible. In the pretreatment epoch, the variable should reflect the absence of intensity. In fact, all 220 countries would be considered "untreated" before the onset of treatment. But once a country enters into their posttreatment epoch, $$M_{ct}$$ should indicate some measure of intensity. In my fake data frame from earlier, the United States is a treated country. The variable $$x$$ takes on positive values at some day $$t$$ in between day 5 and day 1,460. You can think of $$x$$ as your measure of 'bite' or dosage. I simulated some positive values for $$x$$ but it could represent almost any dosage of interest: exposure to particulate matter, the proportion of immigrants in a school, or even the saturation of bike patrol officers within a precinct. Again, you must justify to a reader why your continuous treatment is a close approximation of what is going on in the real world.

A related question is whether country and time fixed effects should kind of "intersect." My guess is that the time effects are somewhat different in each country.

This quote was pulled from our discussion in the comments. It appears you want to estimate the following:

$$y_{ct} = \alpha_{c} + \lambda_{t} + \gamma_{ct} + \delta M_{ct} + \theta X_{ct} + \epsilon_{ct}$$

where $$\gamma_{ct}$$ denotes a "country-day" effect. In essence, you're saying the shock is likely unique in a particular country and day. For example, the shock in India in the first time period is different than the shock in the United States in the first time period. But that is more country-day effects than you can afford. Your model will balk, claiming you have no residual degrees of freedom. Even if you dropped the country and/or day fixed effects, your model is inestimable. A country-by-day effect would work well in settings where you observe, for example, individuals $$i$$ nested within your countries $$c$$. But you don't have that here!

You can estimate geographic shocks particular to specific regions of the world. For example, you might suspect a shock specific to the subset of countries nested within the Balkan Peninsula. You could create a variable indexing the regions where you suspect shocks and interact those with day (time) dummies. But don't overdue it. You should outline some theoretical basis for doing this. In my opinion, I would estimate such a model as a robustness check. The purpose is to see whether your effects hold even after adjusting for the regional shocks.