Beispiel #1
0
    def factor_score_dynamics_plot(
        self,
        factor,
        agg_method="mean",
        group=None,
        figsize=None,
        write_tex=False,
        dpi=200,
        save_path=None,
        width=None,
        height=None,
    ):

        if figsize is None:
            figsize = (12, 8)
        to_concat = []
        for period in self.periods:
            df = self.data_proc.score_df(
                periods=period,
                factors=factor,
                other_vars=[group, "__period__"],
                agg_method=agg_method,
            )
            to_concat.append(df)

        data = pd.concat(to_concat, axis=0, sort=True)

        fig, ax = plt.subplots(figsize=figsize)
        sns.pointplot(
            x="__period__",
            y=factor,
            hue=group,
            data=data,
            ax=ax,
            kind="bar",
            dodge=0.15,
            join=True,
            capsize=0.05,
        )
        sns.despine(fig=fig, ax=ax)

        base_title = "Factor Score Dynamics"
        title = title_text(base_title, periods="all", factors=factor)

        if write_tex is True:
            write_figure_tex_snippet(save_path, title, width=width, height=height)

        if save_path is not None:
            fig.savefig(save_path, dpi=dpi, bbox_inches="tight")

        return fig, ax
Beispiel #2
0
    def score_pairplot(
        self,
        periods="all",
        factors="all",
        agg_method="means",
        group=None,
        pair_kws=None,
        save_path=None,
        dpi=200,
        write_tex=False,
        width=None,
        height=None,
    ):
        pair_kws = {} if pair_kws is None else pair_kws
        if write_tex is True:
            assert (
                save_path is not None
            ), "To write a tex file, please provide a save_path"

        if group is None:
            other_vars = []
        else:
            other_vars = [group]

        if width is None and height is None:
            width = 1

        df = self.data_proc.score_df(
            periods=periods,
            factors=factors,
            other_vars=other_vars,
            agg_method=agg_method,
        )

        kwargs = self._basic_pairplot_args()
        kwargs.update(pair_kws)

        variables = [col for col in df.columns if col != group]

        grid = sns.pairplot(data=df, hue=group, vars=variables, **kwargs)

        base_title = "Joint Distribution of Factor Scores"
        title = title_text(base_title, periods=periods, factors=factors)

        if write_tex is True:
            write_figure_tex_snippet(save_path, title, width=width, height=height)

        if save_path is not None:
            grid.savefig(save_path, dpi=dpi, bbox_inches="tight")
        return grid
Beispiel #3
0
    def score_heatmap(
        self,
        periods="all",
        factors="all",
        agg_method="means",
        figsize=None,
        heatmap_kws=None,
        save_path=None,
        dpi=200,
        write_tex=False,
        width=None,
        height=None,
    ):
        heatmap_kws = {} if heatmap_kws is None else heatmap_kws
        if write_tex is True:
            assert (
                save_path is not None
            ), "To write a tex file, please provide a save_path"

        df = self.data_proc.score_df(
            periods=periods, factors=factors, order="by_factor", agg_method=agg_method
        )
        corr = df.corr()

        if width is None and height is None:
            if len(corr) <= 5:
                width = 0.5
            elif len(corr) <= 9:
                width = 0.8
            else:
                width = 1

        if figsize is None:
            figsize = (len(corr), 0.8 * len(corr))

        kwargs = self._basic_heatmap_args()
        kwargs.update(heatmap_kws)
        fig, ax = plt.subplots(figsize=figsize)
        ax = sns.heatmap(data=corr, ax=ax, **kwargs)

        base_title = "Correlations of Factor Scores"
        title = title_text(basic_name=base_title, periods=periods, factors=factors)

        if save_path is not None:
            fig.savefig(save_path, dpi=dpi, bbox_inches="tight")

        if write_tex is True:
            write_figure_tex_snippet(save_path, title, width=width, height=height)
        return fig, ax
Beispiel #4
0
    def autoregression_plot(
        self,
        period,
        factor,
        agg_method="mean",
        reg_kws=None,
        figsize=(10, 5),
        save_path=None,
        dpi=200,
        write_tex=False,
        width=None,
        height=None,
    ):
        reg_kws = {} if reg_kws is None else reg_kws
        if write_tex is True:
            assert (
                save_path is not None
            ), "To write a tex file, please provide a save_path"
        if width is None and height is None:
            width = 0.8

        kwargs = self._basic_regplot_args()
        kwargs.update(reg_kws)

        df = self.data_proc.score_df(
            factors=factor, periods=[period, period + 1], agg_method=agg_method
        )

        fig, ax = plt.subplots(figsize=figsize)

        x = f"{factor}_{period}"
        y = "{}_{}".format(factor, period + 1)

        sns.regplot(x=x, y=y, data=df, ax=ax, **kwargs)

        base_title = "Autoregression Plot"
        title = title_text(base_title, periods=period, factors=factor)

        if write_tex is True:
            write_figure_tex_snippet(save_path, title, width=width, height=height)

        if save_path is not None:
            fig.savefig(save_path, dpi=dpi, bbox_inches="tight")

        return fig, ax
