Skip to content

Commit 8cc283e

Browse files
committed
remove redundencies, clean up some wording
1 parent df5ae62 commit 8cc283e

File tree

4 files changed

+115
-337
lines changed

4 files changed

+115
-337
lines changed

causalpy/data/simulate_data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def generate_synthetic_control_data(
4646
:param N:
4747
Number fo data points
4848
:param treatment_time:
49-
Index where treatment begins in the generated data frame
49+
Index where treatment begins in the generated dataframe
5050
:param grw_mu:
5151
Mean of Gaussian Random Walk
5252
:param grw_sigma:

causalpy/pymc_experiments.py

Lines changed: 54 additions & 167 deletions
Original file line numberDiff line numberDiff line change
@@ -52,25 +52,6 @@ def __init__(self, model=None, **kwargs):
5252
def idata(self):
5353
"""
5454
Access to the models InferenceData object
55-
56-
Example
57-
--------
58-
>>> import causalpy as cp
59-
>>> df = cp.load_data("did")
60-
>>> seed = 42
61-
>>> result = cp.pymc_experiments.DifferenceInDifferences(
62-
... df,
63-
... formula="y ~ 1 + group*post_treatment",
64-
... time_variable_name="t",
65-
... group_variable_name="group",
66-
... model=cp.pymc_models.LinearRegression(
67-
... sample_kwargs={"random_seed": seed, "progressbar": False}),
68-
... )
69-
>>> result.idata
70-
Inference data...
71-
>>> result.idata.posterior
72-
<xarray.Dataset>
73-
Dimensions...
7455
"""
7556

7657
return self.model.idata
@@ -127,7 +108,7 @@ class PrePostFit(ExperimentalDesign):
127108
the pre-intervention data.
128109
129110
:param data:
130-
A pandas data frame
111+
A pandas dataframe
131112
:param treatment_time:
132113
The time when treatment occured, should be in reference to the data index
133114
:param formula:
@@ -153,6 +134,18 @@ class PrePostFit(ExperimentalDesign):
153134
... }
154135
... ),
155136
... )
137+
>>> result.summary() # doctest: +NUMBER
138+
==================================Pre-Post Fit==================================
139+
Formula: actual ~ 0 + a + b + c + d + e + f + g
140+
Model coefficients:
141+
a 0.33, 94% HDI [0.30, 0.38]
142+
b 0.05, 94% HDI [0.01, 0.09]
143+
c 0.31, 94% HDI [0.26, 0.35]
144+
d 0.06, 94% HDI [0.01, 0.10]
145+
e 0.02, 94% HDI [0.00, 0.06]
146+
f 0.20, 94% HDI [0.12, 0.26]
147+
g 0.04, 94% HDI [0.00, 0.08]
148+
sigma 0.26, 94% HDI [0.22, 0.30]
156149
"""
157150

158151
def __init__(
@@ -237,10 +230,6 @@ def _input_validation(self, data, treatment_time):
237230
def plot(self, counterfactual_label="Counterfactual", **kwargs):
238231
"""
239232
Plot the results
240-
241-
Example
242-
--------
243-
>>> result.plot() # doctest: +SKIP
244233
"""
245234
fig, ax = plt.subplots(3, 1, sharex=True, figsize=(7, 8))
246235

@@ -343,38 +332,6 @@ def plot(self, counterfactual_label="Counterfactual", **kwargs):
343332
def summary(self) -> None:
344333
"""
345334
Print text output summarising the results
346-
347-
Example
348-
---------
349-
>>> import causalpy as cp
350-
>>> sc = cp.load_data("sc")
351-
>>> treatment_time = 70
352-
>>> seed = 42
353-
>>> result = cp.pymc_experiments.PrePostFit(
354-
... sc,
355-
... treatment_time,
356-
... formula="actual ~ 0 + a + b + c + d + e + f + g",
357-
... model=cp.pymc_models.WeightedSumFitter(
358-
... sample_kwargs={
359-
... "draws": 2000,
360-
... "target_accept": 0.95,
361-
... "random_seed": seed,
362-
... "progressbar": False,
363-
... }
364-
... ),
365-
... )
366-
>>> result.summary() # doctest: +NUMBER
367-
==================================Pre-Post Fit==================================
368-
Formula: actual ~ 0 + a + b + c + d + e + f + g
369-
Model coefficients:
370-
a 0.34, 94% HDI [0.30, 0.38]
371-
b 0.05, 94% HDI [0.01, 0.09]
372-
c 0.31, 94% HDI [0.26, 0.35]
373-
d 0.06, 94% HDI [0.01, 0.10]
374-
e 0.0, 94% HDI [0.0, 0.0]
375-
f 0.1, 94% HDI [0.1, 0.2]
376-
g 0.0, 94% HDI [0.0, 0.0]
377-
sigma 0.26, 94% HDI [0.22, 0.30]
378335
"""
379336

380337
print(f"{self.expt_type:=^80}")
@@ -388,7 +345,7 @@ class InterruptedTimeSeries(PrePostFit):
388345
A wrapper around PrePostFit class
389346
390347
:param data:
391-
A pandas data frame
348+
A pandas dataframe
392349
:param treatment_time:
393350
The time when treatment occured, should be in reference to the data index
394351
:param formula:
@@ -427,7 +384,7 @@ class SyntheticControl(PrePostFit):
427384
"""A wrapper around the PrePostFit class
428385
429386
:param data:
430-
A pandas data frame
387+
A pandas dataframe
431388
:param treatment_time:
432389
The time when treatment occured, should be in reference to the data index
433390
:param formula:
@@ -477,7 +434,7 @@ class DifferenceInDifferences(ExperimentalDesign):
477434
There is no pre/post intervention data distinction for DiD, we fit all the
478435
data available.
479436
:param data:
480-
A pandas data frame
437+
A pandas dataframe
481438
:param formula:
482439
A statistical model formula
483440
:param time_variable_name:
@@ -505,6 +462,18 @@ class DifferenceInDifferences(ExperimentalDesign):
505462
... }
506463
... )
507464
... )
465+
>>> result.summary() # doctest: +NUMBER
466+
===========================Difference in Differences============================
467+
Formula: y ~ 1 + group*post_treatment
468+
<BLANKLINE>
469+
Results:
470+
Causal impact = 0.5, $CI_{94%}$[0.4, 0.6]
471+
Model coefficients:
472+
Intercept 1.0, 94% HDI [1.0, 1.1]
473+
post_treatment[T.True] 0.9, 94% HDI [0.9, 1.0]
474+
group 0.1, 94% HDI [0.0, 0.2]
475+
group:post_treatment[T.True] 0.5, 94% HDI [0.4, 0.6]
476+
sigma 0.0, 94% HDI [0.0, 0.1]
508477
"""
509478

510479
def __init__(
@@ -625,12 +594,6 @@ def _input_validation(self):
625594
def plot(self):
626595
"""Plot the results.
627596
Creating the combined mean + HDI legend entries is a bit involved.
628-
629-
Example
630-
--------
631-
Assuming `result` is the result of a DiD experiment:
632-
633-
>>> result.plot() # doctest: +SKIP
634597
"""
635598
fig, ax = plt.subplots()
636599

@@ -769,38 +732,6 @@ def _causal_impact_summary_stat(self) -> str:
769732
def summary(self) -> None:
770733
"""
771734
Print text output summarising the results
772-
773-
Example
774-
--------
775-
>>> import causalpy as cp
776-
>>> df = cp.load_data("did")
777-
>>> seed = 42
778-
>>> result = cp.pymc_experiments.DifferenceInDifferences(
779-
... df,
780-
... formula="y ~ 1 + group*post_treatment",
781-
... time_variable_name="t",
782-
... group_variable_name="group",
783-
... model=cp.pymc_models.LinearRegression(
784-
... sample_kwargs={
785-
... "draws": 2000,
786-
... "target_accept": 0.95,
787-
... "random_seed": seed,
788-
... "progressbar": False,
789-
... }
790-
... )
791-
... )
792-
>>> result.summary() # doctest: +NUMBER
793-
===========================Difference in Differences============================
794-
Formula: y ~ 1 + group*post_treatment
795-
<BLANKLINE>
796-
Results:
797-
Causal impact = 0.5, $CI_{94%}$[0.4, 0.6]
798-
Model coefficients:
799-
Intercept 1.0, 94% HDI [1.0, 1.1]
800-
post_treatment[T.True] 0.9, 94% HDI [0.9, 1.0]
801-
group 0.1, 94% HDI [0.0, 0.2]
802-
group:post_treatment[T.True] 0.5, 94% HDI [0.4, 0.6]
803-
sigma 0.0, 94% HDI [0.0, 0.1]
804735
"""
805736

806737
print(f"{self.expt_type:=^80}")
@@ -849,6 +780,20 @@ class RegressionDiscontinuity(ExperimentalDesign):
849780
... ),
850781
... treatment_threshold=0.5,
851782
... )
783+
>>> result.summary() # doctest: +NUMBER
784+
============================Regression Discontinuity============================
785+
Formula: y ~ 1 + x + treated + x:treated
786+
Running variable: x
787+
Threshold on running variable: 0.5
788+
<BLANKLINE>
789+
Results:
790+
Discontinuity at threshold = 0.91
791+
Model coefficients:
792+
Intercept 0.09, 94% HDI [-0.00, 0.17]
793+
treated[T.True] 2.45, 94% HDI [1.66, 3.28]
794+
x 1.32, 94% HDI [1.14, 1.50]
795+
x:treated[T.True] -3.08, 94% HDI [-4.17, -2.05]
796+
sigma 0.36, 94% HDI [0.31, 0.41]
852797
"""
853798

