Skip to content

The Two Stages Procedure of Meir and Gorfine (2023) - Exact

Source code in pydts/fitters.py
class TwoStagesFitterExact(TwoStagesFitter):

    def __init__(self):
        super().__init__()
        self.beta_models_params_attr = 'params'

    def _fit_event_beta(self, expanded_df, event, model=ConditionalLogit, model_kwargs={}, model_fit_kwargs={}):
        # Model fitting for conditional estimation of Beta_j for specific event
        if isinstance(self.covariates, dict):
            _covs = self.covariates[event]
        else:
            _covs = self.covariates

        beta_j_model = ConditionalLogit(endog=expanded_df[f'j_{event}'],
                                        exog=expanded_df[_covs],
                                        groups=expanded_df[self.duration_col],
                                        **model_kwargs)

        if ('alpha' in model_fit_kwargs.keys()):
            # Use 0 <= L1_wt <= 1 parameter to switch between L2 (L1_wt = 0) and L1 (L1_wt = 1) or elastic net.
            # alpha is the the penalty weight.
            beta_j_model = beta_j_model.fit_regularized(**model_fit_kwargs)
        else:
            beta_j_model = beta_j_model.fit(**model_fit_kwargs)

        return beta_j_model

    def get_beta_SE(self):
        """
        This function returns the Beta coefficients and their Standard Errors for all the events.

        Returns:
            se_df (pandas.DataFrame): Beta coefficients and Standard Errors Dataframe

        """

        full_table = pd.DataFrame()
        for event in self.events:
            if isinstance(self.beta_models[event], RegularizedResultsWrapper):
                _p = self.beta_models[event].params.copy()
                _p.name = 'coef'
                full_table = pd.concat([full_table,
                                        pd.concat([_p], keys=[event], axis=1)],
                                       axis=1)
            else:
                summary = self.beta_models[event].summary()
                summary_df = pd.DataFrame([x.split(',') for x in summary.tables[1].as_csv().split('\n')])
                summary_df.columns = summary_df.iloc[0]
                summary_df = summary_df.iloc[1:].set_index(summary_df.columns[0])
                summary_df.columns = pd.MultiIndex.from_product([[event], summary_df.columns])
                full_table = pd.concat([full_table, summary_df.iloc[-len(self.covariates):]], axis=1)
        return full_table

get_beta_SE(self) ยค

This function returns the Beta coefficients and their Standard Errors for all the events.

Returns:

Type Description
se_df (pandas.DataFrame)

Beta coefficients and Standard Errors Dataframe

Source code in pydts/fitters.py
def get_beta_SE(self):
    """
    This function returns the Beta coefficients and their Standard Errors for all the events.

    Returns:
        se_df (pandas.DataFrame): Beta coefficients and Standard Errors Dataframe

    """

    full_table = pd.DataFrame()
    for event in self.events:
        if isinstance(self.beta_models[event], RegularizedResultsWrapper):
            _p = self.beta_models[event].params.copy()
            _p.name = 'coef'
            full_table = pd.concat([full_table,
                                    pd.concat([_p], keys=[event], axis=1)],
                                   axis=1)
        else:
            summary = self.beta_models[event].summary()
            summary_df = pd.DataFrame([x.split(',') for x in summary.tables[1].as_csv().split('\n')])
            summary_df.columns = summary_df.iloc[0]
            summary_df = summary_df.iloc[1:].set_index(summary_df.columns[0])
            summary_df.columns = pd.MultiIndex.from_product([[event], summary_df.columns])
            full_table = pd.concat([full_table, summary_df.iloc[-len(self.covariates):]], axis=1)
    return full_table