Beispiel #5
0
    def score_regression_table(
        self,
        periods=None,
        stages=None,
        controls=None,
        agg_method="mean",
        write_tex=False,
        save_path=None,
    ):
        controls = [] if controls is None else controls
        assert (
            periods is None or stages is None
        ), "You cannot specify periods and stages for a score regression."

        assert not (
            periods is None and stages is None
        ), "You have to specify periods or stages for a score regression"

        if write_tex is True:
            assert save_path is not None

        if periods == "all":
            periods = self.periods[:-1]

        if stages == "all":
            stages = self.stages

        if periods is not None:
            time = periods

        if isinstance(periods, int) or isinstance(periods, float):
            periods = [periods]

        if isinstance(stages, int) or isinstance(stages, float):
            stages = [stages]

        if periods is not None:
            time = periods
            time_name = "period"
        else:
            time = stages
            time_name = "stage"

        results = []
        for t in time:
            for factor in self.factors:
                ind = self.factors.index(factor)
                trans_name = self.transition_names[ind]
                if trans_name != "constant":
                    mod_kwargs = {
                        "factor": factor,
                        "controls": controls,
                        "agg_method": agg_method,
                        time_name: t,
                    }

                    mod = self._score_regression_model(**mod_kwargs)
                    res = mod.fit()
                    res.name = factor
                    res.period = t
                    results.append(res)

        df = statsmodels_results_to_df(
            res_list=results, decimals=2, period_name=time_name.capitalize()
        )

        base_title = "OLS Estimation of Transition Equations"
        title = title_text(base_title, periods=periods, stages=stages, factors="all")

        if write_tex is True:
            with open(save_path, "w") as t:
                t.write(df_to_tex_table(df, title))

        return df
Beispiel #6
0
    def score_regression_residual_plot(
        self,
        factor,
        period=None,
        stage=None,
        controls=None,
        other_vars=None,
        agg_method="mean",
        reg_kws=None,
        save_path=None,
        write_tex=False,
        width=None,
        height=None,
        dpi=200,
    ):
        controls = [] if controls is None else controls
        other_vars = [] if other_vars is None else other_vars
        reg_kws = {} if reg_kws is None else reg_kws
        if write_tex is True:
            assert (
                save_path is not None
            ), "To write a tex file, please provide a save_path"
        if width is None and height is None:
            height = 0.9

        mod = self._score_regression_model(
            period=period,
            stage=stage,
            factor=factor,
            controls=controls,
            agg_method=agg_method,
        )

        res = mod.fit()

        data = self.data_proc.reg_df(
            factor=factor,
            period=period,
            stage=stage,
            controls=controls + other_vars,
            agg_method=agg_method,
        )

        data["residuals"] = res.resid
        data["fitted"] = res.fittedvalues

        y_name = f"{factor}_t_plusone"
        to_plot = [col for col in data.columns if col not in ["residuals", y_name]]
        figsize = (10, len(to_plot) * 5)
        fig, axes = plt.subplots(nrows=len(to_plot), figsize=figsize)

        kwargs = self._basic_regplot_args()
        kwargs.update(reg_kws)
        for ax, var in zip(axes, to_plot):
            sns.regplot(y="residuals", x=var, ax=ax, data=data, **kwargs)

        base_title = "Residual Plot"
        title = title_text(base_title, periods=period, factors=factor, stages=stage)

        if write_tex is True:
            write_figure_tex_snippet(save_path, title, width=width, height=height)

        if save_path is not None:
            fig.savefig(save_path, dpi=dpi, bbox_inches="tight")

        return fig
Beispiel #7
0
    def measurement_heatmap(
        self,
        periods="all",
        factors="all",
        figsize=None,
        heatmap_kws=None,
        save_path=None,
        dpi=200,
        write_tex=False,
        width=None,
        height=None,
    ):
        """Heatmap of the correlation matrix of measurements.

        Args:
            periods: periods to include. Can be the name of one period, a list
                like object with periods or 'all'.
            factors: factors to include. Can be the name of one factor, a list
                like object with factors or 'all'.
            figsize (tuple): size of the matplotlib figure. If None provided,
                the figure automatically scales with the size of the
                correlation matrix.
            heatmap_kws (dict): dictionary with arguments for sns.heatmap()
            save_path (str): path where the plot will be saved. Needs a valid
                file extension (.png, .jpg, .eps, ...); Other documents are
                saved in the same directory.
            dpi (int): resolution of the plot
            write_tex (bool): if True, a tex file with the plot is written.


        Returns:
            fig, ax

         """
        heatmap_kws = {} if heatmap_kws is None else heatmap_kws
        if write_tex is True:
            assert (
                save_path is not None
            ), "To write a tex file, please provide a save_path"
        df = self.data_proc.measurements_df(periods=periods, factors=factors)
        corr = df.corr()

        if figsize is None:
            figsize = (len(corr), 0.8 * len(corr))

        if width is None and height is None:
            if len(corr) <= 5:
                width = 0.5
            elif len(corr) <= 9:
                width = 0.8
            else:
                width = 1

        kwargs = self._basic_heatmap_args()
        kwargs.update(heatmap_kws)
        fig, ax = plt.subplots(figsize=figsize)
        ax = sns.heatmap(data=corr, ax=ax, **kwargs)

        base_title = "Correlations of Measurements"
        title = title_text(basic_name=base_title, periods=periods, factors=factors)

        if save_path is not None:
            fig.savefig(save_path, dpi=dpi, bbox_inches="tight")

        if write_tex is True:
            write_figure_tex_snippet(save_path, title, width=width, height=height)
        return fig, ax