Skip to content

foryourhealth111-pixel/Vibe-Skills / bundled/skills/performing-causal-analysis

performing-causal-analysis

Verified

Estimate causal effects from existing data. Use when fitting or interpreting DiD, ITS, synthetic control, regression discontinuity, or other treatment-effect analyses, including robustness checks and counterfactual plots. For choosing a study design before analysis, use designing-experiments instead.

FileKindSize
SKILL.mdskill md1.4 KB
reference/diff_in_diff.mdmarkdown1.7 KB
reference/interrupted_time_series.mdmarkdown1.5 KB
reference/synthetic_control.mdmarkdown1.4 KB

reference/diff_in_diff.md

View on GitHub
# Causal Difference-in-Differences (DiD)

Difference-in-Differences (DiD) estimates the causal effect of a treatment by comparing the changes in outcomes over time between a treatment group and a control group.

## Class: `DifferenceInDifferences`

```python
causalpy.experiments.DifferenceInDifferences(
    data,
    formula,
    time_variable_name,
    group_variable_name,
    post_treatment_variable_name="post_treatment",
    model=None,
    **kwargs
)
```

### Parameters
*   **`data`** (`pd.DataFrame`): Input dataframe containing panel data.
*   **`formula`** (`str`): Statistical formula (e.g., `"y ~ 1 + group * post_treatment"`).
*   **`time_variable_name`** (`str`): Column name for the time variable.
*   **`group_variable_name`** (`str`): Column name for the group indicator (0=Control, 1=Treated). **Must be dummy coded**.
*   **`post_treatment_variable_name`** (`str`): Column name indicating the post-treatment period (0=Pre, 1=Post). Default is `"post_treatment"`.
*   **`model`**: A PyMC model (e.g., `cp.pymc_models.LinearRegression`) or a Scikit-Learn Regressor.

### How it Works
1.  **Fit**: The model fits all available data (pre/post, treatment/control).
2.  **Counterfactual**: Predicted by setting the interaction term between `group` and `post_treatment` to 0.
3.  **Impact**: The causal impact is the difference between observed and counterfactual.

### Example

```python
import causalpy as cp
import causalpy.pymc_models as cp_pymc

df = cp.load_data("did")

result = cp.DifferenceInDifferences(
    df,
    formula="y ~ 1 + group*post_treatment",
    time_variable_name="t",
    group_variable_name="group",
    model=cp_pymc.LinearRegression(sample_kwargs={"target_accept": 0.9})
)

result.summary()
result.plot()
```
brainstormingobra/superpowersYou MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation).Stars: 322kUpdated Claude CodeCodexVerifiedInstall
brand-guidelinesanthropics/skillsApplies Anthropic's official brand colors and typography to any sort of artifact that may benefit from having Anthropic's look-and-feel. Use it when brand colors or style guidelines, visual formatting, or company design standards apply.Stars: 198kUpdated Claude CodeCodexVerifiedInstall
frontend-designanthropics/skillsGuidance for distinctive, intentional visual design when building new UI or reshaping an existing one. Helps with aesthetic direction, typography, and making choices that don't read as templated defaults.Stars: 198kUpdated Claude CodeCodexVerifiedInstall
mcp-builderanthropics/skillsGuide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when building MCP servers to integrate external APIs or services, whether in Python (FastMCP) or Node/TypeScript (MCP SDK).Stars: 198kUpdated Claude CodeCodexVerifiedInstall