当前位置: 首页>>代码示例>>Python>>正文


Python api.add_constant方法代码示例

本文整理汇总了Python中statsmodels.api.add_constant方法的典型用法代码示例。如果您正苦于以下问题:Python api.add_constant方法的具体用法?Python api.add_constant怎么用?Python api.add_constant使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在statsmodels.api的用法示例。


在下文中一共展示了api.add_constant方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: linearModel

# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import add_constant [as 别名]
def linearModel(data):
    """
    线性回归统计性质分析步骤展示

    参数
    ----
    data : DataFrame,建模数据
    """
    features = ["x"]
    labels = ["y"]
    Y = data[labels]
    # 加入常量变量
    X = sm.add_constant(data[features])
    # 构建模型
    re = trainModel(X, Y)
    # 分析模型效果
    modelSummary(re)
    # const并不显著,去掉这个常量变量
    resNew = trainModel(data[features], Y)
    # 输出新模型的分析结果
    print(resNew.summary()) 
开发者ID:wdxtub,项目名称:deep-learning-note,代码行数:23,代码来源:2_linear_statsmodels.py

示例2: setup

# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import add_constant [as 别名]
def setup(self):
        nsample = 100
        sig = 0.5
        x1 = np.linspace(0, 20, nsample)
        x2 = 5 + 3* np.random.randn(nsample)
        X = np.c_[x1, x2, np.sin(0.5*x1), (x2-5)**2, np.ones(nsample)]
        beta = [0.5, 0.5, 1, -0.04, 5.]
        y_true = np.dot(X, beta)
        y = y_true + sig * np.random.normal(size=nsample)
        exog0 = sm.add_constant(np.c_[x1, x2], prepend=False)
        exog0 = DataFrame(exog0, columns=["const", "var1", "var2"])
        y = Series(y, name="outcome")
        res = sm.OLS(y, exog0).fit()
        self.res = res
        data = DataFrame(exog0, columns=["const", "var1", "var2"])
        data['y'] = y
        self.data = data 
开发者ID:birforce,项目名称:vnpy_crypto,代码行数:19,代码来源:test_regressionplots.py

示例3: setup_class

# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import add_constant [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() 
开发者ID:birforce,项目名称:vnpy_crypto,代码行数:19,代码来源:test_glm.py

示例4: test_stationary_grid

# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import add_constant [as 别名]
def test_stationary_grid(self):

        endog = np.r_[4, 2, 3, 1, 4, 5, 6, 7, 8, 3, 2, 4.]
        exog = np.r_[2, 3, 1, 4, 3, 2, 5, 4, 5, 6, 3, 2]
        group = np.r_[0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3]
        exog = sm.add_constant(exog)

        cs = Stationary(max_lag=2, grid=True)
        model = sm.GEE(endog, exog, group, cov_struct=cs)
        result = model.fit()
        se = result.bse * np.sqrt(12 / 9.)  # Stata adjustment

        assert_allclose(cs.covariance_matrix(np.r_[1, 1, 1], 0)[0].sum(),
                        6.4633538285149452)

        # Obtained from Stata using:
        # xtgee y x, i(g) vce(robust) corr(Stationary2)
        assert_allclose(result.params, np.r_[
                        4.463968, -0.0386674], rtol=1e-5, atol=1e-5)
        assert_allclose(se, np.r_[0.5217202, 0.2800333], rtol=1e-5, atol=1e-5) 
开发者ID:birforce,项目名称:vnpy_crypto,代码行数:22,代码来源:test_gee.py

示例5: setup_class

# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import add_constant [as 别名]
def setup_class(cls):
        from statsmodels.datasets.stackloss import load
        cls.data = load()
        cls.data.exog = sm.add_constant(cls.data.exog, prepend=False)
        results = RLM(cls.data.endog, cls.data.exog,\
                    M=sm.robust.norms.HuberT()).fit(scale_est=\
                    sm.robust.scale.HuberScale())
        h2 = RLM(cls.data.endog, cls.data.exog,\
                    M=sm.robust.norms.HuberT()).fit(cov="H2",
                    scale_est=sm.robust.scale.HuberScale()).bcov_scaled
        h3 = RLM(cls.data.endog, cls.data.exog,\
                    M=sm.robust.norms.HuberT()).fit(cov="H3",
                    scale_est=sm.robust.scale.HuberScale()).bcov_scaled
        cls.res1 = results
        cls.res1.h2 = h2
        cls.res1.h3 = h3 
