Git Product home page Git Product logo

pysynthdid's Introduction

pysynthdid : Synthetic difference in differences for Python

What is Synthetic difference in differences:

original paper:

Arkhangelsky, Dmitry, et al. Synthetic difference in differences. No. w25532. National Bureau of Economic Research, 2019. https://www.nber.org/papers/w25532

R pkg:

https://github.com/synth-inference/synthdid

Blog:

https://medium.com/@masa_asami/causal-inference-using-synthetic-difference-in-differences-with-python-5758e5a76909

Installation:

$ pip install git+https://github.com/MasaAsami/pysynthdid

This package is still under development. I plan to register with pypi after the following specifications are met.

  • Refactoring and better documentation
  • Completion of the TEST code

How to use:

Here's a simple example :

  • setup
from synthdid.model import SynthDID
from synthdid.sample_data import fetch_CaliforniaSmoking

df = fetch_CaliforniaSmoking()

PRE_TEREM = [1970, 1988]
POST_TEREM = [1989, 2000]

TREATMENT = ["California"]
  • estimation & plot
sdid = SynthDID(df, PRE_TEREM, POST_TEREM, TREATMENT)
sdid.fit(zeta_type="base")
sdid.plot(model="sdid")

  • Details of each method will be created later.

See the jupyter notebook for basic usage

  • ReproductionExperiment_CaliforniaSmoking.ipynb

    • This is a reproduction experiment note of the original paper, using a famous dataset (CaliforniaSmoking).
  • OtherOmegaEstimationMethods.ipynb

    • This note is a different take on the estimation method for parameter omega (& zeta ). As a result, it confirms the robustness of the estimation method in the original paper.
  • ScaleTesting_of_DonorPools.ipynb

    • In this note, we will check how the estimation results change with changes in the scale of the donor pool features.
    • Adding donor pools with extremely different scales (e.g., 10x) can have a significant impact (bias) on the estimates.
    • If different scales are mixed, as is usually the case in traditional regression, preprocessing such as logarithmic transformation is likely to be necessary

Discussions and PR:

  • This module is still under development.
  • If you have any questions or comments, please feel free to use issues.

pysynthdid's People

Contributors

masaasami avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

pysynthdid's Issues

Error when installing

`Collecting git+https://github.com/MasaAsami/pysynthdid
Cloning https://github.com/MasaAsami/pysynthdid to /tmp/pip-req-build-ey_7xzv7
Running command git clone --filter=blob:none --quiet https://github.com/MasaAsami/pysynthdid /tmp/pip-req-build-ey_7xzv7
Resolved https://github.com/MasaAsami/pysynthdid to commit 01afe33
Preparing metadata (setup.py) ... done
Requirement already satisfied: pandas in /usr/local/lib/python3.10/dist-packages (from pysynthdid==0.0.1) (1.5.3)
Requirement already satisfied: matplotlib in /usr/local/lib/python3.10/dist-packages (from pysynthdid==0.0.1) (3.7.1)
Requirement already satisfied: numpy in /usr/local/lib/python3.10/dist-packages (from pysynthdid==0.0.1) (1.23.5)
Requirement already satisfied: tqdm in /usr/local/lib/python3.10/dist-packages (from pysynthdid==0.0.1) (4.66.1)
Collecting sklearn (from pysynthdid==0.0.1)
Using cached sklearn-0.0.post7.tar.gz (3.6 kB)
error: subprocess-exited-with-error

× python setup.py egg_info did not run successfully.
│ exit code: 1
╰─> See above for output.

note: This error originates from a subprocess, and is likely not a problem with pip.
Preparing metadata (setup.py) ... error
error: metadata-generation-failed

× Encountered error while generating package metadata.
╰─> See above for output.

note: This is an issue with the package mentioned above, not pip.
hint: See above for details.`

Applying pysynthdid, obtaining homogeneous weights and no plot

I am trying to apply the code to a time series dataset I am working with. I arranged it as in the example

When I run:

'sdid = SynthDID(df_int, pre_terem, post_terem, treatment )
sdid.fit(zeta_type="base")
hat_omega_simple = sdid.estimated_params(model="sc")
hat_omega_simple'

the sc_weight is the same for all the features, at 0.027

Then when I run:

'sdid.plot(model="sc")'

I get:


TypeError Traceback (most recent call last)
Input In [139], in <cell line: 1>()
----> 1 sdid.plot(model="sc")

File ~/anaconda3/lib/python3.9/site-packages/synthdid/plot.py:12, in Plot.plot(self, model, figsize)
10 result = pd.DataFrame({"actual_y": self.target_y()})
11 post_actural_treat = result.loc[self.post_term[0] :, "actual_y"].mean()
---> 12 post_point = np.mean(self.Y_post_c.index)
14 if model == "sdid":
15 result["sdid"] = self.sdid_trajectory()

File <array_function internals>:5, in mean(*args, **kwargs)

File ~/anaconda3/lib/python3.9/site-packages/numpy/core/fromnumeric.py:3438, in mean(a, axis, dtype, out, keepdims, where)
3436 pass
3437 else:
-> 3438 return mean(axis=axis, dtype=dtype, out=out, **kwargs)
3440 return _methods._mean(a, axis=axis, dtype=dtype,
3441 out=out, **kwargs)

File ~/anaconda3/lib/python3.9/site-packages/pandas/core/indexes/extension.py:95, in _inherit_from_data..method(self, *args, **kwargs)
93 if "inplace" in kwargs:
94 raise ValueError(f"cannot use inplace with {type(self).name}")
---> 95 result = attr(self._data, *args, **kwargs)
96 if wrap:
97 if isinstance(result, type(self._data)):

TypeError: mean() got an unexpected keyword argument 'dtype'

Converting tau when data is log transform

Hey! thanks for your package

I've been playing around with it as an extension of Google's CausalImpact tool.

Got a dataset with market data having different scales, so log transformation is needed. However, I couldn't transform back ATT from its value estimated from log input data to one unscaled. Which formula should I use? Also, is it possible to get confidence interval for tau?

Discrepancy between graph and tau estimate

Hi, thanks so much for this package! I'm really grateful to you for coding this up and building it. For one of my applications, however, I'm finding some apparent discrepancies between the graphed quantities and the reported $\tau$. For example, see the below screenshot:

Screen Shot 2023-03-07 at 6 04 53 PM

This $\tau$ seems obviously much larger than the visible difference between the graphed lines, which inverts from roughly 0.1 above control to 0.2 below control, suggesting a $\tau$ closer to -0.3.

Can you help me understand what might be causing this discrepancy? I was trying to debug by inspecting the fixed effects, but I wasn't able to see in the code where the fixed effects are stored, so had trouble understanding what might have gone amiss; I'm not sure if the plot is wrong, if $\tau$ is wrong, or if there's just something odd about the relatively sparse $\lambda$ weighting that is leading to the mismatch between the estimate and the visual pattern. I'd be happy to provide replication data if that would be helpful, thanks regardless for your attention to this.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.