@@ -52,25 +52,6 @@ def __init__(self, model=None, **kwargs):
52
52
def idata (self ):
53
53
"""
54
54
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...
74
55
"""
75
56
76
57
return self .model .idata
@@ -127,7 +108,7 @@ class PrePostFit(ExperimentalDesign):
127
108
the pre-intervention data.
128
109
129
110
:param data:
130
- A pandas data frame
111
+ A pandas dataframe
131
112
:param treatment_time:
132
113
The time when treatment occured, should be in reference to the data index
133
114
:param formula:
@@ -153,6 +134,18 @@ class PrePostFit(ExperimentalDesign):
153
134
... }
154
135
... ),
155
136
... )
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]
156
149
"""
157
150
158
151
def __init__ (
@@ -237,10 +230,6 @@ def _input_validation(self, data, treatment_time):
237
230
def plot (self , counterfactual_label = "Counterfactual" , ** kwargs ):
238
231
"""
239
232
Plot the results
240
-
241
- Example
242
- --------
243
- >>> result.plot() # doctest: +SKIP
244
233
"""
245
234
fig , ax = plt .subplots (3 , 1 , sharex = True , figsize = (7 , 8 ))
246
235
@@ -343,38 +332,6 @@ def plot(self, counterfactual_label="Counterfactual", **kwargs):
343
332
def summary (self ) -> None :
344
333
"""
345
334
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]
378
335
"""
379
336
380
337
print (f"{ self .expt_type :=^80} " )
@@ -388,7 +345,7 @@ class InterruptedTimeSeries(PrePostFit):
388
345
A wrapper around PrePostFit class
389
346
390
347
:param data:
391
- A pandas data frame
348
+ A pandas dataframe
392
349
:param treatment_time:
393
350
The time when treatment occured, should be in reference to the data index
394
351
:param formula:
@@ -427,7 +384,7 @@ class SyntheticControl(PrePostFit):
427
384
"""A wrapper around the PrePostFit class
428
385
429
386
:param data:
430
- A pandas data frame
387
+ A pandas dataframe
431
388
:param treatment_time:
432
389
The time when treatment occured, should be in reference to the data index
433
390
:param formula:
@@ -477,7 +434,7 @@ class DifferenceInDifferences(ExperimentalDesign):
477
434
There is no pre/post intervention data distinction for DiD, we fit all the
478
435
data available.
479
436
:param data:
480
- A pandas data frame
437
+ A pandas dataframe
481
438
:param formula:
482
439
A statistical model formula
483
440
:param time_variable_name:
@@ -505,6 +462,18 @@ class DifferenceInDifferences(ExperimentalDesign):
505
462
... }
506
463
... )
507
464
... )
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]
508
477
"""
509
478
510
479
def __init__ (
@@ -625,12 +594,6 @@ def _input_validation(self):
625
594
def plot (self ):
626
595
"""Plot the results.
627
596
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
634
597
"""
635
598
fig , ax = plt .subplots ()
636
599
@@ -769,38 +732,6 @@ def _causal_impact_summary_stat(self) -> str:
769
732
def summary (self ) -> None :
770
733
"""
771
734
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]
804
735
"""
805
736
806
737
print (f"{ self .expt_type :=^80} " )
@@ -849,6 +780,20 @@ class RegressionDiscontinuity(ExperimentalDesign):
849
780
... ),
850
781
... treatment_threshold=0.5,
851
782
... )
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]
852
797
"""
853
798
854
799
def __init__ (
@@ -961,10 +906,6 @@ def _is_treated(self, x):
961
906
def plot (self ):
962
907
"""
963
908
Plot the results
964
-
965
- Example
966
- --------
967
- >>> result.plot() # doctest: +SKIP
968
909
"""
969
910
fig , ax = plt .subplots ()
970
911
# Plot raw data
@@ -1013,39 +954,6 @@ def plot(self):
1013
954
def summary (self ) -> None :
1014
955
"""
1015
956
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]
1049
957
"""
1050
958
1051
959
print (f"{ self .expt_type :=^80} " )
@@ -1064,7 +972,7 @@ class PrePostNEGD(ExperimentalDesign):
1064
972
A class to analyse data from pretest/posttest designs
1065
973
1066
974
:param data:
1067
- A pandas data frame
975
+ A pandas dataframe
1068
976
:param formula:
1069
977
A statistical model formula
1070
978
:param group_variable_name:
@@ -1092,6 +1000,17 @@ class PrePostNEGD(ExperimentalDesign):
1092
1000
... }
1093
1001
... )
1094
1002
... )
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]
1095
1014
"""
1096
1015
1097
1016
def __init__ (
@@ -1227,38 +1146,6 @@ def _causal_impact_summary_stat(self) -> str:
1227
1146
def summary (self ) -> None :
1228
1147
"""
1229
1148
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
-
1262
1149
"""
1263
1150
1264
1151
print (f"{ self .expt_type :=^80} " )
0 commit comments