854799
def __init__(
@@ -961,10 +906,6 @@ def _is_treated(self, x):
961906
def plot(self):
962907
"""
963908
Plot the results
964-
965-
Example
966-
--------
967-
>>> result.plot() # doctest: +SKIP
968909
"""
969910
fig, ax = plt.subplots()
970911
# Plot raw data
@@ -1013,39 +954,6 @@ def plot(self):
1013954
def summary(self) -> None:
1014955
"""
1015956
Print text output summarising the results
1016-
1017-
Example
1018-
--------
1019-
>>> import causalpy as cp
1020-
>>> df = cp.load_data("rd")
1021-
>>> seed = 42
1022-
>>> result = cp.pymc_experiments.RegressionDiscontinuity(
1023-
... df,
1024-
... formula="y ~ 1 + x + treated + x:treated",
1025-
... model=cp.pymc_models.LinearRegression(
1026-
... sample_kwargs={
1027-
... "draws": 2000,
1028-
... "target_accept": 0.95,
1029-
... "random_seed": seed,
1030-
... "progressbar": False,
1031-
... },
1032-
... ),
1033-
... treatment_threshold=0.5,
1034-
... )
1035-
>>> result.summary() # doctest: +NUMBER
1036-
============================Regression Discontinuity============================
1037-
Formula: y ~ 1 + x + treated + x:treated
1038-
Running variable: x
1039-
Threshold on running variable: 0.5
1040-
<BLANKLINE>
1041-
Results:
1042-
Discontinuity at threshold = 0.91
1043-
Model coefficients:
1044-
Intercept 0.0, 94% HDI [0.0, 0.1]
1045-
treated[T.True] 2.4, 94% HDI [1.6, 3.2]
1046-
x 1.32, 94% HDI [1.14, 1.50]
1047-
x:treated[T.True] -3.09, 94% HDI [-4.16, -2.03]
1048-
sigma 0.36, 94% HDI [0.31, 0.41]
1049957
"""
1050958

1051959
print(f"{self.expt_type:=^80}")
@@ -1064,7 +972,7 @@ class PrePostNEGD(ExperimentalDesign):
1064972
A class to analyse data from pretest/posttest designs
1065973
1066974
:param data:
1067-
A pandas data frame
975+
A pandas dataframe
1068976
:param formula:
1069977
A statistical model formula
1070978
:param group_variable_name:
@@ -1092,6 +1000,17 @@ class PrePostNEGD(ExperimentalDesign):
10921000
... }
10931001
... )
10941002
... )
1003+
>>> result.summary() # doctest: +NUMBER
1004+
==================Pretest/posttest Nonequivalent Group Design===================
1005+
Formula: post ~ 1 + C(group) + pre
1006+
<BLANKLINE>
1007+
Results:
1008+
Causal impact = 1.8, $CI_{94%}$[1.6, 2.0]
1009+
Model coefficients:
1010+
Intercept -0.4, 94% HDI [-1.2, 0.2]
1011+
C(group)[T.1] 1.8, 94% HDI [1.6, 2.0]
1012+
pre 1.0, 94% HDI [0.9, 1.1]
1013+
sigma 0.5, 94% HDI [0.4, 0.5]
10951014
"""
10961015

10971016
def __init__(
@@ -1227,38 +1146,6 @@ def _causal_impact_summary_stat(self) -> str:
12271146
def summary(self) -> None:
12281147
"""
12291148
Print text output summarising the results
1230-
1231-
Example
1232-
--------
1233-
>>> import causalpy as cp
1234-
>>> df = cp.load_data("anova1")
1235-
>>> seed = 42
1236-
>>> result = cp.pymc_experiments.PrePostNEGD(
1237-
... df,
1238-
... formula="post ~ 1 + C(group) + pre",
1239-
... group_variable_name="group",
1240-
... pretreatment_variable_name="pre",
1241-
... model=cp.pymc_models.LinearRegression(
1242-
... sample_kwargs={
1243-
... "draws": 2000,
1244-
... "target_accept": 0.95,
1245-
... "random_seed": seed,
1246-
... "progressbar": False,
1247-
... }
1248-
... )
1249-
... )
1250-
>>> result.summary() # doctest: +NUMBER
1251-
==================Pretest/posttest Nonequivalent Group Design===================
1252-
Formula: post ~ 1 + C(group) + pre
1253-
<BLANKLINE>
1254-
Results:
1255-
Causal impact = 1.8, $CI_{94%}$[1.6, 2.0]
1256-
Model coefficients:
1257-
Intercept -0.4, 94% HDI [-1.2, 0.2]
1258-
C(group)[T.1] 1.8, 94% HDI [1.6, 2.0]
1259-
pre 1.0, 94% HDI [0.9, 1.1]
1260-
sigma 0.5, 94% HDI [0.4, 0.5]
1261-
12621149
"""
12631150

12641151
print(f"{self.expt_type:=^80}")

0 commit comments

Comments
 (0)