-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
617 lines (475 loc) · 26.1 KB
/
model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
import pandas as pd
import statsmodels
import statsmodels.formula.api as smf
import statsmodels.api as sm
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
from scipy import stats
from typing import List, Dict
from linear_diagnostics import LinearRegDiagnostic
from sklearn.preprocessing import PolynomialFeatures
from statsmodels.tools.tools import add_constant
class BivariateModel():
'''Wrapper for `statsmodels.api` with additional methods for assumption checking.'''
def __init__(self,
data: pd.DataFrame,
*,
outcome_event: int,
predictor_event: int,
model_type: str) -> None:
'''Initializer for the BivariateModel class.
Parameters:
- data (`pd.DataFrame`): a DataFrame with outcome and predictor data
- outcome_event (`int`): the outcome event to be analyzed. Options: 400, 800, or 1500
- predictor_event (`int`): the predictor event being analyzed. Options: 400, 800, or 1500, exclusive of the outcome event
- model_type (`str`): the type of model to call from `statsmodels.formula.api`. Currently supports 'ols', 'quad', 'rlm', and 'quantreg'
'''
self.data = data
self.outcome_event = outcome_event
self.predictor_event = predictor_event
self.model_type = model_type
@property
def model(self):
'''Returns a fitted `statsmodels.api` model based on the specified `model_type` in the initializer. Compatible with other `statsmodels.api` attributes and methods.'''
assert self.model_type in smf.__all__ + ['quad'], 'Choose a valid `statsmodels.api` model'
y = self.data[f'time_{self.outcome_event}']
X = self.data[[f'time_{self.predictor_event}']]
match self.model_type:
case 'ols':
return sm.OLS(y, add_constant(X)).fit()
case 'quad':
X_quad = PolynomialFeatures(degree=2).fit_transform(X)
return sm.OLS(y, X_quad).fit()
case 'rlm':
return sm.RLM(y, add_constant(X)).fit()
case 'quantreg':
model = smf.quantreg(f'time_{self.outcome_event} ~ time_{self.predictor_event}', data=self.data)
quantiles = np.arange(0.05, 0.96, 0.1)
model_list = [self.fit_quantile(q=i, model=model) for i in quantiles]
model_df = pd.DataFrame(
model_list,
columns=[
'q',
'intercept',
f'time_{self.predictor_event}',
f'time_{self.predictor_event}_ll',
f'time_{self.predictor_event}_ul'
]
)
ols = smf.ols(f'time_{self.outcome_event} ~ time_{self.predictor_event}', data=self.data).fit()
ols_ci_b = ols.conf_int().loc[f'time_{self.predictor_event}'].tolist()
ols = dict(
a=ols.params['Intercept'],
b=ols.params[f'time_{self.predictor_event}'],
bll=ols_ci_b[0],
bul=ols_ci_b[1],
)
return model_df, ols
@property
def model_summary(self) -> statsmodels.iolib.summary.Summary:
'''Returns a summary of a `statsmodels.formula.api` model based on the specified `model_type` in the initializer'''
if self.model_type == 'quantreg':
return self.model[0]
else:
return self.model.summary()
def check_assumptions(self) -> None:
'''Check the assumptions of linear regression. That is: linearity, normally-distributed residuals, constant variance, and the model describes all observations.'''
if self.model_type in ['ols', 'quad']:
model_diagnostic = LinearRegDiagnostic(self.model)
model_diagnostic(context='seaborn-v0_8-whitegrid', high_leverage_threshold=True)
def plot_dist(self,
data: pd.DataFrame | None = None,
outcome_event: int | None = None,
predictor_event: int | None = None) -> None:
'''Plot a histogram with overlying KDE for 400m and 800m times in a pd.DataFrame.
Parameters:
- data (`pd.DataFrame`): a pd.DataFrame with 800m and 'predictor_event' times
- outcome_event (`int` | `None`): the outcome event being modelled
- predictor_event (`int` | `None`): the predictor event in the dataset
'''
sns.set_theme(style='whitegrid')
if data is None:
data = self.data
if predictor_event is None:
predictor_event = self.predictor_event
if outcome_event is None:
outcome_event = self.outcome_event
# Sturge's Rule
BINS = int(np.ceil(np.log2(len(data)) + 1))
plt.figure(figsize = (15, 5))
plt.suptitle(f'{predictor_event}m vs {outcome_event}m\nn={len(data):,}')
plt.subplot(1, 3, 1)
plt.scatter(x=data[f'time_{predictor_event}'], y=data[f'time_{outcome_event}'], alpha=0.5)
plt.xlabel(f'{predictor_event}m')
plt.ylabel(f'{outcome_event}m')
plt.subplot(1, 3, 2)
sns.histplot(data[f'time_{outcome_event}'], bins=BINS, kde=True)
plt.subplot(1, 3, 3)
sns.histplot(data[f'time_{predictor_event}'], bins=BINS, kde=True)
def plot_conf_int(self,
data: pd.DataFrame | None = None,
outcome_event: int | None = None,
predictor_event: int | None = None) -> None:
'''Plot a regression plot with 95% Confidence Intervals for a model
Parameters:
- data (`pd.DataFrame`): a pd.DataFrame with [outcome_event]m and [predictor_event]m times
- outcome_event (`int` | `None`): the outcome event being modelled
- predictor_event (`int` | `None`): the predictor event in the dataset
'''
sns.set_theme(style='whitegrid')
if data is None:
data = self.data
if outcome_event is None:
outcome_event = self.outcome_event
if predictor_event is None:
predictor_event = self.predictor_event
match self.model_type:
case 'ols':
plt.figure(figsize=(5, 5))
sns.regplot(data, x=f'time_{predictor_event}', y=f'time_{outcome_event}',
line_kws={'color': 'red'},
scatter_kws={'alpha': 0.3})
case 'quad':
plt.figure(figsize=(5, 5))
sns.regplot(data, x=f'time_{predictor_event}', y=f'time_{outcome_event}',
line_kws={'color': 'red'},
scatter_kws={'alpha': 0.3},
order=2)
case 'rlm':
plt.figure(figsize=(5, 5))
sns.regplot(data, x=f'time_{predictor_event}', y=f'time_{outcome_event}',
robust=True,
line_kws={'color': 'red'},
scatter_kws={'alpha': 0.3})
case _:
raise NotImplementedError('Confidence Intervals for this model type are not available.')
def fit_quantile(self, q: float, model: statsmodels.regression.quantile_regression.QuantReg) -> List[List[float]]:
'''Fit a linear model for a given quantile.
Parameters:
- q (float): the quantile to regress on
- model (statsmodels.regression.quantile_regression.QuantReg): The unfit instantiation of a Quantile Regression model
'''
results = model.fit(q=q)
return [q, results.params['Intercept'], results.params[f'time_{self.predictor_event}']] + \
results.conf_int().loc[f'time_{self.predictor_event}'].tolist()
def plot_quantiles_by_parameter(self, quantile_data: pd.DataFrame | None = None, ols_data: dict | None = None) -> None:
'''Docstring'''
if self.model_type != 'quantreg':
raise ValueError('Plotting parameter quantiles is only available for Quantile Regression (`quantreg`) models.')
sns.set_theme(style='whitegrid')
if quantile_data is None:
quantile_data = self.model[0]
if ols_data is None:
ols_data = self.model[1]
n = quantile_data.shape[0]
plt.figure(figsize=(5,5))
plt.title(f'Conditional Parameter Estimates across {self.outcome_event}m Quantiles')
p1 = plt.plot(quantile_data['q'], quantile_data[f'time_{self.predictor_event}'], color='black', label=f'Quantile Reg {self.predictor_event}m')
p2 = plt.plot(quantile_data['q'], quantile_data[f'time_{self.predictor_event}_ul'], linestyle='dotted', color='black')
p3 = plt.plot(quantile_data['q'], quantile_data[f'time_{self.predictor_event}_ll'], linestyle='dotted', color='black')
p4 = plt.plot(quantile_data['q'], [ols_data['b']] * n, color='red', label=f'OLS {self.predictor_event}m')
p5 = plt.plot(quantile_data['q'], [ols_data['bll']] * n, linestyle='dotted', color='red')
p6 = plt.plot(quantile_data['q'], [ols_data['bul']] * n, linestyle='dotted', color='red')
plt.ylabel(fr'$\beta_{{time_{{{self.predictor_event}}}}}$')
plt.xlabel(f'Quantiles of the conditional {self.outcome_event}m distribution')
plt.legend()
def predict_time(self,
time: float | str,
event: int | str | None = None) -> float:
'''Use the model's parameters to predict the average 800m time for a runner who runs a certain event in a certain time. No protection against extrapolation
Parameters:
- event (`int` | `str`): events to predict the outcome event's time from.
- time (`str`): time elapsed in the specified event. Format: 'm:ss.xx'
Returns:
- estimate (`float`): the estimated time according to the parameters
'''
if event is None:
event = self.predictor_event
# Grab coefficients
try:
beta_0 = self.model.params['Intercept']
except KeyError:
beta_0 = self.model.params['const']
match str(event).lower():
case '1500' | '1600' | 'mile':
beta_1_index = 'time_1500'
case _:
beta_1_index = f'time_{event}'
beta_1 = self.model.params[beta_1_index]
# Convert time to seconds
if isinstance(time, str):
time_sec = float(time.split(':')[0]) * 60 + float(time.split(':')[1])
else:
time_sec = time
# Add 1600m and Mile conversions to 1500m
if str(event) == '1600':
time_sec = time_sec * 0.9375
elif str(event) == 'mile':
time_sec = time_sec * 0.9321
return round(beta_0 + beta_1 * time_sec, 2)
class MultivariateModel():
'''Wrapper for `statsmodels.formula.api` with additional methods for assumption checking, plotting distributions, and using model parameters to predict a time.'''
def __init__(self,
data: pd.DataFrame,
*,
outcome_event: int,
predictor_events: List[int],
model_type: str) -> None:
'''Parameters:
- data (`pd.DataFrame`): a DataFrame with [outcome_event]m and [predictor_events]m data
- outcome_event (`int`): the outcome variable in the analysis. Select one element from the following list: [400, 800, 1500]
- predictor_events (`int`): the predictor events being analyzed. Any two event combination of [400, 800, 1500] exclusive of the outcome event
- model_type (`str`): the type of model to call from `statsmodels.formula.api`. Currently supports 'ols', 'rlm', 'quad', 'quantreg'
'''
self.data = data
self.model_type = model_type
self.outcome_event = outcome_event
self.predictor_events = predictor_events
def __call__(self):
match self.model_type:
case 'quantreg':
self.plot_quantiles_by_parameter()
return self.model_summary
case _:
if self.model_type not in ['quad']:
self.plot_partial_regressors()
self.check_assumptions(vif=False)
return self.model_summary
@property
def model(self):
'''Returns a fitted `statsmodels.formula.api` model based on the specified `model_type` in the initializer. Compatible with other `statsmodels.formula.api` attributes and methods. Currrently only includes OLS and RLM without interactions.'''
# TODO: #10 Convert from formula to regular, it should be easier to generalize that way
assert self.model_type in smf.__all__ + ['quad'], 'Choose a valid `statsmodels.formula.api` model'
predictor_formula = ' + '.join(['time_' + str(event) for event in self.predictor_events])
match self.model_type:
case 'ols':
return smf.ols(f'time_{self.outcome_event} ~ {predictor_formula}', data=self.data).fit()
case 'rlm':
return smf.rlm(f'time_{self.outcome_event} ~ {predictor_formula}', data=self.data).fit()
case 'quantreg':
model = smf.quantreg(f'time_{self.outcome_event} ~ {predictor_formula}', data=self.data)
# TODO: #4 Add option for different quantiles
quantiles = np.arange(0.05, 0.96, 0.1)
model_list = [self.fit_quantile(q=i, model=model) for i in quantiles]
# TODO: #3 Make columns more generalizable for more than two parameters
model_df = pd.DataFrame(
model_list,
columns=[
'q',
'intercept',
f'time_{self.predictor_events[0]}',
f'time_{self.predictor_events[1]}',
f'time_{self.predictor_events[0]}_ll',
f'time_{self.predictor_events[0]}_ul',
f'time_{self.predictor_events[1]}_ll',
f'time_{self.predictor_events[1]}_ul'
]
)
ols = smf.ols(f'time_{self.outcome_event} ~ {predictor_formula}', data=self.data).fit()
# TODO: #5 Make generalizable for more than 2 parameters
ols_ci_b = ols.conf_int().loc[f'time_{self.predictor_events[0]}'].tolist()
ols_ci_c = ols.conf_int().loc[f'time_{self.predictor_events[1]}'].tolist()
ols = dict(
a=ols.params['Intercept'],
b=ols.params[f'time_{self.predictor_events[0]}'],
c=ols.params[f'time_{self.predictor_events[1]}'],
bll=ols_ci_b[0],
bul=ols_ci_b[1],
cll=ols_ci_c[0],
cul=ols_ci_c[1],
)
return model_df, ols
case 'quad':
y = self.data[f'time_{self.outcome_event}']
X = self.data[[f'time_{i}' for i in self.predictor_events]]
X_poly = PolynomialFeatures(degree=2).fit_transform(X)
X_poly = add_constant(X_poly)
return sm.OLS(endog=y, exog=X_poly).fit()
@property
def model_summary(self) -> statsmodels.iolib.summary.Summary:
'''Returns a summary of a `statsmodels.formula.api` model based on the specified `model_type` in the initializer'''
match self.model_type:
case 'quantreg':
return self.model[0]
case 'quad':
# TODO: Add annotations for parameters (const, x1, x2, ...x_i)
return self.model.summary()
case _:
return self.model.summary()
def check_assumptions(self, **kwargs) -> None:
'''Check the assumptions of linear regression. That is: linearity, normally-distributed residuals, constant variance, and the model describes all observations.'''
kwargs.setdefault('vif', True)
if self.model_type in ['ols', 'quad']:
model_diagnostic = LinearRegDiagnostic(self.model)
return model_diagnostic(context='seaborn-v0_8-whitegrid', high_leverage_threshold=True, vif=kwargs.get('vif'))
def plot_partial_regressors(self) -> None:
'''Plot partial regression plots for the model'''
sns.set_theme(style='whitegrid')
fig = plt.figure(figsize=(8,8))
sm.graphics.plot_partregress_grid(self.model, grid=(2,2), fig=fig)
def plot_dist(self, **kwargs) -> None:
'''Plots the distributions of the outcome event and the predictor events.'''
sns.set_theme(style='whitegrid')
kwargs.setdefault('color', 'lightblue')
kwargs.setdefault('linecolor', 'black')
kwargs.setdefault('width', 0.3)
# Sturge's Rule for histograms
BINS: int = int(np.ceil(np.log2(len(self.data)) + 1))
NROWS: int = 1 + len(self.predictor_events)
NCOLS: int = 2
plt.figure(figsize = (10, 5 * NROWS))
plt.suptitle(f'Distributions of Outcome and Predictors')
# Outcome Distribution
plt.subplot(NROWS, NCOLS, 1)
sns.histplot(self.data[f'time_{self.outcome_event}'],
bins=BINS,
kde=True)
plt.subplot(NROWS, NCOLS, 2)
sns.boxplot(y=self.data[f'time_{self.outcome_event}'],
width=kwargs.get('width'),
color=kwargs.get('color'),
linecolor=kwargs.get('linecolor'))
# Predictor Distributions
for i in range(len(self.predictor_events)):
plt.subplot(NROWS, NCOLS, i*2 + 3)
sns.histplot(self.data[f'time_{self.predictor_events[i]}'],
bins=BINS,
kde=True)
plt.subplot(NROWS, NCOLS, i*2 + 4)
sns.boxplot(y=self.data[f'time_{self.predictor_events[i]}'],
width=kwargs.get('width'),
color=kwargs.get('color'),
linecolor=kwargs.get('linecolor'))
def fit_quantile(self,
q: float,
model: statsmodels.regression.quantile_regression.QuantReg) -> List[List[float]]:
'''Fit a linear model for a given quantile.
Parameters:
- q (float): the quantile to regress on
- model (statsmodels.regression.quantile_regression.QuantReg): The unfit instantiation of a Quantile Regression model'''
results = model.fit(q=q)
# TODO: #2 Make the return statement more generalizable for more parameters
return [q, results.params['Intercept'], results.params[f'time_{self.predictor_events[0]}'], results.params[f'time_{self.predictor_events[1]}']] + \
results.conf_int().loc[f'time_{self.predictor_events[0]}'].tolist() + \
results.conf_int().loc[f'time_{self.predictor_events[1]}'].tolist()
def plot_quantiles_by_parameter(self,
quantile_data: pd.DataFrame | None = None,
ols_data: dict | None = None,
title_specifier: str | None = None) -> None:
# TODO: #6 add docstring
# https://www.statsmodels.org/dev/examples/notebooks/generated/quantile_regression.html#Second-plot
# https://www.statsmodels.org/dev/generated/statsmodels.formula.api.quantreg.html#
'''Docstring'''
sns.set_theme(style='whitegrid')
if quantile_data is None:
quantile_data = self.model[0]
if ols_data is None:
ols_data = self.model[1]
if title_specifier is None:
title_specifier = ''
else:
title_specifier = title_specifier + ' | '
n = quantile_data.shape[0]
plt.figure(figsize=(12,5)) # TODO: #7 Make figsize, rest of plotting generalizable
plt.suptitle(f'Conditional Parameter Estimates across Quantiles\n{title_specifier}n = {len(self.data):,}')
plt.subplot(1, 2, 1)
p1 = plt.plot(quantile_data['q'], quantile_data[f'time_{self.predictor_events[0]}'], color='black', label=f'Quantile Reg {self.predictor_events[0]}m')
p2 = plt.plot(quantile_data['q'], quantile_data[f'time_{self.predictor_events[0]}_ul'], linestyle='dotted', color='black')
p3 = plt.plot(quantile_data['q'], quantile_data[f'time_{self.predictor_events[0]}_ll'], linestyle='dotted', color='black')
p4 = plt.plot(quantile_data['q'], [ols_data['b']] * n, color='red', label=f'OLS {self.predictor_events[0]}m')
p5 = plt.plot(quantile_data['q'], [ols_data['bll']] * n, linestyle='dotted', color='red')
p6 = plt.plot(quantile_data['q'], [ols_data['bul']] * n, linestyle='dotted', color='red')
plt.ylabel(fr'$\beta_{{time_{{{self.predictor_events[0]}}}}}$')
plt.xlabel(f'Quantiles of the conditional {self.outcome_event}m distribution')
plt.title(f'{self.predictor_events[0]}m')
plt.legend()
plt.subplot(1, 2, 2)
p7 = plt.plot(quantile_data['q'], quantile_data[f'time_{self.predictor_events[1]}'], color='blue', label=f'Quantile Reg {self.predictor_events[1]}m')
p8 = plt.plot(quantile_data['q'], quantile_data[f'time_{self.predictor_events[1]}_ul'], linestyle='dotted', color='blue')
p9 = plt.plot(quantile_data['q'], quantile_data[f'time_{self.predictor_events[1]}_ll'], linestyle='dotted', color='blue')
p10 = plt.plot(quantile_data['q'], [ols_data['c']] * n, color='red', label=f'OLS {self.predictor_events[1]}m')
p11 = plt.plot(quantile_data['q'], [ols_data['cll']] * n, linestyle='dotted', color='red')
p12 = plt.plot(quantile_data['q'], [ols_data['cul']] * n, linestyle='dotted', color='red')
plt.ylabel(fr'$\beta_{{time_{{{self.predictor_events[1]}}}}}$')
plt.xlabel(f'Quantiles of the conditional {self.outcome_event}m distribution')
plt.title(f'{self.predictor_events[1]}m')
plt.legend()
plt.show()
def predict_time(self,
times: List[str],
events: List[int] | List[str] = None) -> float:
'''Use the model's parameters to predict the average 800m time for a runner who runs a certain event in a certain time. No protection against extrapolation
Parameters:
- times (List[str]): time elapsed in the events specified in the events argument. The times must be in the same order as the events they correspond to in the events argument. The time format must follow: 'm:ss.xx'
- events (List[int] | List[str]): list of 2 events to predict the outcome event time. Options: '400', '800', '1600', 'mile'
Returns:
- estimate (float): the estimated time according to the parameters'''
if events is None:
events = self.predictor_events
# Grab coefficients
try:
beta_0 = self.model.params['Intercept']
except:
beta_0 = self.model.params['const']
match str(events[0]).lower():
case '400':
beta_1_index = 'time_400'
case '1500' | '1600' | 'mile':
beta_1_index = 'time_1500'
beta_1 = self.model.params[beta_1_index]
match str(events[1]).lower():
case '400':
beta_2_index = 'time_400'
case '1500' | '1600' | 'mile':
beta_2_index = 'time_1500'
beta_2 = self.model.params[beta_2_index]
# Convert time to seconds
time_1_sec = float(times[0].split(':')[0]) * 60 + float(times[0].split(':')[1])
time_2_sec = float(times[1].split(':')[0]) * 60 + float(times[1].split(':')[1])
# Add 1600m and Mile conversions to 1500m
if str(events[0]) == '1600':
time_1_sec = time_1_sec * 0.9375
elif str(events[0]) == 'mile':
time_1_sec = time_1_sec * 0.93205678835
if str(events[1]) == '1600':
time_2_sec = time_2_sec * 0.9375
elif str(events[1]) == 'mile':
time_2_sec = time_2_sec * 0.93205678835
return f'{self.outcome_event}m Prediction: {round(beta_0 + beta_1 * time_1_sec + beta_2 * time_2_sec, 2)} seconds'
def plot_bivariate_eda(data: pd.DataFrame, title: str, outcome_event: int, predictor_event: int, **kwargs) -> None:
'''Returns a scatter plot and two boxplots of the data in the `pd.DataFrame`
Parameters:
- data (pd.DataFrame): a `pd.DataFrame` of outcome_event and predictor_event data
- title (str): title for the group of plots
- outcome_event (int): the distance in meters of the outcome event of interest
- predictor_event (int): the distance in meters of the predictor event of interest'''
sns.set_theme(style = 'whitegrid')
kwargs.setdefault('color', 'lightblue')
kwargs.setdefault('linecolor', 'black')
kwargs.setdefault('thresh', 0.01)
plt.figure(figsize=(10, 10))
plt.suptitle(title)
plt.subplot(2, 2, 1)
plt.scatter(x=data[f'time_{predictor_event}'],
y=data[f'time_{outcome_event}'],
alpha = 0.3)
plt.xlabel(f'time_{predictor_event}')
plt.ylabel(f'time_{outcome_event}')
plt.subplot(2, 2, 2)
sns.kdeplot(x=data[f'time_{predictor_event}'],
y=data[f'time_{outcome_event}'],
thresh=kwargs.get('thresh'),
cmap='cividis')
plt.subplot(2, 2, 3)
sns.boxplot(y=data[f'time_{outcome_event}'],
width=0.3,
color=kwargs.get('color'),
linecolor=kwargs.get('linecolor'))
plt.title(f'{outcome_event}m')
plt.subplot(2, 2, 4)
sns.boxplot(y=data[f'time_{predictor_event}'],
width=0.3,
color=kwargs.get('color'),
linecolor=kwargs.get('linecolor'))
plt.title(f'{predictor_event}m')