@tpmjs/tools-diff-in-diff
Estimate the causal effect of a treatment using difference-in-differences (DiD) methodology. Compares changes over time between treatment and control groups to isolate the treatment effect. Returns effect size, statistical significance, and interpretation.
Test @tpmjs/tools-diff-in-diff (diffInDiffTool) with AI-powered execution
0/2000 characters
Install this tool and use it with the AI SDK
npm install @tpmjs/tools-diff-in-diffpnpm add @tpmjs/tools-diff-in-diffyarn add @tpmjs/tools-diff-in-diffbun add @tpmjs/tools-diff-in-diffdeno add npm:@tpmjs/tools-diff-in-diffimport { diffInDiffTool } from '@tpmjs/tools-diff-in-diff';import { generateText } from 'ai';
import { openai } from '@ai-sdk/openai';
import { diffInDiffTool } from '@tpmjs/tools-diff-in-diff';
const result = await generateText({
model: openai('gpt-4o'),
tools: { diffInDiffTool },
prompt: 'Your prompt here...',
});
console.log(result.text);Available configuration options
treatmentBeforearrayOutcome values for treatment group before intervention
treatmentAfterarrayOutcome values for treatment group after intervention
controlBeforearrayOutcome values for control group before intervention
controlAfterarrayOutcome values for control group after intervention
confidenceLevelnumberConfidence level for interval (default: 0.95)
Schema extracted: 1/1/2026, 8:18:16 AM
Causal inference estimator for measuring treatment effects using before/after comparison with a control group.
npm install @tpmjs/tools-diff-in-diff
import { diffInDiffTool } from '@tpmjs/tools-diff-in-diff'; // Example: Evaluate impact of a policy intervention // Treatment group: Cities that implemented the policy // Control group: Cities that did not implement the policy const result = await diffInDiffTool.execute({ treatmentBefore: [100, 105, 98, 102], // Before policy treatmentAfter: [120, 125, 118, 122], // After policy controlBefore: [95, 100, 92, 98], // Before (no policy) controlAfter: [98, 103, 95, 101], // After (no policy) confidenceLevel: 0.95, }); console.log(result); // { // effect: 17.5, // Treatment caused 17.5 unit increase // standardError: 2.1, // tStatistic: 8.33, // pValue: 0.0001, // significant: true, // confidenceInterval: { // lower: 13.2, // upper: 21.8, // level: 0.95 // }, // interpretation: "The treatment effect is 17.5 (increased by 17.5 units)...", // groupMeans: { // treatmentBefore: 101.25, // treatmentAfter: 121.25, // controlBefore: 96.25, // controlAfter: 99.25 // }, // differences: { // treatmentDiff: 20.0, // Treatment group change // controlDiff: 3.0 // Control group change // } // }
The DiD estimator removes time-invariant confounders by differencing:
Formula: DiD = (T_after - T_before) - (C_after - C_before)
Where:
This double-differencing removes:
Key Assumption: Parallel trends - Without treatment, both groups would have changed similarly.
// States that raised minimum wage (treatment) // vs states that didn't (control) const result = await diffInDiffTool.execute({ treatmentBefore: [5.2, 5.5, 5.1, 5.4], // Employment before treatmentAfter: [5.1, 5.3, 5.0, 5.2], // Employment after controlBefore: [5.3, 5.4, 5.2, 5.5], controlAfter: [5.4, 5.5, 5.3, 5.6], }); // Effect tells us the causal impact on employment
A positive effect means treatment increased the outcome. A negative effect means treatment decreased the outcome.
Statistical significance (p < 0.05) suggests the effect is real, not due to chance.
MIT
Downloads/month
38
Quality Score