本文整理汇总了Python中statsmodels.api.OLS属性的典型用法代码示例。如果您正苦于以下问题:Python api.OLS属性的具体用法?Python api.OLS怎么用?Python api.OLS使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类statsmodels.api
的用法示例。
在下文中一共展示了api.OLS属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: model_fit_to_dataframe
# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import OLS [as 别名]
def model_fit_to_dataframe(fit):
"""
Take an object containing a statsmodels OLS model fit and extact
the main model fit metrics into a data frame.
Parameters
----------
fit : a statsmodels fit object
Model fit object obtained from a linear model trained using
`statsmodels.OLS`.
Returns
-------
df_fit : pandas DataFrame
Data frame with the main model fit metrics.
"""
df_fit = pd.DataFrame({"N responses": [int(fit.nobs)]})
df_fit['N features'] = int(fit.df_model)
df_fit['R2'] = fit.rsquared
df_fit['R2_adjusted'] = fit.rsquared_adj
return df_fit
示例2: setup_class
# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import OLS [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)
params = sm.OLS(cls.data.endog, cls.data.exog).fit().params
cls.res1 = GLM(cls.data.endog, cls.data.exog,
family=sm.families.Gaussian()).fit(start_params=params)
from .results.results_glm import Longley
cls.res2 = Longley()
示例3: test_framing_example_formula
# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import OLS [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)
示例4: _model2dataframe
# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import OLS [as 别名]
def _model2dataframe(model_endog, model_exog, model_type=OLS, **kwargs):
"""return a series containing the summary of a linear model
All the exceding parameters will be redirected to the linear model
"""
# create the linear model and perform the fit
model_result = model_type(model_endog, model_exog, **kwargs).fit()
# keeps track of some global statistics
statistics = pd.Series({'r2': model_result.rsquared,
'adj_r2': model_result.rsquared_adj})
# put them togher with the result for each term
result_df = pd.DataFrame({'params': model_result.params,
'pvals': model_result.pvalues,
'std': model_result.bse,
'statistics': statistics})
# add the complexive results for f-value and the total p-value
fisher_df = pd.DataFrame({'params': {'_f_test': model_result.fvalue},
'pvals': {'_f_test': model_result.f_pvalue}})
# merge them and unstack to obtain a hierarchically indexed series
res_series = pd.concat([result_df, fisher_df]).unstack()
return res_series.dropna()
示例5: test_combine
# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import OLS [as 别名]
def test_combine(self):
np.random.seed(3897)
x1 = np.random.normal(size=300)
x2 = np.random.normal(size=300)
y = x1 + x2 + np.random.normal(size=300)
x1[0:100] = np.nan
x2[250:] = np.nan
df = pd.DataFrame({"x1": x1, "x2": x2, "y": y})
idata = mice.MICEData(df)
mi = mice.MICE("y ~ x1 + x2", sm.OLS, idata, n_skip=20)
result = mi.fit(10, 20)
fmi = np.asarray([ 0.1920533 , 0.1587287 , 0.33174032])
assert_allclose(result.frac_miss_info, fmi, atol=1e-5)
params = np.asarray([-0.05397474, 0.97273307, 1.01652293])
assert_allclose(result.params, params, atol=1e-5)
tvalues = np.asarray([ -0.84781698, 15.10491582, 13.59998039])
assert_allclose(result.tvalues, tvalues, atol=1e-5)
示例6: fit_regularized
# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import OLS [as 别名]
def fit_regularized(self, method="elastic_net", alpha=0.,
L1_wt=1., start_params=None, profile_scale=False,
refit=False, **kwargs):
# Docstring attached below
# Need to adjust since RSS/n term in elastic net uses nominal
# n in denominator
if self.sigma is not None:
alpha = alpha * np.sum(1 / np.diag(self.sigma)) / len(self.endog)
rslt = OLS(self.wendog, self.wexog).fit_regularized(
method=method, alpha=alpha,
L1_wt=L1_wt,
start_params=start_params,
profile_scale=profile_scale,
refit=refit, **kwargs)
from statsmodels.base.elastic_net import (
RegularizedResults, RegularizedResultsWrapper)
rrslt = RegularizedResults(self, rslt.params)
return RegularizedResultsWrapper(rrslt)
示例7: get_influence
# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import OLS [as 别名]
def get_influence(self):
"""
get an instance of Influence with influence and outlier measures
Returns
-------
infl : Influence instance
the instance has methods to calculate the main influence and
outlier measures for the OLS regression
See also
--------
statsmodels.stats.outliers_influence.OLSInfluence
"""
from statsmodels.stats.outliers_influence import OLSInfluence
return OLSInfluence(self)
示例8: ind_reg_ret
# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import OLS [as 别名]
def ind_reg_ret(ind_ret,ret):
'''
用行业收益率去回归股票收益率,得到股票在行业因子上的暴露。我们这里只算个暴露,就不关心鲜猪肚了
:param DataFrame ind_ret: 行业收益率(月份*行业数)
:param DataFrame ret: 股票收益率(月份*股票数)
:return:
'''
#显著的pvalue阈值定在0.1
pvalue_threshold=0.1
ind_loading=np.zeros([ret.shape[1],ind_ret.shape[1]])
# 依次取出每一只股票的收益率,与行业收益率做多元回归
for i in range(ret.shape[1]):
# 取出一个行业来与股票做回归
for j in range(ind_ret.shape[1]):
model=sm.OLS(ret.values[:,i],ind_ret.values[:,j]).fit()
#将回归系数写入暴露矩阵
ind_loading[i,j]=model.params[0]
#对于pvalue小于阈值(显著)的因子,将其对应的significant_stocks_list加1
ind_loading=pd.DataFrame(ind_loading,columns=ind_ret.columns)
return ind_loading
示例9: ret_reg_loading
# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import OLS [as 别名]
def ret_reg_loading(tech_loading,ret,dummy):
'''
取每月每个指标111个股票的Loading去回归当月这111个股票的收益率,判断是否显著。根据判断结果来筛选变量
:param tech_loading:
:param ret:
:return:
'''
# 初始化显著列表
significant_days=dict()
for tech in tech_loading.columns:
significant_days[tech]=0
# 取每个指标在111只股票上的loading做自变量
for tech in tech_loading.columns:
# 取某一个月111只股票的收益率做因变量
for i in range(ret.shape[0]):
model = sm.OLS(ret.iloc[i,:].values, pd.concat([tech_loading[tech],dummy],axis=1).values).fit()
pvalue=model.pvalues[0]
if pvalue<0.1:
significant_days[tech]+=1
return significant_days
示例10: reg_m
# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import OLS [as 别名]
def reg_m(y, x, estimator, weights=None):
ones = np.ones(len(x[0]))
X = sm.add_constant(np.column_stack((x[0], ones)))
for ele in x[1:]:
X = sm.add_constant(np.column_stack((ele, X)))
if estimator=='ols':
return sm.OLS(y, X).fit()
elif estimator=='wls':
return sm.WLS(y, X, weights).fit()
elif estimator=='gls':
return sm.GLS(y, X).fit()
return None
############################
#Run general linear regression
####func array contains the array of functions consdered in the regression
####params coefficients are reversed; the first param coefficient corresponds to the last function in func array
####notice the independent vectors are given in dictionary format, egs:{'bob':[1,2,3,4,5],'mary':[1,2,3,4,5]}
示例11: trainModel
# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import OLS [as 别名]
def trainModel(X, Y):
"""
训练模型
"""
model = sm.OLS(Y, X) # 创建一个线性模型
re = model.fit()
return re
示例12: hedge_ratio
# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import OLS [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
示例13: estimate_treatment_effect
# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import OLS [as 别名]
def estimate_treatment_effect(covariates, treatment, outcome):
"""Run ordinary least squares to estimate causal effect of treatment variable on the outcome Y.
Parameters
----------
covariates: `np.ndarray`
Array of shape [num_samples, num_features] of features
treatment: `np.ndarray`
Binary array of shape [num_samples] indicating treatment status for each
sample.
outcome: `np.ndarray`
Array of shape [num_samples] containing the observed outcome for each sample.
Returns
-------
result: `whynot.framework.InferenceResult`
InferenceResult object for this procedure
"""
features = np.copy(covariates)
treatment = treatment.reshape(-1, 1)
features = np.concatenate([treatment, features], axis=1)
features = sm.add_constant(features, prepend=True, has_constant="add")
# Only time model fitting, not preprocessing
start_time = perf_counter()
model = sm.OLS(outcome, features)
results = model.fit()
stop_time = perf_counter()
# Treatment is the second variable (first is the constant offset)
ate = results.params[1]
stderr = results.bse[1]
conf_int = (ate - 1.96 * stderr, ate + 1.96 * stderr)
return InferenceResult(
ate=ate,
stderr=stderr,
ci=conf_int,
individual_effects=None,
elapsed_time=stop_time - start_time,
)
示例14: main
# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import OLS [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
示例15: regression
# 需要导入模块: from statsmodels import api [as 别名]
# 或者: from statsmodels.api import OLS [as 别名]
def regression(df):
"""
Create linear regression of time series data with a lag of 1.
Parameters:
- df: The dataframe with the stock data.
Returns:
X, Y, and the fitted statsmodels linear regression
"""
X = df.close.shift().dropna()
Y = df.close[1:]
return X, Y, sm.OLS(Y, X).fit()