@@ -59,10 +59,13 @@ def idata(self):
59
59
60
60
return self .model .idata
61
61
62
- def print_coefficients (self ) -> None :
62
+ def print_coefficients (self , round_to = None ) -> None :
63
63
"""
64
64
Prints the model coefficients
65
65
66
+ :param round_to:
67
+ Number of decimals used to round results. Defaults to 2. Use "None" to return raw numbers.
68
+
66
69
Example
67
70
--------
68
71
>>> import causalpy as cp
@@ -80,13 +83,13 @@ def print_coefficients(self) -> None:
80
83
... "progressbar": False
81
84
... }),
82
85
... )
83
- >>> result.print_coefficients() # doctest: +NUMBER
86
+ >>> result.print_coefficients(round_to=1 ) # doctest: +NUMBER
84
87
Model coefficients:
85
- Intercept 1.0 , 94% HDI [1.0, 1. 1]
86
- post_treatment[T.True] 0.9 , 94% HDI [0.9, 1.0 ]
87
- group 0.1 , 94% HDI [0.0 , 0.2]
88
+ Intercept 1, 94% HDI [1, 1]
89
+ post_treatment[T.True] 1 , 94% HDI [0.9, 1]
90
+ group 0.2 , 94% HDI [0.09 , 0.2]
88
91
group:post_treatment[T.True] 0.5, 94% HDI [0.4, 0.6]
89
- sigma 0.0 , 94% HDI [0.0 , 0.1]
92
+ sigma 0.08 , 94% HDI [0.07 , 0.1]
90
93
"""
91
94
print ("Model coefficients:" )
92
95
coeffs = az .extract (self .idata .posterior , var_names = "beta" )
@@ -95,13 +98,13 @@ def print_coefficients(self) -> None:
95
98
for name in self .labels :
96
99
coeff_samples = coeffs .sel (coeffs = name )
97
100
print (
98
- f"{ name : <30} { coeff_samples .mean ().data :.2f } , 94% HDI [{ coeff_samples .quantile (0.03 ).data :.2f } , { coeff_samples .quantile (1 - 0.03 ).data :.2f } ]" # noqa: E501
101
+ f"{ name : <30} { round_num ( coeff_samples .mean ().data , round_to ) } , 94% HDI [{ round_num ( coeff_samples .quantile (0.03 ).data , round_to ) } , { round_num ( coeff_samples .quantile (1 - 0.03 ).data , round_to ) } ]" # noqa: E501
99
102
)
100
103
# add coeff for measurement std
101
104
coeff_samples = az .extract (self .model .idata .posterior , var_names = "sigma" )
102
105
name = "sigma"
103
106
print (
104
- f"{ name : <30} { coeff_samples .mean ().data :.2f } , 94% HDI [{ coeff_samples .quantile (0.03 ).data :.2f } , { coeff_samples .quantile (1 - 0.03 ).data :.2f } ]" # noqa: E501
107
+ f"{ name : <30} { round_num ( coeff_samples .mean ().data , round_to ) } , 94% HDI [{ round_num ( coeff_samples .quantile (0.03 ).data , round_to ) } , { round_num ( coeff_samples .quantile (1 - 0.03 ).data , round_to ) } ]" # noqa: E501
105
108
)
106
109
107
110
@@ -138,7 +141,7 @@ class PrePostFit(ExperimentalDesign):
138
141
... }
139
142
... ),
140
143
... )
141
- >>> result.summary() # doctest: +NUMBER
144
+ >>> result.summary(round_to=1 ) # doctest: +NUMBER
142
145
==================================Pre-Post Fit==================================
143
146
Formula: actual ~ 0 + a + g
144
147
Model coefficients:
@@ -231,7 +234,7 @@ def plot(self, counterfactual_label="Counterfactual", round_to=None, **kwargs):
231
234
Plot the results
232
235
233
236
:param round_to:
234
- Number of decimals used to round results. Defaults to 2. Use "none " to return raw numbers.
237
+ Number of decimals used to round results. Defaults to 2. Use "None " to return raw numbers.
235
238
"""
236
239
fig , ax = plt .subplots (3 , 1 , sharex = True , figsize = (7 , 8 ))
237
240
@@ -331,15 +334,18 @@ def plot(self, counterfactual_label="Counterfactual", round_to=None, **kwargs):
331
334
332
335
return fig , ax
333
336
334
- def summary (self ) -> None :
337
+ def summary (self , round_to = None ) -> None :
335
338
"""
336
339
Print text output summarising the results
340
+
341
+ :param round_to:
342
+ Number of decimals used to round results. Defaults to 2. Use "None" to return raw numbers.
337
343
"""
338
344
339
345
print (f"{ self .expt_type :=^80} " )
340
346
print (f"Formula: { self .formula } " )
341
347
# TODO: extra experiment specific outputs here
342
- self .print_coefficients ()
348
+ self .print_coefficients (round_to )
343
349
344
350
345
351
class InterruptedTimeSeries (PrePostFit ):
@@ -420,7 +426,7 @@ def plot(self, plot_predictors=False, **kwargs):
420
426
"""Plot the results
421
427
422
428
:param round_to:
423
- Number of decimals used to round results. Defaults to 2. Use "none " to return raw numbers.
429
+ Number of decimals used to round results. Defaults to 2. Use "None " to return raw numbers.
424
430
"""
425
431
fig , ax = super ().plot (counterfactual_label = "Synthetic control" , ** kwargs )
426
432
if plot_predictors :
@@ -589,7 +595,7 @@ def plot(self, round_to=None):
589
595
"""Plot the results.
590
596
591
597
:param round_to:
592
- Number of decimals used to round results. Defaults to 2. Use "none " to return raw numbers.
598
+ Number of decimals used to round results. Defaults to 2. Use "None " to return raw numbers.
593
599
"""
594
600
fig , ax = plt .subplots ()
595
601
@@ -728,17 +734,19 @@ def _causal_impact_summary_stat(self, round_to=None) -> str:
728
734
causal_impact = f"{ round_num (self .causal_impact .mean (), round_to )} , "
729
735
return f"Causal impact = { causal_impact + ci } "
730
736
731
- def summary (self ) -> None :
737
+ def summary (self , round_to = None ) -> None :
732
738
"""
733
- Print text output summarising the results
739
+ Print text output summarising the results.
740
+
741
+ :param round_to:
742
+ Number of decimals used to round results. Defaults to 2. Use "None" to return raw numbers.
734
743
"""
735
744
736
745
print (f"{ self .expt_type :=^80} " )
737
746
print (f"Formula: { self .formula } " )
738
747
print ("\n Results:" )
739
- # TODO: extra experiment specific outputs here
740
- print (self ._causal_impact_summary_stat ())
741
- self .print_coefficients ()
748
+ print (round_num (self ._causal_impact_summary_stat (), round_to ))
749
+ self .print_coefficients (round_to )
742
750
743
751
744
752
class RegressionDiscontinuity (ExperimentalDesign ):
@@ -894,7 +902,7 @@ def plot(self, round_to=None):
894
902
Plot the results
895
903
896
904
:param round_to:
897
- Number of decimals used to round results. Defaults to 2. Use "none " to return raw numbers.
905
+ Number of decimals used to round results. Defaults to 2. Use "None " to return raw numbers.
898
906
"""
899
907
fig , ax = plt .subplots ()
900
908
# Plot raw data
@@ -943,9 +951,12 @@ def plot(self, round_to=None):
943
951
)
944
952
return fig , ax
945
953
946
- def summary (self ) -> None :
954
+ def summary (self , round_to : None ) -> None :
947
955
"""
948
956
Print text output summarising the results
957
+
958
+ :param round_to:
959
+ Number of decimals used to round results. Defaults to 2. Use "None" to return raw numbers.
949
960
"""
950
961
951
962
print (f"{ self .expt_type :=^80} " )
@@ -954,9 +965,9 @@ def summary(self) -> None:
954
965
print (f"Threshold on running variable: { self .treatment_threshold } " )
955
966
print ("\n Results:" )
956
967
print (
957
- f"Discontinuity at threshold = { self .discontinuity_at_threshold .mean ():.2f } "
968
+ f"Discontinuity at threshold = { round_num ( self .discontinuity_at_threshold .mean (), round_to ) } "
958
969
)
959
- self .print_coefficients ()
970
+ self .print_coefficients (round_to )
960
971
961
972
962
973
class RegressionKink (ExperimentalDesign ):
@@ -1111,7 +1122,7 @@ def plot(self, round_to=None):
1111
1122
Plot the results
1112
1123
1113
1124
:param round_to:
1114
- Number of decimals used to round results. Defaults to 2. Use "none " to return raw numbers.
1125
+ Number of decimals used to round results. Defaults to 2. Use "None " to return raw numbers.
1115
1126
"""
1116
1127
fig , ax = plt .subplots ()
1117
1128
# Plot raw data
@@ -1160,9 +1171,12 @@ def plot(self, round_to=None):
1160
1171
)
1161
1172
return fig , ax
1162
1173
1163
- def summary (self ) -> None :
1174
+ def summary (self , round_to = None ) -> None :
1164
1175
"""
1165
1176
Print text output summarising the results
1177
+
1178
+ :param round_to:
1179
+ Number of decimals used to round results. Defaults to 2. Use "None" to return raw numbers.
1166
1180
"""
1167
1181
1168
1182
print (
@@ -1173,10 +1187,10 @@ def summary(self) -> None:
1173
1187
Kink point on running variable: { self .kink_point }
1174
1188
1175
1189
Results:
1176
- Change in slope at kink point = { self .gradient_change .mean ():.2f }
1190
+ Change in slope at kink point = { round_num ( self .gradient_change .mean (), round_to ) }
1177
1191
"""
1178
1192
)
1179
- self .print_coefficients ()
1193
+ self .print_coefficients (round_to )
1180
1194
1181
1195
1182
1196
class PrePostNEGD (ExperimentalDesign ):
@@ -1213,17 +1227,17 @@ class PrePostNEGD(ExperimentalDesign):
1213
1227
... }
1214
1228
... )
1215
1229
... )
1216
- >>> result.summary() # doctest: +NUMBER
1230
+ >>> result.summary(round_to=1 ) # doctest: +NUMBER
1217
1231
==================Pretest/posttest Nonequivalent Group Design===================
1218
1232
Formula: post ~ 1 + C(group) + pre
1219
1233
<BLANKLINE>
1220
1234
Results:
1221
- Causal impact = 1.8 , $CI_{94%}$[1.7 , 2.1 ]
1235
+ Causal impact = 2 , $CI_{94%}$[2 , 2]
1222
1236
Model coefficients:
1223
- Intercept -0.4 , 94% HDI [-1. 1, 0.2]
1224
- C(group)[T.1] 1.8 , 94% HDI [1.6 , 2.0 ]
1225
- pre 1.0 , 94% HDI [0.9, 1. 1]
1226
- sigma 0.5, 94% HDI [0.4 , 0.5 ]
1237
+ Intercept -0.5 , 94% HDI [-1, 0.2]
1238
+ C(group)[T.1] 2 , 94% HDI [2 , 2]
1239
+ pre 1, 94% HDI [1, 1]
1240
+ sigma 0.5, 94% HDI [0.5 , 0.6 ]
1227
1241
"""
1228
1242
1229
1243
def __init__ (
@@ -1304,7 +1318,7 @@ def plot(self, round_to=None):
1304
1318
"""Plot the results
1305
1319
1306
1320
:param round_to:
1307
- Number of decimals used to round results. Defaults to 2. Use "none " to return raw numbers.
1321
+ Number of decimals used to round results. Defaults to 2. Use "None " to return raw numbers.
1308
1322
"""
1309
1323
fig , ax = plt .subplots (
1310
1324
2 , 1 , figsize = (7 , 9 ), gridspec_kw = {"height_ratios" : [3 , 1 ]}
@@ -1362,20 +1376,23 @@ def _causal_impact_summary_stat(self, round_to) -> str:
1362
1376
r"$CI_{94%}$"
1363
1377
+ f"[{ round_num (percentiles [0 ], round_to )} , { round_num (percentiles [1 ], round_to )} ]"
1364
1378
)
1365
- causal_impact = f"{ self .causal_impact .mean ():.2f } , "
1379
+ causal_impact = f"{ round_num ( self .causal_impact .mean (), round_to ) } , "
1366
1380
return f"Causal impact = { causal_impact + ci } "
1367
1381
1368
1382
def summary (self , round_to = None ) -> None :
1369
1383
"""
1370
1384
Print text output summarising the results
1385
+
1386
+ :param round_to:
1387
+ Number of decimals used to round results. Defaults to 2. Use "None" to return raw numbers.
1371
1388
"""
1372
1389
1373
1390
print (f"{ self .expt_type :=^80} " )
1374
1391
print (f"Formula: { self .formula } " )
1375
1392
print ("\n Results:" )
1376
1393
# TODO: extra experiment specific outputs here
1377
1394
print (self ._causal_impact_summary_stat (round_to ))
1378
- self .print_coefficients ()
1395
+ self .print_coefficients (round_to )
1379
1396
1380
1397
def _get_treatment_effect_coeff (self ) -> str :
1381
1398
"""Find the beta regression coefficient corresponding to the
@@ -1452,7 +1469,6 @@ class InstrumentalVariable(ExperimentalDesign):
1452
1469
... formula=formula,
1453
1470
... model=InstrumentalVariableRegression(sample_kwargs=sample_kwargs),
1454
1471
... )
1455
-
1456
1472
"""
1457
1473
1458
1474
def __init__ (
0 commit comments