Add "reduced" parameter for IntervalCensoredCox

This commit is contained in:
RunasSudo 2023-04-22 17:08:36 +10:00
parent 7746ca275e
commit 3c22fe4197
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
1 changed files with 11 additions and 2 deletions

View File

@ -103,6 +103,7 @@ def regress(
nan_policy='warn',
exposure=None,
method=None, maxiter=None, start_params=None,
reduced=None,
bool_baselevels=False, exp=None
):
"""
@ -123,6 +124,7 @@ def regress(
:param method: See statsmodels *model.fit*
:param maxiter: See statsmodels *model.fit*
:param start_params: See statsmodels *model.fit*
:param reduced: See :meth:`yli.IntervalCensoredCox`
:param bool_baselevels: Show reference categories for boolean independent variables even if reference category is *False*
:type bool_baselevels: bool
:param exp: Report exponentiated parameters rather than raw parameters, default (*None*) is to autodetect based on *model_class*
@ -149,6 +151,8 @@ def regress(
fit_kwargs['maxiter'] = maxiter
if start_params is not None:
fit_kwargs['start_params'] = start_params
if reduced is not None:
fit_kwargs['reduced'] = reduced
# Fit model
result = model_class.fit(dmatrices[0], dmatrices[1], **fit_kwargs)
@ -689,7 +693,7 @@ class IntervalCensoredCox(RegressionModel):
self.lambda_ = None
@classmethod
def fit(cls, data_dep, data_ind):
def fit(cls, data_dep, data_ind, *, reduced=False):
if len(data_dep.columns) != 2:
raise ValueError('IntervalCensoredCox requires left and right times')
@ -703,13 +707,18 @@ class IntervalCensoredCox(RegressionModel):
result.nevents = np.isfinite(data_dep.iloc[:, 1]).sum()
result.dof_model = len(data_ind.columns)
# Prepare arguments
intcox_args = [config.hpstat_path, 'intcox', '-', '--output', 'json']
if reduced:
intcox_args.append('--reduced')
# Export data to CSV
csv_buf = io.StringIO()
data_dep.join(data_ind).to_csv(csv_buf, index=False)
csv_str = csv_buf.getvalue()
# Run intcens binary
proc = subprocess.run([config.hpstat_path, 'intcox', '-', '--output', 'json'], input=csv_str, stdout=subprocess.PIPE, stderr=None, encoding='utf-8', check=True)
proc = subprocess.run(intcox_args, input=csv_str, stdout=subprocess.PIPE, stderr=None, encoding='utf-8', check=True)
raw_result = json.loads(proc.stdout)
from IPython.display import clear_output