@wentorai/research-plugins 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +204 -0
- package/curated/analysis/README.md +64 -0
- package/curated/domains/README.md +104 -0
- package/curated/literature/README.md +53 -0
- package/curated/research/README.md +62 -0
- package/curated/tools/README.md +87 -0
- package/curated/writing/README.md +61 -0
- package/index.ts +39 -0
- package/mcp-configs/academic-db/ChatSpatial.json +17 -0
- package/mcp-configs/academic-db/academia-mcp.json +17 -0
- package/mcp-configs/academic-db/academic-paper-explorer.json +17 -0
- package/mcp-configs/academic-db/academic-search-mcp-server.json +17 -0
- package/mcp-configs/academic-db/agentinterviews-mcp.json +17 -0
- package/mcp-configs/academic-db/all-in-mcp.json +17 -0
- package/mcp-configs/academic-db/apple-health-mcp.json +17 -0
- package/mcp-configs/academic-db/arxiv-latex-mcp.json +17 -0
- package/mcp-configs/academic-db/arxiv-mcp-server.json +17 -0
- package/mcp-configs/academic-db/bgpt-mcp.json +17 -0
- package/mcp-configs/academic-db/biomcp.json +17 -0
- package/mcp-configs/academic-db/biothings-mcp.json +17 -0
- package/mcp-configs/academic-db/catalysishub-mcp-server.json +17 -0
- package/mcp-configs/academic-db/clinicaltrialsgov-mcp-server.json +17 -0
- package/mcp-configs/academic-db/deep-research-mcp.json +17 -0
- package/mcp-configs/academic-db/dicom-mcp.json +17 -0
- package/mcp-configs/academic-db/enrichr-mcp-server.json +17 -0
- package/mcp-configs/academic-db/fec-mcp-server.json +17 -0
- package/mcp-configs/academic-db/fhir-mcp-server-themomentum.json +17 -0
- package/mcp-configs/academic-db/fhir-mcp.json +19 -0
- package/mcp-configs/academic-db/gget-mcp.json +17 -0
- package/mcp-configs/academic-db/google-researcher-mcp.json +17 -0
- package/mcp-configs/academic-db/idea-reality-mcp.json +17 -0
- package/mcp-configs/academic-db/legiscan-mcp.json +19 -0
- package/mcp-configs/academic-db/lex.json +17 -0
- package/mcp-configs/ai-platform/Adaptive-Graph-of-Thoughts-MCP-server.json +17 -0
- package/mcp-configs/ai-platform/ai-counsel.json +17 -0
- package/mcp-configs/ai-platform/atlas-mcp-server.json +17 -0
- package/mcp-configs/ai-platform/counsel-mcp.json +17 -0
- package/mcp-configs/ai-platform/cross-llm-mcp.json +17 -0
- package/mcp-configs/ai-platform/gptr-mcp.json +17 -0
- package/mcp-configs/browser/decipher-research-agent.json +17 -0
- package/mcp-configs/browser/deep-research.json +17 -0
- package/mcp-configs/browser/everything-claude-code.json +17 -0
- package/mcp-configs/browser/gpt-researcher.json +17 -0
- package/mcp-configs/browser/heurist-agent-framework.json +17 -0
- package/mcp-configs/data-platform/4everland-hosting-mcp.json +17 -0
- package/mcp-configs/data-platform/context-keeper.json +17 -0
- package/mcp-configs/data-platform/context7.json +19 -0
- package/mcp-configs/data-platform/contextstream-mcp.json +17 -0
- package/mcp-configs/data-platform/email-mcp.json +17 -0
- package/mcp-configs/note-knowledge/ApeRAG.json +17 -0
- package/mcp-configs/note-knowledge/In-Memoria.json +17 -0
- package/mcp-configs/note-knowledge/agent-memory.json +17 -0
- package/mcp-configs/note-knowledge/aimemo.json +17 -0
- package/mcp-configs/note-knowledge/biel-mcp.json +19 -0
- package/mcp-configs/note-knowledge/cognee.json +17 -0
- package/mcp-configs/note-knowledge/context-awesome.json +17 -0
- package/mcp-configs/note-knowledge/context-mcp.json +17 -0
- package/mcp-configs/note-knowledge/conversation-handoff-mcp.json +17 -0
- package/mcp-configs/note-knowledge/cortex.json +17 -0
- package/mcp-configs/note-knowledge/devrag.json +17 -0
- package/mcp-configs/note-knowledge/easy-obsidian-mcp.json +17 -0
- package/mcp-configs/note-knowledge/engram.json +17 -0
- package/mcp-configs/note-knowledge/gnosis-mcp.json +17 -0
- package/mcp-configs/note-knowledge/graphlit-mcp-server.json +19 -0
- package/mcp-configs/reference-mgr/arxiv-cli.json +17 -0
- package/mcp-configs/reference-mgr/arxiv-search-mcp.json +17 -0
- package/mcp-configs/reference-mgr/chiken.json +17 -0
- package/mcp-configs/reference-mgr/claude-scholar.json +17 -0
- package/mcp-configs/reference-mgr/devonthink-mcp.json +17 -0
- package/mcp-configs/registry.json +447 -0
- package/openclaw.plugin.json +21 -0
- package/package.json +61 -0
- package/skills/analysis/dataviz/color-accessibility-guide/SKILL.md +230 -0
- package/skills/analysis/dataviz/geospatial-viz-guide/SKILL.md +218 -0
- package/skills/analysis/dataviz/interactive-viz-guide/SKILL.md +287 -0
- package/skills/analysis/dataviz/network-visualization-guide/SKILL.md +195 -0
- package/skills/analysis/dataviz/publication-figures-guide/SKILL.md +238 -0
- package/skills/analysis/dataviz/python-dataviz-guide/SKILL.md +195 -0
- package/skills/analysis/econometrics/causal-inference-guide/SKILL.md +197 -0
- package/skills/analysis/econometrics/iv-regression-guide/SKILL.md +198 -0
- package/skills/analysis/econometrics/panel-data-guide/SKILL.md +274 -0
- package/skills/analysis/econometrics/robustness-checks/SKILL.md +250 -0
- package/skills/analysis/econometrics/stata-regression/SKILL.md +117 -0
- package/skills/analysis/econometrics/time-series-guide/SKILL.md +235 -0
- package/skills/analysis/statistics/bayesian-statistics-guide/SKILL.md +221 -0
- package/skills/analysis/statistics/hypothesis-testing-guide/SKILL.md +210 -0
- package/skills/analysis/statistics/meta-analysis-guide/SKILL.md +206 -0
- package/skills/analysis/statistics/nonparametric-tests-guide/SKILL.md +221 -0
- package/skills/analysis/statistics/power-analysis-guide/SKILL.md +240 -0
- package/skills/analysis/statistics/sem-guide/SKILL.md +231 -0
- package/skills/analysis/statistics/survival-analysis-guide/SKILL.md +195 -0
- package/skills/analysis/wrangling/missing-data-handling/SKILL.md +224 -0
- package/skills/analysis/wrangling/pandas-data-wrangling/SKILL.md +242 -0
- package/skills/analysis/wrangling/questionnaire-design-guide/SKILL.md +234 -0
- package/skills/analysis/wrangling/text-mining-guide/SKILL.md +225 -0
- package/skills/domains/ai-ml/computer-vision-guide/SKILL.md +213 -0
- package/skills/domains/ai-ml/deep-learning-papers-guide/SKILL.md +200 -0
- package/skills/domains/ai-ml/llm-evaluation-guide/SKILL.md +194 -0
- package/skills/domains/ai-ml/prompt-engineering-research/SKILL.md +233 -0
- package/skills/domains/ai-ml/reinforcement-learning-guide/SKILL.md +254 -0
- package/skills/domains/ai-ml/transformer-architecture-guide/SKILL.md +233 -0
- package/skills/domains/biomedical/clinical-research-guide/SKILL.md +232 -0
- package/skills/domains/biomedical/clinicaltrials-api/SKILL.md +177 -0
- package/skills/domains/biomedical/epidemiology-guide/SKILL.md +200 -0
- package/skills/domains/biomedical/genomics-analysis-guide/SKILL.md +270 -0
- package/skills/domains/business/market-analysis-guide/SKILL.md +112 -0
- package/skills/domains/business/strategic-management-guide/SKILL.md +154 -0
- package/skills/domains/chemistry/computational-chemistry-guide/SKILL.md +266 -0
- package/skills/domains/chemistry/retrosynthesis-guide/SKILL.md +215 -0
- package/skills/domains/cs/algorithms-complexity-guide/SKILL.md +194 -0
- package/skills/domains/cs/dblp-api/SKILL.md +129 -0
- package/skills/domains/cs/software-engineering-research/SKILL.md +218 -0
- package/skills/domains/ecology/biodiversity-data-guide/SKILL.md +296 -0
- package/skills/domains/ecology/conservation-biology-guide/SKILL.md +198 -0
- package/skills/domains/ecology/gbif-api/SKILL.md +158 -0
- package/skills/domains/ecology/inaturalist-api/SKILL.md +173 -0
- package/skills/domains/economics/behavioral-economics-guide/SKILL.md +239 -0
- package/skills/domains/economics/development-economics-guide/SKILL.md +181 -0
- package/skills/domains/economics/fred-api/SKILL.md +189 -0
- package/skills/domains/education/curriculum-design-guide/SKILL.md +144 -0
- package/skills/domains/education/learning-science-guide/SKILL.md +150 -0
- package/skills/domains/finance/financial-data-analysis/SKILL.md +152 -0
- package/skills/domains/finance/quantitative-finance-guide/SKILL.md +151 -0
- package/skills/domains/geoscience/climate-science-guide/SKILL.md +158 -0
- package/skills/domains/geoscience/gis-remote-sensing-guide/SKILL.md +129 -0
- package/skills/domains/humanities/digital-humanities-guide/SKILL.md +181 -0
- package/skills/domains/humanities/philosophy-research-guide/SKILL.md +148 -0
- package/skills/domains/law/courtlistener-api/SKILL.md +213 -0
- package/skills/domains/law/legal-research-guide/SKILL.md +250 -0
- package/skills/domains/math/linear-algebra-applications/SKILL.md +227 -0
- package/skills/domains/math/numerical-methods-guide/SKILL.md +236 -0
- package/skills/domains/math/oeis-api/SKILL.md +158 -0
- package/skills/domains/pharma/clinical-pharmacology-guide/SKILL.md +165 -0
- package/skills/domains/pharma/drug-development-guide/SKILL.md +177 -0
- package/skills/domains/physics/computational-physics-guide/SKILL.md +300 -0
- package/skills/domains/physics/nasa-ads-api/SKILL.md +150 -0
- package/skills/domains/physics/quantum-computing-guide/SKILL.md +234 -0
- package/skills/domains/social-science/social-research-methods/SKILL.md +194 -0
- package/skills/domains/social-science/survey-research-guide/SKILL.md +182 -0
- package/skills/literature/discovery/citation-alert-guide/SKILL.md +154 -0
- package/skills/literature/discovery/conference-proceedings-guide/SKILL.md +142 -0
- package/skills/literature/discovery/literature-mapping-guide/SKILL.md +175 -0
- package/skills/literature/discovery/paper-tracking-guide/SKILL.md +211 -0
- package/skills/literature/discovery/rss-paper-feeds/SKILL.md +214 -0
- package/skills/literature/discovery/semantic-scholar-recs-guide/SKILL.md +164 -0
- package/skills/literature/fulltext/doaj-api/SKILL.md +120 -0
- package/skills/literature/fulltext/interlibrary-loan-guide/SKILL.md +163 -0
- package/skills/literature/fulltext/open-access-guide/SKILL.md +183 -0
- package/skills/literature/fulltext/pmc-oai-api/SKILL.md +184 -0
- package/skills/literature/fulltext/preprint-servers-guide/SKILL.md +128 -0
- package/skills/literature/fulltext/repository-harvesting-guide/SKILL.md +207 -0
- package/skills/literature/fulltext/unpaywall-api/SKILL.md +113 -0
- package/skills/literature/metadata/altmetrics-guide/SKILL.md +132 -0
- package/skills/literature/metadata/citation-network-guide/SKILL.md +236 -0
- package/skills/literature/metadata/crossref-api/SKILL.md +133 -0
- package/skills/literature/metadata/datacite-api/SKILL.md +126 -0
- package/skills/literature/metadata/doi-resolution-guide/SKILL.md +168 -0
- package/skills/literature/metadata/h-index-guide/SKILL.md +183 -0
- package/skills/literature/metadata/journal-metrics-guide/SKILL.md +188 -0
- package/skills/literature/metadata/opencitations-api/SKILL.md +128 -0
- package/skills/literature/metadata/orcid-api/SKILL.md +136 -0
- package/skills/literature/metadata/orcid-integration-guide/SKILL.md +178 -0
- package/skills/literature/search/arxiv-api/SKILL.md +95 -0
- package/skills/literature/search/biorxiv-api/SKILL.md +123 -0
- package/skills/literature/search/boolean-search-guide/SKILL.md +199 -0
- package/skills/literature/search/citation-chaining-guide/SKILL.md +148 -0
- package/skills/literature/search/database-comparison-guide/SKILL.md +100 -0
- package/skills/literature/search/europe-pmc-api/SKILL.md +120 -0
- package/skills/literature/search/google-scholar-guide/SKILL.md +182 -0
- package/skills/literature/search/mesh-terms-guide/SKILL.md +164 -0
- package/skills/literature/search/openalex-api/SKILL.md +134 -0
- package/skills/literature/search/pubmed-api/SKILL.md +130 -0
- package/skills/literature/search/scientify-literature-survey/SKILL.md +203 -0
- package/skills/literature/search/semantic-scholar-api/SKILL.md +134 -0
- package/skills/literature/search/systematic-search-strategy/SKILL.md +214 -0
- package/skills/research/automation/ai-scientist-guide/SKILL.md +228 -0
- package/skills/research/automation/data-collection-automation/SKILL.md +248 -0
- package/skills/research/automation/research-workflow-automation/SKILL.md +266 -0
- package/skills/research/deep-research/meta-synthesis-guide/SKILL.md +174 -0
- package/skills/research/deep-research/research-cog/SKILL.md +153 -0
- package/skills/research/deep-research/scoping-review-guide/SKILL.md +217 -0
- package/skills/research/deep-research/systematic-review-guide/SKILL.md +250 -0
- package/skills/research/funding/figshare-api/SKILL.md +163 -0
- package/skills/research/funding/grant-writing-guide/SKILL.md +233 -0
- package/skills/research/funding/nsf-grant-guide/SKILL.md +206 -0
- package/skills/research/funding/open-science-guide/SKILL.md +255 -0
- package/skills/research/funding/zenodo-api/SKILL.md +174 -0
- package/skills/research/methodology/action-research-guide/SKILL.md +201 -0
- package/skills/research/methodology/experimental-design-guide/SKILL.md +236 -0
- package/skills/research/methodology/grad-school-guide/SKILL.md +182 -0
- package/skills/research/methodology/grounded-theory-guide/SKILL.md +171 -0
- package/skills/research/methodology/mixed-methods-guide/SKILL.md +208 -0
- package/skills/research/methodology/qualitative-research-guide/SKILL.md +234 -0
- package/skills/research/methodology/scientify-idea-generation/SKILL.md +222 -0
- package/skills/research/paper-review/paper-reading-assistant/SKILL.md +266 -0
- package/skills/research/paper-review/peer-review-guide/SKILL.md +227 -0
- package/skills/research/paper-review/rebuttal-writing-guide/SKILL.md +185 -0
- package/skills/research/paper-review/scientify-write-review-paper/SKILL.md +209 -0
- package/skills/tools/code-exec/jupyter-notebook-guide/SKILL.md +178 -0
- package/skills/tools/code-exec/python-reproducibility-guide/SKILL.md +341 -0
- package/skills/tools/code-exec/r-reproducibility-guide/SKILL.md +236 -0
- package/skills/tools/code-exec/sandbox-execution-guide/SKILL.md +221 -0
- package/skills/tools/diagram/mermaid-diagram-guide/SKILL.md +269 -0
- package/skills/tools/diagram/plantuml-guide/SKILL.md +397 -0
- package/skills/tools/diagram/scientific-illustration-guide/SKILL.md +225 -0
- package/skills/tools/document/anystyle-api/SKILL.md +199 -0
- package/skills/tools/document/grobid-pdf-parsing/SKILL.md +294 -0
- package/skills/tools/document/markdown-academic-guide/SKILL.md +217 -0
- package/skills/tools/document/pdf-extraction-guide/SKILL.md +321 -0
- package/skills/tools/knowledge-graph/knowledge-graph-construction/SKILL.md +306 -0
- package/skills/tools/knowledge-graph/ontology-design-guide/SKILL.md +214 -0
- package/skills/tools/knowledge-graph/rag-methodology-guide/SKILL.md +325 -0
- package/skills/tools/ocr-translate/formula-recognition-guide/SKILL.md +367 -0
- package/skills/tools/ocr-translate/handwriting-recognition-guide/SKILL.md +211 -0
- package/skills/tools/ocr-translate/latex-ocr-guide/SKILL.md +204 -0
- package/skills/tools/ocr-translate/multilingual-research-guide/SKILL.md +234 -0
- package/skills/tools/scraping/academic-web-scraping/SKILL.md +326 -0
- package/skills/tools/scraping/api-data-collection-guide/SKILL.md +301 -0
- package/skills/tools/scraping/web-scraping-ethics-guide/SKILL.md +250 -0
- package/skills/writing/citation/bibtex-management-guide/SKILL.md +246 -0
- package/skills/writing/citation/citation-style-guide/SKILL.md +248 -0
- package/skills/writing/citation/reference-manager-comparison/SKILL.md +208 -0
- package/skills/writing/citation/zotero-api/SKILL.md +188 -0
- package/skills/writing/composition/abstract-writing-guide/SKILL.md +188 -0
- package/skills/writing/composition/discussion-writing-guide/SKILL.md +194 -0
- package/skills/writing/composition/introduction-writing-guide/SKILL.md +194 -0
- package/skills/writing/composition/literature-review-writing/SKILL.md +196 -0
- package/skills/writing/composition/methods-section-guide/SKILL.md +185 -0
- package/skills/writing/composition/response-to-reviewers/SKILL.md +215 -0
- package/skills/writing/composition/scientific-writing-guide/SKILL.md +152 -0
- package/skills/writing/latex/bibliography-management-guide/SKILL.md +206 -0
- package/skills/writing/latex/latex-drawing-guide/SKILL.md +234 -0
- package/skills/writing/latex/latex-ecosystem-guide/SKILL.md +240 -0
- package/skills/writing/latex/math-typesetting-guide/SKILL.md +231 -0
- package/skills/writing/latex/overleaf-collaboration-guide/SKILL.md +211 -0
- package/skills/writing/latex/tikz-diagrams-guide/SKILL.md +211 -0
- package/skills/writing/polish/academic-translation-guide/SKILL.md +175 -0
- package/skills/writing/polish/academic-writing-refiner/SKILL.md +143 -0
- package/skills/writing/polish/ai-writing-humanizer/SKILL.md +178 -0
- package/skills/writing/polish/grammar-checker-guide/SKILL.md +184 -0
- package/skills/writing/polish/plagiarism-detection-guide/SKILL.md +167 -0
- package/skills/writing/templates/beamer-presentation-guide/SKILL.md +263 -0
- package/skills/writing/templates/conference-paper-template/SKILL.md +219 -0
- package/skills/writing/templates/thesis-template-guide/SKILL.md +200 -0
- package/skills/writing/templates/thesis-writing-guide/SKILL.md +220 -0
- package/src/tools/arxiv.ts +131 -0
- package/src/tools/crossref.ts +112 -0
- package/src/tools/openalex.ts +174 -0
- package/src/tools/pubmed.ts +166 -0
- package/src/tools/semantic-scholar.ts +108 -0
- package/src/tools/unpaywall.ts +58 -0
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: publication-figures-guide
|
|
3
|
+
description: "Create journal-quality scientific figures with proper styling and accessibility"
|
|
4
|
+
metadata:
|
|
5
|
+
openclaw:
|
|
6
|
+
emoji: "art"
|
|
7
|
+
category: "analysis"
|
|
8
|
+
subcategory: "dataviz"
|
|
9
|
+
keywords: ["scientific figure creation", "publication quality figure", "figure standards", "colorblind-friendly palette", "data visualization"]
|
|
10
|
+
source: "wentor"
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
# Publication Figures Guide
|
|
14
|
+
|
|
15
|
+
A skill for creating publication-quality scientific figures that meet journal standards for resolution, formatting, accessibility, and visual clarity. Covers matplotlib, seaborn, and ggplot2 workflows with journal-ready export settings.
|
|
16
|
+
|
|
17
|
+
## Journal Figure Requirements
|
|
18
|
+
|
|
19
|
+
### Common Standards
|
|
20
|
+
|
|
21
|
+
| Requirement | Typical Spec | Notes |
|
|
22
|
+
|------------|-------------|-------|
|
|
23
|
+
| Resolution | 300-600 DPI | 300 DPI minimum for print |
|
|
24
|
+
| File format | PDF, EPS, TIFF | Vector (PDF/EPS) preferred |
|
|
25
|
+
| Color mode | CMYK for print, RGB for online | Check journal spec |
|
|
26
|
+
| Max width | Single column: 3.3in / Double: 6.7in | Varies by journal |
|
|
27
|
+
| Font size | 6-8pt minimum | Must be legible at final print size |
|
|
28
|
+
| Line width | 0.5-1.5pt | Thin lines may not reproduce |
|
|
29
|
+
| File size | Varies (often <10MB per figure) | TIFF can be large |
|
|
30
|
+
|
|
31
|
+
### Matplotlib Configuration for Publication
|
|
32
|
+
|
|
33
|
+
```python
|
|
34
|
+
import matplotlib.pyplot as plt
|
|
35
|
+
import matplotlib as mpl
|
|
36
|
+
import numpy as np
|
|
37
|
+
|
|
38
|
+
def setup_publication_style(journal: str = 'nature'):
|
|
39
|
+
"""
|
|
40
|
+
Configure matplotlib for publication-quality figures.
|
|
41
|
+
"""
|
|
42
|
+
styles = {
|
|
43
|
+
'nature': {
|
|
44
|
+
'figure.figsize': (3.3, 2.5), # single column
|
|
45
|
+
'font.size': 7,
|
|
46
|
+
'font.family': 'sans-serif',
|
|
47
|
+
'font.sans-serif': ['Arial', 'Helvetica'],
|
|
48
|
+
'axes.linewidth': 0.5,
|
|
49
|
+
'axes.labelsize': 8,
|
|
50
|
+
'xtick.labelsize': 7,
|
|
51
|
+
'ytick.labelsize': 7,
|
|
52
|
+
'legend.fontsize': 6,
|
|
53
|
+
'lines.linewidth': 1.0,
|
|
54
|
+
'lines.markersize': 4,
|
|
55
|
+
'savefig.dpi': 300,
|
|
56
|
+
'savefig.bbox': 'tight',
|
|
57
|
+
'savefig.pad_inches': 0.05,
|
|
58
|
+
},
|
|
59
|
+
'ieee': {
|
|
60
|
+
'figure.figsize': (3.5, 2.6),
|
|
61
|
+
'font.size': 8,
|
|
62
|
+
'font.family': 'serif',
|
|
63
|
+
'font.serif': ['Times New Roman', 'Times'],
|
|
64
|
+
'axes.linewidth': 0.5,
|
|
65
|
+
'axes.labelsize': 9,
|
|
66
|
+
'xtick.labelsize': 8,
|
|
67
|
+
'ytick.labelsize': 8,
|
|
68
|
+
'legend.fontsize': 7,
|
|
69
|
+
'lines.linewidth': 1.0,
|
|
70
|
+
'savefig.dpi': 300,
|
|
71
|
+
},
|
|
72
|
+
'acs': {
|
|
73
|
+
'figure.figsize': (3.25, 2.5),
|
|
74
|
+
'font.size': 7,
|
|
75
|
+
'font.family': 'sans-serif',
|
|
76
|
+
'font.sans-serif': ['Arial'],
|
|
77
|
+
'axes.linewidth': 0.5,
|
|
78
|
+
'savefig.dpi': 600,
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
style = styles.get(journal, styles['nature'])
|
|
83
|
+
mpl.rcParams.update(style)
|
|
84
|
+
return style
|
|
85
|
+
|
|
86
|
+
setup_publication_style('nature')
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## Colorblind-Friendly Palettes
|
|
90
|
+
|
|
91
|
+
### Recommended Color Schemes
|
|
92
|
+
|
|
93
|
+
```python
|
|
94
|
+
def get_accessible_palette(n_colors: int = 8, style: str = 'categorical') -> list:
|
|
95
|
+
"""
|
|
96
|
+
Return colorblind-friendly palettes.
|
|
97
|
+
"""
|
|
98
|
+
palettes = {
|
|
99
|
+
'categorical': {
|
|
100
|
+
# Wong (2011) Nature Methods palette
|
|
101
|
+
3: ['#0072B2', '#D55E00', '#009E73'],
|
|
102
|
+
4: ['#0072B2', '#D55E00', '#009E73', '#CC79A7'],
|
|
103
|
+
5: ['#0072B2', '#D55E00', '#009E73', '#CC79A7', '#F0E442'],
|
|
104
|
+
8: ['#0072B2', '#D55E00', '#009E73', '#CC79A7',
|
|
105
|
+
'#F0E442', '#56B4E9', '#E69F00', '#000000']
|
|
106
|
+
},
|
|
107
|
+
'sequential': {
|
|
108
|
+
# Viridis-based (perceptually uniform)
|
|
109
|
+
'cmap': 'viridis' # Also: 'cividis', 'inferno', 'magma'
|
|
110
|
+
},
|
|
111
|
+
'diverging': {
|
|
112
|
+
'cmap': 'RdBu_r' # Also: 'coolwarm', 'BrBG'
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if style == 'categorical':
|
|
117
|
+
n = min(n_colors, 8)
|
|
118
|
+
return palettes['categorical'].get(n, palettes['categorical'][8][:n])
|
|
119
|
+
else:
|
|
120
|
+
return palettes[style]
|
|
121
|
+
|
|
122
|
+
# Usage
|
|
123
|
+
colors = get_accessible_palette(4)
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
## Common Figure Types
|
|
127
|
+
|
|
128
|
+
### Bar Charts with Error Bars
|
|
129
|
+
|
|
130
|
+
```python
|
|
131
|
+
def publication_barplot(data: dict, ylabel: str, title: str = '',
|
|
132
|
+
output: str = 'figure.pdf'):
|
|
133
|
+
"""
|
|
134
|
+
Create a publication-quality bar chart.
|
|
135
|
+
|
|
136
|
+
Args:
|
|
137
|
+
data: Dict mapping group names to (mean, std_error) tuples
|
|
138
|
+
"""
|
|
139
|
+
setup_publication_style('nature')
|
|
140
|
+
colors = get_accessible_palette(len(data))
|
|
141
|
+
|
|
142
|
+
fig, ax = plt.subplots()
|
|
143
|
+
x = np.arange(len(data))
|
|
144
|
+
names = list(data.keys())
|
|
145
|
+
means = [data[k][0] for k in names]
|
|
146
|
+
errors = [data[k][1] for k in names]
|
|
147
|
+
|
|
148
|
+
bars = ax.bar(x, means, yerr=errors, capsize=3, color=colors,
|
|
149
|
+
edgecolor='black', linewidth=0.5, width=0.6,
|
|
150
|
+
error_kw={'linewidth': 0.5})
|
|
151
|
+
|
|
152
|
+
ax.set_xticks(x)
|
|
153
|
+
ax.set_xticklabels(names, rotation=0)
|
|
154
|
+
ax.set_ylabel(ylabel)
|
|
155
|
+
if title:
|
|
156
|
+
ax.set_title(title)
|
|
157
|
+
|
|
158
|
+
# Remove top and right spines
|
|
159
|
+
ax.spines['top'].set_visible(False)
|
|
160
|
+
ax.spines['right'].set_visible(False)
|
|
161
|
+
|
|
162
|
+
fig.savefig(output, dpi=300, bbox_inches='tight')
|
|
163
|
+
plt.close()
|
|
164
|
+
return output
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
### Scatter Plots with Regression Lines
|
|
168
|
+
|
|
169
|
+
```python
|
|
170
|
+
from scipy import stats
|
|
171
|
+
|
|
172
|
+
def publication_scatter(x, y, xlabel, ylabel, output='scatter.pdf',
|
|
173
|
+
groups=None, group_labels=None):
|
|
174
|
+
"""Publication-quality scatter plot with optional regression line."""
|
|
175
|
+
setup_publication_style('nature')
|
|
176
|
+
fig, ax = plt.subplots()
|
|
177
|
+
|
|
178
|
+
if groups is None:
|
|
179
|
+
ax.scatter(x, y, s=15, alpha=0.7, color='#0072B2', edgecolors='none')
|
|
180
|
+
# Regression line
|
|
181
|
+
slope, intercept, r, p, se = stats.linregress(x, y)
|
|
182
|
+
x_fit = np.linspace(min(x), max(x), 100)
|
|
183
|
+
ax.plot(x_fit, slope*x_fit + intercept, '--', color='#D55E00', linewidth=0.8)
|
|
184
|
+
ax.text(0.05, 0.95, f'r = {r:.2f}, p = {p:.3f}',
|
|
185
|
+
transform=ax.transAxes, fontsize=6, va='top')
|
|
186
|
+
else:
|
|
187
|
+
colors = get_accessible_palette(len(set(groups)))
|
|
188
|
+
for i, label in enumerate(group_labels or sorted(set(groups))):
|
|
189
|
+
mask = np.array(groups) == label
|
|
190
|
+
ax.scatter(np.array(x)[mask], np.array(y)[mask],
|
|
191
|
+
s=15, alpha=0.7, color=colors[i], label=label)
|
|
192
|
+
ax.legend(frameon=False)
|
|
193
|
+
|
|
194
|
+
ax.set_xlabel(xlabel)
|
|
195
|
+
ax.set_ylabel(ylabel)
|
|
196
|
+
ax.spines['top'].set_visible(False)
|
|
197
|
+
ax.spines['right'].set_visible(False)
|
|
198
|
+
|
|
199
|
+
fig.savefig(output, dpi=300, bbox_inches='tight')
|
|
200
|
+
plt.close()
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
## Multi-Panel Figures
|
|
204
|
+
|
|
205
|
+
```python
|
|
206
|
+
def multi_panel_figure(n_rows, n_cols, panel_data, output='multipanel.pdf'):
|
|
207
|
+
"""Create a multi-panel figure with automatic panel labels."""
|
|
208
|
+
setup_publication_style('nature')
|
|
209
|
+
fig, axes = plt.subplots(n_rows, n_cols,
|
|
210
|
+
figsize=(3.3*n_cols, 2.5*n_rows))
|
|
211
|
+
if n_rows * n_cols == 1:
|
|
212
|
+
axes = np.array([axes])
|
|
213
|
+
axes = axes.flatten()
|
|
214
|
+
|
|
215
|
+
labels = 'abcdefghijklmnopqrstuvwxyz'
|
|
216
|
+
for i, ax in enumerate(axes[:len(panel_data)]):
|
|
217
|
+
# Add panel label
|
|
218
|
+
ax.text(-0.15, 1.05, labels[i], transform=ax.transAxes,
|
|
219
|
+
fontsize=10, fontweight='bold', va='bottom')
|
|
220
|
+
|
|
221
|
+
plt.tight_layout()
|
|
222
|
+
fig.savefig(output, dpi=300, bbox_inches='tight')
|
|
223
|
+
plt.close()
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
## Export Best Practices
|
|
227
|
+
|
|
228
|
+
1. **Vector formats first**: Use PDF or EPS for line art and charts; TIFF only for photographs
|
|
229
|
+
2. **Font embedding**: Ensure all fonts are embedded (use `plt.rcParams['pdf.fonttype'] = 42`)
|
|
230
|
+
3. **Check at print size**: View the figure at actual print size (3.3in wide) to verify readability
|
|
231
|
+
4. **CMYK conversion**: For print journals, convert RGB to CMYK using ImageMagick or Photoshop
|
|
232
|
+
5. **Consistent styling**: All figures in a paper should use the same fonts, colors, and styling
|
|
233
|
+
|
|
234
|
+
```python
|
|
235
|
+
# Ensure fonts are embedded in PDF output
|
|
236
|
+
mpl.rcParams['pdf.fonttype'] = 42 # TrueType fonts
|
|
237
|
+
mpl.rcParams['ps.fonttype'] = 42
|
|
238
|
+
```
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: python-dataviz-guide
|
|
3
|
+
description: "Publication-quality data visualization with matplotlib, seaborn, and plotly"
|
|
4
|
+
metadata:
|
|
5
|
+
openclaw:
|
|
6
|
+
emoji: "📊"
|
|
7
|
+
category: "analysis"
|
|
8
|
+
subcategory: "dataviz"
|
|
9
|
+
keywords: ["data visualization", "chart design", "Python dataviz", "scientific figure creation", "publication quality figure"]
|
|
10
|
+
source: "N/A"
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
# Python Data Visualization Guide
|
|
14
|
+
|
|
15
|
+
## Overview
|
|
16
|
+
|
|
17
|
+
Data visualization is how researchers communicate quantitative findings. A well-designed figure can convey complex relationships instantly, while a poor one buries the signal in clutter. Python's visualization ecosystem -- anchored by matplotlib, seaborn, and plotly -- provides everything needed to produce publication-quality figures for journals, conferences, and presentations.
|
|
18
|
+
|
|
19
|
+
This guide covers the three major Python visualization libraries, their strengths and trade-offs, and concrete recipes for the chart types researchers use most frequently. Each example is designed to be copy-paste ready and customizable for your specific dataset and venue requirements.
|
|
20
|
+
|
|
21
|
+
The emphasis is on producing figures that meet journal standards: correct DPI, appropriate font sizes, accessible color palettes, and vector-format exports. We also cover interactive visualization with plotly for exploratory analysis and supplementary materials.
|
|
22
|
+
|
|
23
|
+
## Matplotlib: The Foundation
|
|
24
|
+
|
|
25
|
+
Matplotlib is the most flexible Python plotting library. Nearly every other visualization tool in the Python ecosystem builds on it.
|
|
26
|
+
|
|
27
|
+
### Setting Up Publication Defaults
|
|
28
|
+
|
|
29
|
+
```python
|
|
30
|
+
import matplotlib.pyplot as plt
|
|
31
|
+
import matplotlib as mpl
|
|
32
|
+
|
|
33
|
+
# Publication-quality defaults
|
|
34
|
+
plt.rcParams.update({
|
|
35
|
+
'figure.figsize': (6, 4),
|
|
36
|
+
'figure.dpi': 150,
|
|
37
|
+
'savefig.dpi': 300,
|
|
38
|
+
'savefig.bbox': 'tight',
|
|
39
|
+
'font.size': 11,
|
|
40
|
+
'font.family': 'serif',
|
|
41
|
+
'font.serif': ['Times New Roman'],
|
|
42
|
+
'axes.labelsize': 12,
|
|
43
|
+
'axes.titlesize': 13,
|
|
44
|
+
'xtick.labelsize': 10,
|
|
45
|
+
'ytick.labelsize': 10,
|
|
46
|
+
'legend.fontsize': 10,
|
|
47
|
+
'lines.linewidth': 1.5,
|
|
48
|
+
'lines.markersize': 6,
|
|
49
|
+
'axes.grid': True,
|
|
50
|
+
'grid.alpha': 0.3,
|
|
51
|
+
})
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### Line Plot with Error Bands
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
import numpy as np
|
|
58
|
+
|
|
59
|
+
epochs = np.arange(1, 51)
|
|
60
|
+
acc_mean = 1 - 0.5 * np.exp(-epochs / 10)
|
|
61
|
+
acc_std = 0.03 * np.exp(-epochs / 20)
|
|
62
|
+
|
|
63
|
+
fig, ax = plt.subplots()
|
|
64
|
+
ax.plot(epochs, acc_mean, label='Our Method', color='#2563EB')
|
|
65
|
+
ax.fill_between(epochs, acc_mean - acc_std, acc_mean + acc_std,
|
|
66
|
+
alpha=0.2, color='#2563EB')
|
|
67
|
+
ax.set_xlabel('Epoch')
|
|
68
|
+
ax.set_ylabel('Accuracy')
|
|
69
|
+
ax.set_ylim(0.4, 1.0)
|
|
70
|
+
ax.legend(frameon=False)
|
|
71
|
+
fig.savefig('accuracy_curve.pdf') # Vector format for papers
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Multi-Panel Figures
|
|
75
|
+
|
|
76
|
+
```python
|
|
77
|
+
fig, axes = plt.subplots(1, 3, figsize=(15, 4), sharey=True)
|
|
78
|
+
|
|
79
|
+
for ax, dataset, color in zip(axes, ['CIFAR-10', 'ImageNet', 'COCO'],
|
|
80
|
+
['#2563EB', '#DC2626', '#16A34A']):
|
|
81
|
+
x = np.random.randn(200)
|
|
82
|
+
ax.hist(x, bins=30, color=color, alpha=0.7, edgecolor='white')
|
|
83
|
+
ax.set_title(dataset)
|
|
84
|
+
ax.set_xlabel('Score Distribution')
|
|
85
|
+
|
|
86
|
+
axes[0].set_ylabel('Count')
|
|
87
|
+
plt.tight_layout()
|
|
88
|
+
fig.savefig('multi_panel.pdf')
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## Seaborn: Statistical Visualization
|
|
92
|
+
|
|
93
|
+
Seaborn excels at statistical graphics with minimal code. It handles data frames natively and produces polished output by default.
|
|
94
|
+
|
|
95
|
+
### Comparison Bar Chart with Significance
|
|
96
|
+
|
|
97
|
+
```python
|
|
98
|
+
import seaborn as sns
|
|
99
|
+
import pandas as pd
|
|
100
|
+
|
|
101
|
+
data = pd.DataFrame({
|
|
102
|
+
'Method': ['Baseline', 'Baseline', 'Ours', 'Ours', 'Ours+FT', 'Ours+FT'],
|
|
103
|
+
'Metric': ['BLEU', 'ROUGE'] * 3,
|
|
104
|
+
'Score': [34.2, 45.1, 41.8, 52.3, 48.5, 58.7]
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
fig, ax = plt.subplots(figsize=(8, 5))
|
|
108
|
+
sns.barplot(data=data, x='Metric', y='Score', hue='Method',
|
|
109
|
+
palette=['#94A3B8', '#3B82F6', '#EF4444'], ax=ax)
|
|
110
|
+
ax.set_ylabel('Score')
|
|
111
|
+
ax.legend(title='Method', frameon=False)
|
|
112
|
+
fig.savefig('comparison.pdf')
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### Correlation Heatmap
|
|
116
|
+
|
|
117
|
+
```python
|
|
118
|
+
corr_matrix = pd.DataFrame(
|
|
119
|
+
np.random.randn(8, 8),
|
|
120
|
+
columns=[f'Feature {i}' for i in range(8)]
|
|
121
|
+
).corr()
|
|
122
|
+
|
|
123
|
+
fig, ax = plt.subplots(figsize=(8, 7))
|
|
124
|
+
sns.heatmap(corr_matrix, annot=True, fmt='.2f', cmap='RdBu_r',
|
|
125
|
+
center=0, square=True, linewidths=0.5, ax=ax)
|
|
126
|
+
ax.set_title('Feature Correlation Matrix')
|
|
127
|
+
fig.savefig('heatmap.pdf')
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### Violin Plot for Distribution Comparison
|
|
131
|
+
|
|
132
|
+
```python
|
|
133
|
+
df = pd.DataFrame({
|
|
134
|
+
'Group': np.repeat(['Control', 'Treatment A', 'Treatment B'], 100),
|
|
135
|
+
'Value': np.concatenate([
|
|
136
|
+
np.random.normal(50, 10, 100),
|
|
137
|
+
np.random.normal(55, 8, 100),
|
|
138
|
+
np.random.normal(60, 12, 100)
|
|
139
|
+
])
|
|
140
|
+
})
|
|
141
|
+
|
|
142
|
+
fig, ax = plt.subplots(figsize=(8, 5))
|
|
143
|
+
sns.violinplot(data=df, x='Group', y='Value', palette='Set2',
|
|
144
|
+
inner='box', ax=ax)
|
|
145
|
+
ax.set_ylabel('Measurement')
|
|
146
|
+
fig.savefig('violin.pdf')
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
## Plotly: Interactive Visualization
|
|
150
|
+
|
|
151
|
+
Plotly is ideal for exploratory analysis and HTML-based supplementary materials.
|
|
152
|
+
|
|
153
|
+
```python
|
|
154
|
+
import plotly.express as px
|
|
155
|
+
|
|
156
|
+
df = px.data.gapminder().query("year == 2007")
|
|
157
|
+
fig = px.scatter(df, x="gdpPercap", y="lifeExp",
|
|
158
|
+
size="pop", color="continent",
|
|
159
|
+
hover_name="country",
|
|
160
|
+
log_x=True, size_max=60,
|
|
161
|
+
title="GDP vs Life Expectancy (2007)")
|
|
162
|
+
fig.write_html("interactive_scatter.html")
|
|
163
|
+
fig.write_image("scatter.pdf") # Requires kaleido
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
## Chart Type Selection Guide
|
|
167
|
+
|
|
168
|
+
| Data Relationship | Recommended Chart | Library |
|
|
169
|
+
|-------------------|-------------------|---------|
|
|
170
|
+
| Trend over time | Line plot | matplotlib |
|
|
171
|
+
| Distribution | Histogram, violin, box | seaborn |
|
|
172
|
+
| Comparison (categories) | Bar chart, grouped bar | seaborn |
|
|
173
|
+
| Correlation (2 vars) | Scatter plot | matplotlib/plotly |
|
|
174
|
+
| Correlation (matrix) | Heatmap | seaborn |
|
|
175
|
+
| Part-to-whole | Stacked bar (not pie) | matplotlib |
|
|
176
|
+
| High-dimensional | PCA/t-SNE scatter | plotly |
|
|
177
|
+
| Geospatial | Choropleth | plotly |
|
|
178
|
+
|
|
179
|
+
## Best Practices
|
|
180
|
+
|
|
181
|
+
- **Export as PDF or SVG for print, PNG at 300 DPI as fallback.** Never submit JPEG figures to journals.
|
|
182
|
+
- **Use colorblind-safe palettes.** `sns.color_palette("colorblind")` or use tools like ColorBrewer.
|
|
183
|
+
- **Label everything.** Axes, legends, and units should be readable without referring to the caption.
|
|
184
|
+
- **Avoid chartjunk.** Remove unnecessary gridlines, borders, and decorative elements.
|
|
185
|
+
- **Match the figure width to the journal column width.** Single-column is typically 3.3 inches; double-column is 6.9 inches.
|
|
186
|
+
- **Use consistent styling across all figures in a paper.** Define a style dictionary once and reuse it.
|
|
187
|
+
- **Include error bars or confidence intervals.** Raw point estimates without uncertainty are incomplete.
|
|
188
|
+
|
|
189
|
+
## References
|
|
190
|
+
|
|
191
|
+
- [matplotlib Documentation](https://matplotlib.org/stable/) -- Official reference
|
|
192
|
+
- [seaborn Documentation](https://seaborn.pydata.org/) -- Statistical visualization
|
|
193
|
+
- [plotly Documentation](https://plotly.com/python/) -- Interactive charts
|
|
194
|
+
- [Scientific Visualization: Python + Matplotlib](https://github.com/rougier/scientific-visualization-book) -- Nicolas Rougier
|
|
195
|
+
- [Ten Simple Rules for Better Figures](https://journals.plos.org/ploscompbiol/article?id=10.1371/journal.pcbi.1003833) -- Rougier et al.
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: causal-inference-guide
|
|
3
|
+
description: "Causal inference methods including DiD, IV, RDD, and synthetic control"
|
|
4
|
+
metadata:
|
|
5
|
+
openclaw:
|
|
6
|
+
emoji: "link"
|
|
7
|
+
category: "analysis"
|
|
8
|
+
subcategory: "econometrics"
|
|
9
|
+
keywords: ["difference-in-differences", "DiD", "causal inference", "instrumental variables", "IV estimation", "regression discontinuity"]
|
|
10
|
+
source: "wentor"
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
# Causal Inference Guide
|
|
14
|
+
|
|
15
|
+
A skill for applying quasi-experimental causal inference methods in observational research. Covers difference-in-differences, instrumental variables, regression discontinuity designs, and synthetic control methods with implementation code and diagnostic checks.
|
|
16
|
+
|
|
17
|
+
## Difference-in-Differences (DiD)
|
|
18
|
+
|
|
19
|
+
### Classic Two-Period DiD
|
|
20
|
+
|
|
21
|
+
```python
|
|
22
|
+
import numpy as np
|
|
23
|
+
import pandas as pd
|
|
24
|
+
import statsmodels.formula.api as smf
|
|
25
|
+
|
|
26
|
+
def did_estimation(df: pd.DataFrame, outcome: str, treatment: str,
|
|
27
|
+
post: str, covariates: list[str] = None) -> dict:
|
|
28
|
+
"""
|
|
29
|
+
Estimate a difference-in-differences model.
|
|
30
|
+
|
|
31
|
+
Args:
|
|
32
|
+
df: Panel DataFrame
|
|
33
|
+
outcome: Name of outcome variable column
|
|
34
|
+
treatment: Name of treatment group indicator (0/1)
|
|
35
|
+
post: Name of post-treatment period indicator (0/1)
|
|
36
|
+
covariates: Optional list of control variable names
|
|
37
|
+
"""
|
|
38
|
+
# Create interaction term
|
|
39
|
+
df = df.copy()
|
|
40
|
+
df['did'] = df[treatment] * df[post]
|
|
41
|
+
|
|
42
|
+
# Build formula
|
|
43
|
+
formula = f"{outcome} ~ {treatment} + {post} + did"
|
|
44
|
+
if covariates:
|
|
45
|
+
formula += ' + ' + ' + '.join(covariates)
|
|
46
|
+
|
|
47
|
+
model = smf.ols(formula, data=df).fit(cov_type='cluster',
|
|
48
|
+
cov_kwds={'groups': df.get('unit_id', df.index)})
|
|
49
|
+
|
|
50
|
+
return {
|
|
51
|
+
'did_estimate': model.params['did'],
|
|
52
|
+
'se': model.bse['did'],
|
|
53
|
+
'p_value': model.pvalues['did'],
|
|
54
|
+
'ci_95': (model.conf_int().loc['did', 0], model.conf_int().loc['did', 1]),
|
|
55
|
+
'r_squared': model.rsquared,
|
|
56
|
+
'n_obs': model.nobs,
|
|
57
|
+
'interpretation': (
|
|
58
|
+
f"The treatment effect is {model.params['did']:.3f} "
|
|
59
|
+
f"(SE = {model.bse['did']:.3f}, p = {model.pvalues['did']:.4f}). "
|
|
60
|
+
f"{'Statistically significant' if model.pvalues['did'] < 0.05 else 'Not significant'} "
|
|
61
|
+
f"at the 5% level."
|
|
62
|
+
)
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### Parallel Trends Test
|
|
67
|
+
|
|
68
|
+
The key identifying assumption. Test it with pre-treatment data:
|
|
69
|
+
|
|
70
|
+
```python
|
|
71
|
+
def test_parallel_trends(df: pd.DataFrame, outcome: str,
|
|
72
|
+
treatment: str, time: str,
|
|
73
|
+
treatment_period: int) -> dict:
|
|
74
|
+
"""
|
|
75
|
+
Test the parallel trends assumption using event study specification.
|
|
76
|
+
"""
|
|
77
|
+
df = df.copy()
|
|
78
|
+
pre_periods = sorted(df[df[time] < treatment_period][time].unique())
|
|
79
|
+
|
|
80
|
+
# Create period dummies interacted with treatment
|
|
81
|
+
for t in pre_periods:
|
|
82
|
+
df[f'pre_{t}'] = ((df[time] == t) & (df[treatment] == 1)).astype(int)
|
|
83
|
+
|
|
84
|
+
period_vars = [f'pre_{t}' for t in pre_periods[:-1]] # omit last pre-period (reference)
|
|
85
|
+
formula = f"{outcome} ~ {' + '.join(period_vars)} + C({time}) + C(unit_id)"
|
|
86
|
+
|
|
87
|
+
model = smf.ols(formula, data=df).fit()
|
|
88
|
+
|
|
89
|
+
# Joint F-test: all pre-treatment interactions = 0
|
|
90
|
+
f_test = model.f_test(' = '.join([f'{v} = 0' for v in period_vars]))
|
|
91
|
+
|
|
92
|
+
return {
|
|
93
|
+
'pre_period_coefficients': {v: model.params[v] for v in period_vars},
|
|
94
|
+
'f_statistic': f_test.fvalue[0][0],
|
|
95
|
+
'f_pvalue': f_test.pvalue,
|
|
96
|
+
'parallel_trends_hold': f_test.pvalue > 0.05,
|
|
97
|
+
'interpretation': (
|
|
98
|
+
'Parallel trends assumption supported (cannot reject joint null)'
|
|
99
|
+
if f_test.pvalue > 0.05
|
|
100
|
+
else 'WARNING: Parallel trends assumption may be violated'
|
|
101
|
+
)
|
|
102
|
+
}
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## Instrumental Variables (IV)
|
|
106
|
+
|
|
107
|
+
### Two-Stage Least Squares
|
|
108
|
+
|
|
109
|
+
```python
|
|
110
|
+
from linearmodels.iv import IV2SLS
|
|
111
|
+
|
|
112
|
+
def iv_estimation(df: pd.DataFrame, outcome: str, endogenous: str,
|
|
113
|
+
instrument: str, exogenous: list[str] = None) -> dict:
|
|
114
|
+
"""
|
|
115
|
+
Estimate an IV model using 2SLS.
|
|
116
|
+
|
|
117
|
+
Args:
|
|
118
|
+
outcome: Dependent variable
|
|
119
|
+
endogenous: Endogenous regressor
|
|
120
|
+
instrument: Instrumental variable
|
|
121
|
+
exogenous: List of exogenous control variables
|
|
122
|
+
"""
|
|
123
|
+
exog_formula = '1'
|
|
124
|
+
if exogenous:
|
|
125
|
+
exog_formula += ' + ' + ' + '.join(exogenous)
|
|
126
|
+
|
|
127
|
+
model = IV2SLS(
|
|
128
|
+
dependent=df[outcome],
|
|
129
|
+
exog=df[exogenous] if exogenous else None,
|
|
130
|
+
endog=df[[endogenous]],
|
|
131
|
+
instruments=df[[instrument]]
|
|
132
|
+
).fit(cov_type='robust')
|
|
133
|
+
|
|
134
|
+
# First-stage F-statistic
|
|
135
|
+
first_stage = smf.ols(f"{endogenous} ~ {instrument}", data=df).fit()
|
|
136
|
+
f_stat = first_stage.fvalue
|
|
137
|
+
|
|
138
|
+
return {
|
|
139
|
+
'iv_estimate': model.params[endogenous],
|
|
140
|
+
'se': model.std_errors[endogenous],
|
|
141
|
+
'p_value': model.pvalues[endogenous],
|
|
142
|
+
'first_stage_F': f_stat,
|
|
143
|
+
'weak_instrument': f_stat < 10, # Stock-Yogo rule of thumb
|
|
144
|
+
'interpretation': (
|
|
145
|
+
f"IV estimate: {model.params[endogenous]:.3f}. "
|
|
146
|
+
f"First-stage F = {f_stat:.1f} "
|
|
147
|
+
f"({'Strong' if f_stat >= 10 else 'WEAK'} instrument)."
|
|
148
|
+
)
|
|
149
|
+
}
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
### IV Diagnostic Checklist
|
|
153
|
+
|
|
154
|
+
1. **Relevance**: First-stage F > 10 (Stock & Yogo, 2005)
|
|
155
|
+
2. **Exclusion restriction**: Instrument affects outcome only through the endogenous variable (untestable, argue conceptually)
|
|
156
|
+
3. **Overidentification test**: Sargan/Hansen J-test when you have more instruments than endogenous variables
|
|
157
|
+
|
|
158
|
+
## Regression Discontinuity Design (RDD)
|
|
159
|
+
|
|
160
|
+
```python
|
|
161
|
+
def rdd_estimation(df: pd.DataFrame, outcome: str, running_var: str,
|
|
162
|
+
cutoff: float, bandwidth: float = None) -> dict:
|
|
163
|
+
"""
|
|
164
|
+
Sharp regression discontinuity design estimation.
|
|
165
|
+
"""
|
|
166
|
+
df = df.copy()
|
|
167
|
+
df['centered'] = df[running_var] - cutoff
|
|
168
|
+
df['treated'] = (df[running_var] >= cutoff).astype(int)
|
|
169
|
+
|
|
170
|
+
if bandwidth is None:
|
|
171
|
+
bandwidth = df['centered'].std() # simple default
|
|
172
|
+
|
|
173
|
+
# Restrict to bandwidth
|
|
174
|
+
local = df[df['centered'].abs() <= bandwidth]
|
|
175
|
+
|
|
176
|
+
# Local linear regression
|
|
177
|
+
formula = f"{outcome} ~ treated * centered"
|
|
178
|
+
model = smf.ols(formula, data=local).fit(cov_type='HC1')
|
|
179
|
+
|
|
180
|
+
return {
|
|
181
|
+
'rdd_estimate': model.params['treated'],
|
|
182
|
+
'se': model.bse['treated'],
|
|
183
|
+
'p_value': model.pvalues['treated'],
|
|
184
|
+
'bandwidth': bandwidth,
|
|
185
|
+
'n_obs': len(local),
|
|
186
|
+
'n_treated': local['treated'].sum(),
|
|
187
|
+
'n_control': len(local) - local['treated'].sum()
|
|
188
|
+
}
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
## Best Practices
|
|
192
|
+
|
|
193
|
+
- Always visualize your data: plot outcome trends over time (DiD), first-stage relationships (IV), or running variable distributions (RDD)
|
|
194
|
+
- Report robustness checks: varying bandwidths, alternative specifications, placebo tests
|
|
195
|
+
- Use cluster-robust standard errors at the appropriate level (usually the treatment unit level)
|
|
196
|
+
- Be transparent about identifying assumptions and potential violations
|
|
197
|
+
- Pre-register your analysis plan when possible to avoid p-hacking concerns
|