本文整理汇总了Python中statsmodels.regression.mixed_linear_model.MixedLMParams.cov_re方法的典型用法代码示例。如果您正苦于以下问题:Python MixedLMParams.cov_re方法的具体用法?Python MixedLMParams.cov_re怎么用?Python MixedLMParams.cov_re使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类statsmodels.regression.mixed_linear_model.MixedLMParams
的用法示例。
在下文中一共展示了MixedLMParams.cov_re方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: txest_vcomp_1
# 需要导入模块: from statsmodels.regression.mixed_linear_model import MixedLMParams [as 别名]
# 或者: from statsmodels.regression.mixed_linear_model.MixedLMParams import cov_re [as 别名]
def txest_vcomp_1(self):
"""
Fit the same model using constrained random effects and variance components.
"""
np.random.seed(4279)
exog = np.random.normal(size=(400, 1))
exog_re = np.random.normal(size=(400, 2))
groups = np.kron(np.arange(100), np.ones(4))
slopes = np.random.normal(size=(100, 2))
slopes[:, 1] *= 2
slopes = np.kron(slopes, np.ones((4, 1))) * exog_re
errors = slopes.sum(1) + np.random.normal(size=400)
endog = exog.sum(1) + errors
free = MixedLMParams(1, 2, 0)
free.fe_params = np.ones(1)
free.cov_re = np.eye(2)
free.vcomp = np.zeros(0)
model1 = MixedLM(endog, exog, groups, exog_re=exog_re)
result1 = model1.fit(free=free)
exog_vc = {"a": {}, "b": {}}
for k,group in enumerate(model1.group_labels):
ix = model1.row_indices[group]
exog_vc["a"][group] = exog_re[ix, 0:1]
exog_vc["b"][group] = exog_re[ix, 1:2]
model2 = MixedLM(endog, exog, groups, exog_vc=exog_vc)
result2 = model2.fit()
result2.summary()
assert_allclose(result1.fe_params, result2.fe_params, atol=1e-4)
assert_allclose(np.diag(result1.cov_re), result2.vcomp, atol=1e-2, rtol=1e-4)
assert_allclose(result1.bse[[0, 1, 3]], result2.bse, atol=1e-2, rtol=1e-2)
示例2: do1
# 需要导入模块: from statsmodels.regression.mixed_linear_model import MixedLMParams [as 别名]
# 或者: from statsmodels.regression.mixed_linear_model.MixedLMParams import cov_re [as 别名]
def do1(reml, irf, ds_ix):
# No need to check independent random effects when there is
# only one of them.
if irf and ds_ix < 6:
return
irfs = "irf" if irf else "drf"
meth = "reml" if reml else "ml"
rslt = R_Results(meth, irfs, ds_ix)
# Fit the model
md = MixedLM(rslt.endog, rslt.exog_fe, rslt.groups,
rslt.exog_re)
if not irf: # Free random effects covariance
if np.any(np.diag(rslt.cov_re_r) < 1e-5):
with warnings.catch_warnings():
warnings.simplefilter("ignore")
mdf = md.fit(gtol=1e-7, reml=reml)
else:
mdf = md.fit(gtol=1e-7, reml=reml)
else: # Independent random effects
k_fe = rslt.exog_fe.shape[1]
k_re = rslt.exog_re.shape[1]
free = MixedLMParams(k_fe, k_re, 0)
free.fe_params = np.ones(k_fe)
free.cov_re = np.eye(k_re)
free.vcomp = np.array([])
if np.any(np.diag(rslt.cov_re_r) < 1e-5):
with warnings.catch_warnings():
warnings.simplefilter("ignore")
mdf = md.fit(reml=reml, gtol=1e-7, free=free)
else:
mdf = md.fit(reml=reml, gtol=1e-7, free=free)
assert_almost_equal(mdf.fe_params, rslt.coef, decimal=4)
assert_almost_equal(mdf.cov_re, rslt.cov_re_r, decimal=4)
assert_almost_equal(mdf.scale, rslt.scale_r, decimal=4)
k_fe = md.k_fe
assert_almost_equal(rslt.vcov_r, mdf.cov_params()[0:k_fe, 0:k_fe],
decimal=3)
assert_almost_equal(mdf.llf, rslt.loglike[0], decimal=2)
# Not supported in R except for independent random effects
if not irf:
assert_almost_equal(mdf.random_effects[0], rslt.ranef_postmean,
decimal=3)
assert_almost_equal(mdf.random_effects_cov[0],
rslt.ranef_condvar,
decimal=3)