本文整理汇总了Python中statsmodels.api.GLM属性的典型用法代码示例。如果您正苦于以下问题:Python api.GLM属性的具体用法?Python api.GLM怎么用?Python api.GLM使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类statsmodels.api
的用法示例。
在下文中一共展示了api.GLM属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: setup_class
# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import GLM [as 别名]
def setup_class(cls):
fweights = [1, 1, 1, 2, 2, 2, 3, 3, 3, 1, 1, 1, 2, 2, 2, 3, 3]
# faking aweights by using normalized freq_weights
fweights = np.array(fweights)
wsum = fweights.sum()
nobs = len(cpunish_data.endog)
aweights = fweights / wsum * nobs
cls.res1 = GLM(cpunish_data.endog, cpunish_data.exog,
family=sm.families.Poisson(), var_weights=aweights).fit()
# compare with discrete, start close to save time
modd = discrete.Poisson(cpunish_data.endog, cpunish_data.exog)
# Need to copy to avoid inplace adjustment
from copy import copy
cls.res2 = copy(res_stata.results_poisson_aweight_nonrobust)
cls.res2.resids = cls.res2.resids.copy()
# Need to adjust resids for pearson and deviance to add weights
cls.res2.resids[:, 3:5] *= np.sqrt(aweights[:, np.newaxis])
# prob_weights fail with HC, not properly implemented yet
示例2: test_warnings_raised
# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import GLM [as 别名]
def test_warnings_raised():
if sys.version_info < (3, 4):
raise SkipTest
weights = [1, 1, 1, 2, 2, 2, 3, 3, 3, 1, 1, 1, 2, 2, 2, 3, 3]
# faking aweights by using normalized freq_weights
weights = np.array(weights)
gid = np.arange(1, 17 + 1) // 2
cov_kwds = {'groups': gid, 'use_correction': False}
with warnings.catch_warnings(record=True) as w:
res1 = GLM(cpunish_data.endog, cpunish_data.exog,
family=sm.families.Poisson(), freq_weights=weights
).fit(cov_type='cluster', cov_kwds=cov_kwds)
res1.summary()
assert len(w) >= 1
with warnings.catch_warnings(record=True) as w:
res1 = GLM(cpunish_data.endog, cpunish_data.exog,
family=sm.families.Poisson(), var_weights=weights
).fit(cov_type='cluster', cov_kwds=cov_kwds)
res1.summary()
assert len(w) >= 1
示例3: test_incompatible_input
# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import GLM [as 别名]
def test_incompatible_input():
weights = [1, 1, 1, 2, 2, 2, 3, 3, 3, 1, 1, 1, 2, 2, 2, 3, 3]
exog = cpunish_data.exog
endog = cpunish_data.endog
family = sm.families.Poisson()
# Too short
assert_raises(ValueError, GLM, endog, exog, family=family,
freq_weights=weights[:-1])
assert_raises(ValueError, GLM, endog, exog, family=family,
var_weights=weights[:-1])
# Too long
assert_raises(ValueError, GLM, endog, exog, family=family,
freq_weights=weights + [3])
assert_raises(ValueError, GLM, endog, exog, family=family,
var_weights=weights + [3])
# Too many dimensions
assert_raises(ValueError, GLM, endog, exog, family=family,
freq_weights=[weights, weights])
assert_raises(ValueError, GLM, endog, exog, family=family,
var_weights=[weights, weights])
示例4: setup_class
# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import GLM [as 别名]
def setup_class(cls):
'''
Test Gaussian family with canonical identity link
'''
# Test Precisions
cls.decimal_resids = DECIMAL_3
cls.decimal_params = DECIMAL_2
cls.decimal_bic = DECIMAL_0
cls.decimal_bse = DECIMAL_3
from statsmodels.datasets.longley import load
cls.data = load()
cls.data.exog = add_constant(cls.data.exog, prepend=False)
cls.res1 = GLM(cls.data.endog, cls.data.exog,
family=sm.families.Gaussian()).fit()
from .results.results_glm import Longley
cls.res2 = Longley()
示例5: test_score_test_OLS
# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import GLM [as 别名]
def test_score_test_OLS():
# nicer example than Longley
from statsmodels.regression.linear_model import OLS
np.random.seed(5)
nobs = 100
sige = 0.5
x = np.random.uniform(0, 1, size=(nobs, 5))
x[:, 0] = 1
beta = 1. / np.arange(1., x.shape[1] + 1)
y = x.dot(beta) + sige * np.random.randn(nobs)
res_ols = OLS(y, x).fit()
res_olsc = OLS(y, x[:, :-2]).fit()
co = res_ols.compare_lm_test(res_olsc, demean=False)
res_glm = GLM(y, x[:, :-2], family=sm.families.Gaussian()).fit()
co2 = res_glm.model.score_test(res_glm.params, exog_extra=x[:, -2:])
# difference in df_resid versus nobs in scale see #1786
assert_allclose(co[0] * 97 / 100., co2[0], rtol=1e-13)
示例6: test_formula_missing_exposure
# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import GLM [as 别名]
def test_formula_missing_exposure():
# see 2083
import statsmodels.formula.api as smf
import pandas as pd
d = {'Foo': [1, 2, 10, 149], 'Bar': [1, 2, 3, np.nan],
'constant': [1] * 4, 'exposure' : np.random.uniform(size=4),
'x': [1, 3, 2, 1.5]}
df = pd.DataFrame(d)
family = sm.families.Gaussian(link=sm.families.links.log())
mod = smf.glm("Foo ~ Bar", data=df, exposure=df.exposure,
family=family)
assert_(type(mod.exposure) is np.ndarray, msg='Exposure is not ndarray')
exposure = pd.Series(np.random.uniform(size=5))
df.loc[3, 'Bar'] = 4 # nan not relevant for Valueerror for shape mismatch
assert_raises(ValueError, smf.glm, "Foo ~ Bar", data=df,
exposure=exposure, family=family)
assert_raises(ValueError, GLM, df.Foo, df[['constant', 'Bar']],
exposure=exposure, family=family)
示例7: test_wtd_patsy_missing
# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import GLM [as 别名]
def test_wtd_patsy_missing():
from statsmodels.datasets.cpunish import load
import pandas as pd
data = load()
data.exog[0, 0] = np.nan
data.endog[[2, 4, 6, 8]] = np.nan
data.pandas = pd.DataFrame(data.exog, columns=data.exog_name)
data.pandas['EXECUTIONS'] = data.endog
weights = np.arange(1, len(data.endog)+1)
formula = """EXECUTIONS ~ INCOME + PERPOVERTY + PERBLACK + VC100k96 +
SOUTH + DEGREE"""
mod_misisng = GLM.from_formula(formula, data=data.pandas,
freq_weights=weights)
assert_equal(mod_misisng.freq_weights.shape[0],
mod_misisng.endog.shape[0])
assert_equal(mod_misisng.freq_weights.shape[0],
mod_misisng.exog.shape[0])
assert_equal(mod_misisng.freq_weights.shape[0], 12)
keep_weights = np.array([2, 4, 6, 8, 10, 11, 12, 13, 14, 15, 16, 17])
assert_equal(mod_misisng.freq_weights, keep_weights)
示例8: test_framing_example_formula
# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import GLM [as 别名]
def test_framing_example_formula():
cur_dir = os.path.dirname(os.path.abspath(__file__))
data = pd.read_csv(os.path.join(cur_dir, 'results', "framing.csv"))
probit = sm.families.links.probit
outcome_model = sm.GLM.from_formula("cong_mesg ~ emo + treat + age + educ + gender + income",
data, family=sm.families.Binomial(link=probit()))
mediator_model = sm.OLS.from_formula("emo ~ treat + age + educ + gender + income", data)
med = Mediation(outcome_model, mediator_model, "treat", "emo",
outcome_fit_kwargs={'atol': 1e-11})
np.random.seed(4231)
med_rslt = med.fit(method='boot', n_rep=100)
diff = np.asarray(med_rslt.summary() - framing_boot_4231)
assert_allclose(diff, 0, atol=1e-6)
np.random.seed(4231)
med_rslt = med.fit(method='parametric', n_rep=100)
diff = np.asarray(med_rslt.summary() - framing_para_4231)
assert_allclose(diff, 0, atol=1e-6)
示例9: test_framing_example_moderator_formula
# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import GLM [as 别名]
def test_framing_example_moderator_formula():
cur_dir = os.path.dirname(os.path.abspath(__file__))
data = pd.read_csv(os.path.join(cur_dir, 'results', "framing.csv"))
probit = sm.families.links.probit
outcome_model = sm.GLM.from_formula("cong_mesg ~ emo + treat*age + emo*age + educ + gender + income",
data, family=sm.families.Binomial(link=probit()))
mediator_model = sm.OLS.from_formula("emo ~ treat*age + educ + gender + income", data)
moderators = {"age" : 20}
med = Mediation(outcome_model, mediator_model, "treat", "emo",
moderators=moderators)
np.random.seed(4231)
med_rslt = med.fit(method='parametric', n_rep=100)
diff = np.asarray(med_rslt.summary() - framing_moderated_4231)
assert_allclose(diff, 0, atol=1e-6)
示例10: setup_class
# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import GLM [as 别名]
def setup_class(cls):
# generate artificial data
np.random.seed(98765678)
nobs = 200
rvs = np.random.randn(nobs,6)
data_exog = rvs
data_exog = sm.add_constant(data_exog, prepend=False)
xbeta = 0.1 + 0.1*rvs.sum(1)
data_endog = np.random.poisson(np.exp(xbeta))
#estimate discretemod.Poisson as benchmark
cls.res_discrete = Poisson(data_endog, data_exog).fit(disp=0)
mod_glm = sm.GLM(data_endog, data_exog, family=sm.families.Poisson())
cls.res_glm = mod_glm.fit()
#estimate generic MLE
cls.mod = PoissonGMLE(data_endog, data_exog)
cls.res = cls.mod.fit(start_params=0.9 * cls.res_discrete.params,
method='bfgs', disp=0)
示例11: setup
# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import GLM [as 别名]
def setup(self):
#fit for each test, because results will be changed by test
x = self.exog
np.random.seed(987689)
y = x.sum(1) + np.random.randn(x.shape[0])
self.results = sm.GLM(y, self.exog).fit()
示例12: test_partial_residual_poisson
# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import GLM [as 别名]
def test_partial_residual_poisson(self):
np.random.seed(3446)
n = 100
p = 3
exog = np.random.normal(size=(n, p))
exog[:, 0] = 1
lin_pred = 4 + exog[:, 1] + 0.2*exog[:, 2]**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 1, 2:
for j in 0,1:
if j == 0:
fig = plot_partial_residuals(results, focus_col)
else:
fig = results.plot_partial_residuals(focus_col)
ax = fig.get_axes()[0]
add_lowess(ax)
ax.set_position([0.1, 0.1, 0.8, 0.77])
effect_str = ["Intercept", "Linear effect, slope=1",
"Quadratic effect"][focus_col]
ti = "Partial residual plot"
if j == 1:
ti += " (called as method)"
ax.set_title(ti + "\nPoisson regression\n" +
effect_str)
close_or_save(pdf, fig)
示例13: test_ceres_poisson
# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import GLM [as 别名]
def test_ceres_poisson(self):
np.random.seed(3446)
n = 100
p = 3
exog = np.random.normal(size=(n, p))
exog[:, 0] = 1
lin_pred = 4 + exog[:, 1] + 0.2*exog[:, 2]**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 1, 2:
for j in 0, 1:
if j == 0:
fig = plot_ceres_residuals(results, focus_col)
else:
fig = results.plot_ceres_residuals(focus_col)
ax = fig.get_axes()[0]
add_lowess(ax)
ax.set_position([0.1, 0.1, 0.8, 0.77])
effect_str = ["Intercept", "Linear effect, slope=1",
"Quadratic effect"][focus_col]
ti = "CERES plot"
if j == 1:
ti += " (called as method)"
ax.set_title(ti + "\nPoisson regression\n" +
effect_str)
close_or_save(pdf, fig)
示例14: _get_init_kwds
# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import GLM [as 别名]
def _get_init_kwds(self):
# this is a temporary fixup because exposure has been transformed
# see #1609, copied from discrete_model.CountModel
kwds = super(GLM, self)._get_init_kwds()
if 'exposure' in kwds and kwds['exposure'] is not None:
kwds['exposure'] = np.exp(kwds['exposure'])
return kwds
示例15: null
# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import GLM [as 别名]
def null(self):
endog = self._endog
model = self.model
exog = np.ones((len(endog), 1))
kwargs = model._get_init_kwds()
kwargs.pop('family')
if hasattr(self, '_offset_exposure'):
return GLM(endog, exog, family=self.family,
**kwargs).fit().fittedvalues
else:
# correct if fitted is identical across observations
wls_model = lm.WLS(endog, exog,
weights=self._iweights * self._n_trials)
return wls_model.fit().fittedvalues