开发者ID:birforce,项目名称:vnpy_crypto,代码行数:18,代码来源:test_rlm.py

示例6: calc_factors

# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import add_constant [as 别名]
def calc_factors(self, x=None, keepdim=0, addconst=True):
        '''get factor decomposition of exogenous variables

        This uses principal component analysis to obtain the factors. The number
        of factors kept is the maximum that will be considered in the regression.
        '''
        if x is None:
            x = self.exog
        else:
            x = np.asarray(x)
        xred, fact, evals, evecs  = pca(x, keepdim=keepdim, normalize=1)
        self.exog_reduced = xred
        #self.factors = fact
        if addconst:
            self.factors = sm.add_constant(fact, prepend=True)
            self.hasconst = 1  #needs to be int
        else:
            self.factors = fact
            self.hasconst = 0  #needs to be int

        self.evals = evals
        self.evecs = evecs 
开发者ID:birforce,项目名称:vnpy_crypto,代码行数:24,代码来源:factormodels.py

示例7: linmod

# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import add_constant [as 别名]
def linmod(y, x, weights=None, sigma=None, add_const=True, filter_missing=True,
           **kwds):
    '''get linear model with extra options for entry

    dispatches to regular model class and does not wrap the output

    If several options are exclusive, for example sigma and weights, then the
    chosen class depends on the implementation sequence.
    '''

    if filter_missing:
        y, x = remove_nanrows(y, x)
        #do the same for masked arrays

    if add_const:
        x = sm.add_constant(x, prepend=True)

    if not sigma is None:
        return GLS(y, x, sigma=sigma, **kwds)
    elif not weights is None:
        return WLS(y, x, weights=weights, **kwds)
    else:
        return OLS(y, x, **kwds) 
开发者ID:birforce,项目名称:vnpy_crypto,代码行数:25,代码来源:wrappers.py

示例8: setup_class

# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import add_constant [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) 
开发者ID:birforce,项目名称:vnpy_crypto,代码行数:23,代码来源:test_poisson.py

示例9: setup_class

# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import add_constant [as 别名]
def setup_class(cls):
        data = sm.datasets.randhie.load()
        cls.endog = data.endog
        exog = sm.add_constant(data.exog[:,1:4], prepend=False)
        exog_infl = sm.add_constant(data.exog[:,0], prepend=False)
        cls.res1 = sm.ZeroInflatedPoisson(data.endog, exog,
            exog_infl=exog_infl, offset=data.exog[:,7]).fit(method='newton',
                                                            maxiter=500,
                                                            disp=0)
        # for llnull test
        cls.res1._results._attach_nullmodel = True
        cls.init_keys = ['exog_infl', 'exposure', 'inflation', 'offset']
        cls.init_kwds = {'inflation': 'logit'}
        res2 = RandHIE()
        res2.zero_inflated_poisson_offset()
        cls.res2 = res2 
开发者ID:birforce,项目名称:vnpy_crypto,代码行数:18,代码来源:test_count_model.py

示例10: setup_class

# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import add_constant [as 别名]
def setup_class(cls):
        data = sm.datasets.spector.load()
        data.exog = sm.add_constant(data.exog, prepend=False)
        res2 = Spector()
        res2.probit()
        cls.res2 = res2

        # fmin_cg fails to converge on some machines - reparameterize
        from statsmodels.tools.transform_model import StandardizeTransform
        transf = StandardizeTransform(data.exog)
        exog_st = transf(data.exog)
        res1_st = Probit(data.endog,
                         exog_st).fit(method="cg", disp=0, maxiter=1000,
                                      gtol=1e-08)
        start_params = transf.transform_params(res1_st.params)
        assert_allclose(start_params, res2.params, rtol=1e-5, atol=1e-6)

        cls.res1 = Probit(data.endog,
                          data.exog).fit(start_params=start_params,
                                         method="cg", maxiter=1000,
                                         gtol=1e-05, disp=0)

        assert_array_less(cls.res1.mle_retvals['fcalls'], 100) 
开发者ID:birforce,项目名称:vnpy_crypto,代码行数:25,代码来源:test_discrete.py

