Exemple #1
0
    def setup_class(cls):
        cls.idx = [6, 2, 3, 4, 5, 0]  # 2 is dropped baseline for categorical

        # example without offset
        formula = 'deaths ~ smokes + C(agecat)'
        mod = cls.model_cls.from_formula(formula,
                                         data=data,
                                         offset=np.log(data['pyears'].values))

        constr = 'C(agecat)[T.5] - C(agecat)[T.4] = 0.5'
        lc = patsy.DesignInfo(mod.exog_names).linear_constraint(constr)
        cls.res1 = fit_constrained(mod,
                                   lc.coefs,
                                   lc.constants,
                                   fit_kwds={
                                       'method': 'newton',
                                       'disp': 0
                                   })
        cls.constraints = lc
        # TODO: bfgs fails

        # test method of Poisson, not monkey patched
        cls.res1m = mod.fit_constrained(constr,
                                        method='bfgs',
                                        disp=0,
                                        start_params=cls.res1[0])
Exemple #2
0
    def setup_class(cls):
        mod1 = cls.model_cls(spector_data.endog,
                             spector_data.exog,
                             family=families.Binomial())

        # not used to match Stata for HC
        # nobs, k_params = mod1.exog.shape
        # k_params -= 1   # one constraint
        cov_type = 'HC0'
        cov_kwds = {'scaling_factor': 32 / 31.}
        # looks like nobs / (nobs - 1) and not (nobs - 1.) / (nobs - k_params)}
        constr = 'x1 - x3 = 0'
        cls.res1m = mod1.fit_constrained(constr,
                                         cov_type=cov_type,
                                         cov_kwds=cov_kwds,
                                         atol=1e-10)

        R, q = cls.res1m.constraints.coefs, cls.res1m.constraints.constants
        cls.res1 = fit_constrained(mod1,
                                   R,
                                   q,
                                   fit_kwds={
                                       'atol': 1e-10,
                                       'cov_type': cov_type,
                                       'cov_kwds': cov_kwds
                                   })
        cls.constraints_rq = (R, q)
Exemple #3
0
    def setup_class(cls):
        # example without offset
        formula = 'deaths ~ logpyears + smokes + C(agecat)'
        mod = cls.model_cls.from_formula(formula, data=data)

        # get start_params, example fails to converge on one py TravisCI
        k_vars = len(mod.exog_names)
        start_params = np.zeros(k_vars)
        start_params[0] = np.log(mod.endog.mean())
        # if we need it, this is desired params
        # p = np.array([-9.43762015, 1.52762442, 2.74155711, 3.58730007,
        #               4.08730007, 1.15987869, 0.12111539])

        constr = 'C(agecat)[T.5] - C(agecat)[T.4] = 0.5'
        lc = patsy.DesignInfo(mod.exog_names).linear_constraint(constr)
        cls.res1 = fit_constrained(mod,
                                   lc.coefs,
                                   lc.constants,
                                   start_params=start_params,
                                   fit_kwds={
                                       'method': 'bfgs',
                                       'disp': 0
                                   })
        # TODO: Newton fails

        # test method of Poisson, not monkey patched
        cls.res1m = mod.fit_constrained(constr,
                                        start_params=start_params,
                                        method='bfgs',
                                        disp=0)
Exemple #4
0
    def setup_class(cls):
        # example without offset
        formula = 'deaths ~ logpyears + smokes + C(agecat)'
        mod = cls.model_cls.from_formula(formula, data=data)
        # get start_params, example fails to converge on one py TravisCI
        k_vars = len(mod.exog_names)
        start_params = np.zeros(k_vars)
        start_params[0] = np.log(mod.endog.mean())
        # if we need it, this is desired params
        # p = np.array([-3.93478643, 1.37276214, 2.33077032, 2.71338891,
        #               2.71338891, 0.57966535, 0.97254074])

        constr = 'C(agecat)[T.4] = C(agecat)[T.5]'
        lc = patsy.DesignInfo(mod.exog_names).linear_constraint(constr)
        cls.res1 = fit_constrained(mod,
                                   lc.coefs,
                                   lc.constants,
                                   start_params=start_params,
                                   fit_kwds={
                                       'method': 'bfgs',
                                       'disp': 0
                                   })
        # TODO: Newton fails

        # test method of Poisson, not monkey patched
        cls.res1m = mod.fit_constrained(constr,
                                        start_params=start_params,
                                        method='bfgs',
                                        disp=0)
Exemple #5
0
    def setup_class(cls):
        mod1 = cls.model_cls(spector_data.endog,
                             spector_data.exog,
                             family=families.Binomial())

        constr = 'x1 - x3 = 0'
        cls.res1m = mod1.fit_constrained(constr, atol=1e-10)

        R, q = cls.res1m.constraints.coefs, cls.res1m.constraints.constants
        cls.res1 = fit_constrained(mod1, R, q, fit_kwds={'atol': 1e-10})
        cls.constraints_rq = (R, q)
Exemple #6
0
    def setup_class(cls):
        cls.res2 = reslogit.results_constraint1
        mod1 = cls.model_cls(spector_data.endog,
                             spector_data.exog,
                             family=families.Binomial())

        constr = 'x1 = 2.8'
        cls.res1m = mod1.fit_constrained(constr)

        R, q = cls.res1m.constraints.coefs, cls.res1m.constraints.constants
        cls.res1 = fit_constrained(mod1, R, q)
Exemple #7
0
    def setup_class(cls):
        # example without offset
        formula = 'deaths ~ logpyears + smokes + C(agecat)'
        mod = cls.model_cls.from_formula(formula,
                                         data=data,
                                         family=families.Poisson())

        constr = 'C(agecat)[T.4] = C(agecat)[T.5]'
        lc = patsy.DesignInfo(mod.exog_names).linear_constraint(constr)
        cls.res1 = fit_constrained(mod,
                                   lc.coefs,
                                   lc.constants,
                                   fit_kwds={'atol': 1e-10})
        cls.constraints = lc
        cls.res1m = mod.fit_constrained(constr, atol=1e-10)