Пример #1
0
def test_added_variable():

    fig = plt.figure()
    ax = fig.add_subplot(1,1,1)
    np.random.seed(3446)

    n = 100
    p = 3
    exog = np.random.normal(size=(n, p))
    lin_pred = 4 + exog[:, 0] + 0.2*exog[:, 1]**2
    expval = np.exp(lin_pred)
    endog = np.random.poisson(expval)

    model = sm.GLM(endog, exog, family=sm.families.Poisson())
    results = model.fit()

    focus_col = 0
    use_glm_weights = False#, True:
    resid_type = ["resid_deviance", "resid_response"]

    fig = rplots.plot_added_variable(results, focus_col,
                              use_glm_weights=use_glm_weights,
                              resid_type=resid_type[0], ax=ax)
    ti = "Added variable plot"
    #fig.savefig('test_added_variable-expected.pdf')
    fig2 = plt.figure()
    ax = fig2.add_subplot(1,1,1)
    fig2 = rplots.plot_added_variable(results, focus_col,
                              use_glm_weights=use_glm_weights,
                              resid_type=resid_type[1], ax=ax)
    return fig, fig2
Пример #2
0
def test_added_variable():

    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
    np.random.seed(3446)

    n = 100
    p = 3
    exog = np.random.normal(size=(n, p))
    lin_pred = 4 + exog[:, 0] + 0.2 * exog[:, 1]**2
    expval = np.exp(lin_pred)
    endog = np.random.poisson(expval)

    model = sm.GLM(endog, exog, family=sm.families.Poisson())
    results = model.fit()

    focus_col = 0
    use_glm_weights = False  #, True:
    resid_type = ["resid_deviance", "resid_response"]

    fig = rplots.plot_added_variable(results,
                                     focus_col,
                                     use_glm_weights=use_glm_weights,
                                     resid_type=resid_type[0],
                                     ax=ax)
    ti = "Added variable plot"
    #fig.savefig('test_added_variable-expected.pdf')
    fig2 = plt.figure()
    ax = fig2.add_subplot(1, 1, 1)
    fig2 = rplots.plot_added_variable(results,
                                      focus_col,
                                      use_glm_weights=use_glm_weights,
                                      resid_type=resid_type[1],
                                      ax=ax)
    return fig, fig2
Пример #3
0
    def test_added_variable_poisson(self, close_figures):

        np.random.seed(3446)

        n = 100
        p = 3
        exog = np.random.normal(size=(n, p))
        lin_pred = 4 + exog[:, 0] + 0.2 * exog[:, 1]**2
        expval = np.exp(lin_pred)
        endog = np.random.poisson(expval)

        model = sm.GLM(endog, exog, family=sm.families.Poisson())
        results = model.fit()

        for focus_col in 0, 1, 2:
            for use_glm_weights in False, True:
                for resid_type in "resid_deviance", "resid_response":
                    weight_str = ["Unweighted", "Weighted"][use_glm_weights]

                    # Run directly and called as a results method.
                    for j in 0, 1:

                        if j == 0:
                            fig = plot_added_variable(
                                results,
                                focus_col,
                                use_glm_weights=use_glm_weights,
                                resid_type=resid_type)
                            ti = "Added variable plot"
                        else:
                            fig = results.plot_added_variable(
                                focus_col,
                                use_glm_weights=use_glm_weights,
                                resid_type=resid_type)
                            ti = "Added variable plot (called as method)"
                        ax = fig.get_axes()[0]

                        add_lowess(ax)
                        ax.set_position([0.1, 0.1, 0.8, 0.7])
                        effect_str = [
                            "Linear effect, slope=1", "Quadratic effect",
                            "No effect"
                        ][focus_col]
                        ti += "\nPoisson regression\n"
                        ti += effect_str + "\n"
                        ti += weight_str + "\n"
                        ti += "Using '%s' residuals" % resid_type
                        ax.set_title(ti)
                        close_or_save(pdf, fig)
                        close_figures()
    def test_added_variable_poisson(self, close_figures):

        np.random.seed(3446)

        n = 100
        p = 3
        exog = np.random.normal(size=(n, p))
        lin_pred = 4 + exog[:, 0] + 0.2 * exog[:, 1]**2
        expval = np.exp(lin_pred)
        endog = np.random.poisson(expval)

        model = sm.GLM(endog, exog, family=sm.families.Poisson())
        results = model.fit()

        for focus_col in 0, 1, 2:
            for use_glm_weights in False, True:
                for resid_type in "resid_deviance", "resid_response":
                    weight_str = ["Unweighted", "Weighted"][use_glm_weights]

                    # Run directly and called as a results method.
                    for j in 0, 1:

                        if j == 0:
                            fig = plot_added_variable(results, focus_col,
                                                      use_glm_weights=use_glm_weights,
                                                      resid_type=resid_type)
                            ti = "Added variable plot"
                        else:
                            fig = results.plot_added_variable(focus_col,
                                                              use_glm_weights=use_glm_weights,
                                                              resid_type=resid_type)
                            ti = "Added variable plot (called as method)"
                        ax = fig.get_axes()[0]

                        add_lowess(ax)
                        ax.set_position([0.1, 0.1, 0.8, 0.7])
                        effect_str = ["Linear effect, slope=1",
                                      "Quadratic effect", "No effect"][focus_col]
                        ti += "\nPoisson regression\n"
                        ti += effect_str + "\n"
                        ti += weight_str + "\n"
                        ti += "Using '%s' residuals" % resid_type
                        ax.set_title(ti)
                        close_or_save(pdf, fig)
                        close_figures()