示例11: test_margeff_dummy

# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import add_constant [as 别名]
def test_margeff_dummy(self):
        data = self.data
        vote = data.data['vote']
        exog = np.column_stack((data.exog, vote))
        exog = sm.add_constant(exog, prepend=False)
        res = MNLogit(data.endog, exog).fit(method="newton", disp=0)
        me = res.get_margeff(dummy=True)
        assert_almost_equal(me.margeff, self.res2.margeff_dydx_dummy_overall,
                6)
        assert_almost_equal(me.margeff_se,
                self.res2.margeff_dydx_dummy_overall_se, 6)
        me = res.get_margeff(dummy=True, method="eydx")
        assert_almost_equal(me.margeff, self.res2.margeff_eydx_dummy_overall,
                5)
        assert_almost_equal(me.margeff_se,
                self.res2.margeff_eydx_dummy_overall_se, 6) 
开发者ID:birforce,项目名称:vnpy_crypto,代码行数:18,代码来源:test_discrete.py

示例12: test_poisson_newton

# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import add_constant [as 别名]
def test_poisson_newton():
    #GH: 24, Newton doesn't work well sometimes
    nobs = 10000
    np.random.seed(987689)
    x = np.random.randn(nobs, 3)
    x = sm.add_constant(x, prepend=True)
    y_count = np.random.poisson(np.exp(x.sum(1)))
    mod = sm.Poisson(y_count, x)
    from pandas.util.testing import assert_produces_warning
    # this is not thread-safe
    # py 2.7 and 3.3 don't raise here anymore #4235
    import sys
    PY3_g3 = sys.version_info[:2] > (3, 3)
    if PY3_g3:
        with assert_produces_warning():
            warnings.simplefilter('always')
            res = mod.fit(start_params=-np.ones(4), method='newton', disp=0)
    else:
        res = mod.fit(start_params=-np.ones(4), method='newton', disp=0)

    assert_(not res.mle_retvals['converged']) 
开发者ID:birforce,项目名称:vnpy_crypto,代码行数:23,代码来源:test_discrete.py

示例13: test_issue_339

# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import add_constant [as 别名]
def test_issue_339():
    # make sure MNLogit summary works for J != K.
    data = sm.datasets.anes96.load()
    exog = data.exog
    # leave out last exog column
    exog = exog[:,:-1]
    exog = sm.add_constant(exog, prepend=True)
    res1 = sm.MNLogit(data.endog, exog).fit(method="newton", disp=0)
    # strip the header from the test
    smry = "\n".join(res1.summary().as_text().split('\n')[9:])
    cur_dir = os.path.dirname(os.path.abspath(__file__))
    test_case_file = os.path.join(cur_dir, 'results', 'mn_logit_summary.txt')
    with open(test_case_file, 'r') as fd:
        test_case = fd.read()
    np.testing.assert_equal(smry, test_case[:-1])
    # smoke test for summary2
    res1.summary2()  # see #3651 
开发者ID:birforce,项目名称:vnpy_crypto,代码行数:19,代码来源:test_discrete.py

示例14: main

# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import add_constant [as 别名]
def main():
    original_data = extract_data()
    splitted_data = split_data(original_data)
    useful_data = extract_useful_data(splitted_data)
    plot_data(useful_data)
    convert_image()
    returns = np.asarray(find_returns(useful_data))
    training_data = np.asarray(get_pixel_values())
    training_data = sm.add_constant(training_data, has_constant='add')
    results = sm.OLS(returns[0:4340], training_data[0:4340]).fit()
    y_in_sample = results.predict(training_data[0:4340])
    r2 = r_squared(returns[0:4340], y_in_sample)
    print r2 
开发者ID:JasonDoingGreat,项目名称:Convolutional-Networks-for-Stock-Predicting,代码行数:15,代码来源:main.py

示例15: hedge_ratio

# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import add_constant [as 别名]
def hedge_ratio(Y, X, add_const=True):
    if add_const:
        X = sm.add_constant(X)
        model = sm.OLS(Y, X).fit()
        return model.params[1]
    model = sm.OLS(Y, X).fit()
    return model.params.values 
开发者ID:vsmolyakov,项目名称:fin,代码行数:9,代码来源:pairs_trading.py


注:本文中的statsmodels.api.add_constant方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。