Пример #1
0
def fitNB(X, Y):
    X = add_constant(X)
    model = NegativeBinomial(Y, X).fit(disp=0, skip_hessian=True, maxiter=500)
    if not model.mle_retvals['converged']:
        model = NegativeBinomial(Y, X).fit_regularized(disp=0,
                                                       skip_hessian=True,
                                                       maxiter=500)
    return model.params
Пример #2
0
    def setup_class(cls):
        # here we don't need to check convergence from default start_params
        start_params = [13.1996, 0.8582, -2.8005, -1.5031, 2.3849, -8.5552,
                        -2.88, 1.14]
        mod = NegativeBinomial(endog, exog)
        res = mod.fit(start_params=start_params, method='nm', maxiter=2000)
        marge = res.get_margeff(dummy=True)
        cls.res = res
        cls.margeff = marge

        cls.res1_slice = cls.res1_slice = [0, 1, 2, 3, 5, 6]
        cls.res1 = res_stata.results_negbin_margins_dummy
        cls.rtol_fac = 5e1
Пример #3
0
    def setup_class(cls):
        # here we don't need to check convergence from default start_params
        start_params = [
            13.1996, 0.8582, -2.8005, -1.5031, 2.3849, -8.5552, -2.88, 1.14
        ]
        mod = NegativeBinomial(endog, exog)
        res = mod.fit(start_params=start_params, method='nm', maxiter=2000)
        marge = res.get_margeff()
        cls.res = res
        cls.margeff = marge

        cls.res1_slice = slice(None, None, None)
        cls.res1 = res_stata.results_negbin_margins_cont
        cls.rtol_fac = 5e1
    def initialize(cls):
        from statsmodels.discrete.discrete_model import NegativeBinomial

        formula = "Days ~ C(Duration, Sum)*C(Weight, Sum)"
        mod = NegativeBinomial.from_formula(formula, cls.data,
                                            loglike_method='nb1')
        cls.res = mod.fit(cov_type='HC0')
Пример #5
0
    def initialize(cls):
        from statsmodels.discrete.discrete_model import NegativeBinomial

        formula = "Days ~ C(Duration, Sum)*C(Weight, Sum)"
        mod = NegativeBinomial.from_formula(formula, cls.data,
                                            loglike_method='nb1')
        cls.res = mod.fit(cov_type='HC0')
Пример #6
0
 def setupClass(cls):
     data = sm.datasets.randhie.load()
     exog = sm.add_constant(data.exog, prepend=False)
     cls.res1 = NegativeBinomial(data.endog, exog, 'nb2').fit(method='newton', disp=0)
     res2 = RandHIE()
     res2.negativebinomial_nb2_bfgs()
     cls.res2 = res2
Пример #7
0
def nb_cluster(formula, cluster, covs, coef):
    """Model a cluster of correlated features with the negative binomial"""
    methylated = np.array([f.methylated for f in cluster])
    counts = np.array([f.counts for f in cluster])
    try:
        res = [NegativeBinomial.from_formula(formula, covs, offset=np.log(count))\
               .fit(disp=0) for methylation, count in izip(methylated, counts)]
    except:
        return dict(t=np.nan, coef=np.nan, covar="NA", p=np.nan, corr=np.nan)

    methylations = methylated / counts  # for correlation below.

    r = get_ptc(res, coef)
    nan = np.isnan(r['p'])
    r['p'] = zscore_combine(r['p'][~nan], corr(methylations[~nan]))
    r['t'], r['coef'] = np.mean(r['t']), np.mean(r['coef'])
    return r
Пример #8
0
def nb_cluster(formula, cluster, covs, coef):
    """Model a cluster of correlated features with the negative binomial"""
    methylated = np.array([f.methylated for f in cluster])
    counts = np.array([f.counts for f in cluster])
    try:
        res = [NegativeBinomial.from_formula(formula, covs, offset=np.log(count))\
               .fit(disp=0) for methylation, count in izip(methylated, counts)]
    except:
        return dict(t=np.nan, coef=np.nan, covar="NA", p=np.nan, corr=np.nan)

    methylations = methylated / counts # for correlation below.

    r = get_ptc(res, coef)
    nan = np.isnan(r['p'])
    r['p'] = zscore_combine(r['p'][~nan], corr(methylations[~nan]))
    r['t'], r['coef'] = np.mean(r['t']), np.mean(r['coef'])
    return r
Пример #9
0
test.setupClass()

data = test.data.drop([0,1,2])
res_ols = ols("np.log(Days+1) ~ C(Duration, Sum)*C(Weight, Sum)", data).fit(use_t=False)

res_glm = glm("np.log(Days+1) ~ C(Duration, Sum)*C(Weight, Sum)",
                        data).fit()

res_poi = Poisson.from_formula("Days ~ C(Weight) * C(Duration)", data).fit(cov_type='HC0')
res_poi_2 = poisson("Days ~ C(Weight) + C(Duration)", data).fit(cov_type='HC0')

print('\nOLS')
print(res_ols.wald_test_terms())
print('\nGLM')
print(res_glm.wald_test_terms(skip_single=False, combine_terms=['Duration', 'Weight']))
print('\nPoisson 1')
print(res_poi.wald_test_terms(skip_single=False, combine_terms=['Duration', 'Weight']))
print('\nPoisson 2')
print(res_poi_2.wald_test_terms(skip_single=False))

from statsmodels.discrete.discrete_model import NegativeBinomial
res_nb2 = NegativeBinomial.from_formula("Days ~ C(Weight) * C(Duration)", data).fit()
print('\nNegative Binomial nb2')
print(res_nb2.wald_test_terms(skip_single=False))

res_nb1 = NegativeBinomial.from_formula("Days ~ C(Weight) * C(Duration)", data,
                                        loglike_method='nb1').fit(cov_type='HC0')
print('\nNegative Binomial nb2')
print(res_nb1.wald_test_terms(skip_single=False))

Пример #10
0
res_glm = glm("np.log(Days+1) ~ C(Duration, Sum)*C(Weight, Sum)", data).fit()

res_poi = Poisson.from_formula("Days ~ C(Weight) * C(Duration)",
                               data).fit(cov_type='HC0')
res_poi_2 = poisson("Days ~ C(Weight) + C(Duration)", data).fit(cov_type='HC0')

print('\nOLS')
print(res_ols.wald_test_terms())
print('\nGLM')
print(
    res_glm.wald_test_terms(skip_single=False,
                            combine_terms=['Duration', 'Weight']))
print('\nPoisson 1')
print(
    res_poi.wald_test_terms(skip_single=False,
                            combine_terms=['Duration', 'Weight']))
print('\nPoisson 2')
print(res_poi_2.wald_test_terms(skip_single=False))

from statsmodels.discrete.discrete_model import NegativeBinomial
res_nb2 = NegativeBinomial.from_formula("Days ~ C(Weight) * C(Duration)",
                                        data).fit()
print('\nNegative Binomial nb2')
print(res_nb2.wald_test_terms(skip_single=False))

res_nb1 = NegativeBinomial.from_formula(
    "Days ~ C(Weight) * C(Duration)", data,
    loglike_method='nb1').fit(cov_type='HC0')
print('\nNegative Binomial nb2')
print(res_nb1.wald_test_terms(skip_single=False))