def setup_class(cls): data = sm.datasets.randhie.load(as_pandas=False) cls.endog = data.endog exog = sm.add_constant(data.exog[:, 1:4], prepend=False) exog_infl = sm.add_constant(data.exog[:, 0], prepend=False) model = sm.ZeroInflatedPoisson(data.endog, exog, exog_infl=exog_infl, inflation='logit') cls.res1 = model.fit(method='newton', maxiter=500) # for llnull test cls.res1._results._attach_nullmodel = True cls.init_keys = ['exog_infl', 'exposure', 'inflation', 'offset'] cls.init_kwds = {'inflation': 'logit'}
def setup_class(cls): data = sm.datasets.randhie.load_pandas() cls.endog = data.endog cls.data = data exog = sm.add_constant(data.exog.iloc[:, 1:4], prepend=False) exog_infl = sm.add_constant(data.exog.iloc[:, 0], prepend=False) model = cls.model_cls(data.endog, exog, exog_infl=exog_infl, inflation='logit') cls.res1 = model.fit(**cls.fit_kwargs) # for llnull test cls.res1._results._attach_nullmodel = True cls.init_keys = ['exog_infl', 'exposure', 'inflation', 'offset'] cls.init_kwds = {'inflation': 'logit'}
def setup(self): # fit for each test, because results will be changed by test np.random.seed(987689) data = sm.datasets.randhie.load(as_pandas=False) data.exog = sm.add_constant(data.exog, prepend=False) mod = sm.NegativeBinomial(data.endog, data.exog) self.results = mod.fit(disp=0)
def setup_class(cls): data = sm.datasets.anes96.load(as_pandas=False) exog = data.exog exog = sm.add_constant(exog, prepend=False) cls.mod = sm.MNLogit(data.endog, exog) res = cls.mod.fit(disp=0) cls.params = [res.params.ravel('F')]
def test_names(self): param_names = [ 'inflate_lncoins', 'inflate_const', 'idp', 'lpi', 'fmde', 'const' ] assert_array_equal(self.res1.model.exog_names, param_names) assert_array_equal(self.res1.params.index.tolist(), param_names) assert_array_equal(self.res1.bse.index.tolist(), param_names) exog = sm.add_constant(self.data.exog.iloc[:, 1:4], prepend=True) exog_infl = sm.add_constant(self.data.exog.iloc[:, 0], prepend=True) param_names = [ 'inflate_const', 'inflate_lncoins', 'const', 'idp', 'lpi', 'fmde' ] model = sm.ZeroInflatedPoisson(self.data.endog, exog, exog_infl=exog_infl, inflation='logit') assert_array_equal(model.exog_names, param_names)
def setup_class(cls): nobs = 10000 np.random.seed(987689) x = np.random.randn(nobs, 3) x = sm.add_constant(x) cls.exog = x cls.xf = 0.25 * np.ones((2, 4)) cls.l_max = 20000 cls.predict_kwds = {}
def setup_class(cls): data = sm.datasets.randhie.load(as_pandas=False) cls.endog = data.endog exog = sm.add_constant(data.exog[:, 1], prepend=False) exog_infl = sm.add_constant(data.exog[:, 0], prepend=False) # cheating for now, parameters are not well identified in this dataset # github.com/statsmodels/statsmodels/pull/3928#issuecomment-331724022 sp = np.array([1.88, -10.28, -0.20, 1.14, 1.34]) model = sm.ZeroInflatedNegativeBinomialP(data.endog, exog, exog_infl=exog_infl, p=2) cls.res1 = model.fit(start_params=sp, method='nm', xtol=1e-6, maxiter=5000) # for llnull test cls.res1._results._attach_nullmodel = True cls.init_keys = ['exog_infl', 'exposure', 'inflation', 'offset', 'p'] cls.init_kwds = {'inflation': 'logit', 'p': 2}
def setup_class(cls): path = os.path.join(cur_dir, 'results', 'MNLogitUnderflowExample.csv') df = pd.read_csv(path) exog = sm.add_constant(df[['X', 'Y', 'Z']]) endog = df['Count'].copy() # edit endog to be _less_ imbalanced # TODO: after this editing, upstream version gets params right # but fails on test_llf. We need a slightly-more-imbalanced case endog[endog == 6] = 7 endog[endog == 9] = 7 endog[endog == 4] = 7 model = MNLogit(endog=endog, exog=exog) cls.res = model.fit()
def setup_class(cls): from .results.results_glm import CancerLog res2 = CancerLog endog = res2.endog exog = res2.exog[:, :-1] exog = sm.add_constant(exog, prepend=True) aweights = np.repeat(1, len(endog)) aweights[::5] = 5 aweights[::13] = 3 model = sm.GLM(endog, exog, family=sm.families.Gamma(link=sm.families.links.log()), var_weights=aweights) cls.res1 = model.fit(rtol=1e-25, atol=0)
def test_HC_use(): np.random.seed(0) nsample = 100 x = np.linspace(0, 10, 100) X = sm.add_constant(np.column_stack((x, x**2)), prepend=False) beta = np.array([1, 0.1, 10]) y = np.dot(X, beta) + np.random.normal(size=nsample) results = sm.OLS(y, X).fit() # test cov_params idx = np.array([1, 2]) # need to call HC0_se to have cov_HC0 available results.HC0_se cov12 = results.cov_params(column=[1, 2], cov_p=results.cov_HC0) assert_almost_equal(cov12, results.cov_HC0[idx[:, None], idx], decimal=15) # test t_test tvals = results.params / results.HC0_se ttest = results.t_test(np.eye(3), cov_p=results.cov_HC0) assert_almost_equal(ttest.tvalue, tvals, decimal=14) assert_almost_equal(ttest.sd, results.HC0_se, decimal=14) # test f_test ftest = results.f_test(np.eye(3)[:-1], cov_p=results.cov_HC0) slopes = results.params[:-1] idx = np.array([0, 1]) cov_slopes = results.cov_HC0[idx[:, None], idx] fval = np.dot(slopes, np.dot(np.linalg.inv(cov_slopes), slopes)) / len(idx) assert_allclose(ftest.fvalue, fval, rtol=12)
Author: Josef Perktold """ import pytest import numpy as np from numpy.testing import assert_allclose import sm2.api as sm from sm2.discrete.discrete_model import NegativeBinomialP import sm2.discrete.tests.results.results_count_margins as res_stata # load data into module namespace cpunish_data = sm.datasets.cpunish.load(as_pandas=False) cpunish_data.exog[:, 3] = np.log(cpunish_data.exog[:, 3]) exog = sm.add_constant(cpunish_data.exog, prepend=False) endog = cpunish_data.endog - 1 # avoid zero-truncation exog /= np.round(exog.max(0), 3) @pytest.mark.not_vetted class CheckMarginMixin(object): res2_slice = slice(None, None, None) rtol_fac = 1 dummy = False @classmethod def setup_class(cls): mod = cls.model_cls(endog, exog) res = mod.fit(**cls.fit_kwargs) cls.res = res
def setup_class(cls): data = sm.datasets.spector.load(as_pandas=False) data.exog = sm.add_constant(data.exog, prepend=False) cls.mod = sm.Logit(data.endog, data.exog) cls.params = [np.array([1, 0.25, 1.4, -7])]
def test_design(self): npt.assert_equal(self.model.exog, sm.add_constant(self.data.exog, prepend=True))
def setup_class(cls): cls.data = sm.datasets.stackloss.load(as_pandas=False) cls.data.exog = sm.add_constant(cls.data.exog, prepend=False)