本文整理汇总了Python中statsmodels.regression.linear_model.OLS.summary方法的典型用法代码示例。如果您正苦于以下问题:Python OLS.summary方法的具体用法?Python OLS.summary怎么用?Python OLS.summary使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类statsmodels.regression.linear_model.OLS
的用法示例。
在下文中一共展示了OLS.summary方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_OLSsummary_rsquared_label
# 需要导入模块: from statsmodels.regression.linear_model import OLS [as 别名]
# 或者: from statsmodels.regression.linear_model.OLS import summary [as 别名]
def test_OLSsummary_rsquared_label(self):
# Check that the "uncentered" label is correctly added after rsquared
x = [1, 5, 7, 3, 5, 2, 5, 3]
y = [6, 4, 2, 7, 4, 9, 10, 2]
reg_with_constant = OLS(y, x, hasconst=True).fit()
assert 'R-squared:' in str(reg_with_constant.summary2())
assert 'R-squared:' in str(reg_with_constant.summary())
reg_without_constant = OLS(y, x, hasconst=False).fit()
assert 'R-squared (uncentered):' in str(reg_without_constant.summary2())
assert 'R-squared (uncentered):' in str(reg_without_constant.summary())
示例2: test_regression_with_tuples
# 需要导入模块: from statsmodels.regression.linear_model import OLS [as 别名]
# 或者: from statsmodels.regression.linear_model.OLS import summary [as 别名]
def test_regression_with_tuples(self):
i = pandas.Series([1, 2, 3, 4] * 10, name="i")
y = pandas.Series([1, 2, 3, 4, 5] * 8, name="y")
x = pandas.Series([1, 2, 3, 4, 5, 6, 7, 8] * 5, name="x")
df = pandas.DataFrame(index=i.index)
df = df.join(i)
endo = df.join(y)
exo = df.join(x)
endo_groups = endo.groupby("i")
exo_groups = exo.groupby("i")
exo_df = exo_groups.agg([np.sum, np.max])
endo_df = endo_groups.agg([np.sum, np.max])
reg = OLS(exo_df[[("x", "sum")]], endo_df).fit()
interesting_lines = []
import warnings
with warnings.catch_warnings():
# Catch ominormal warning, not interesting here
warnings.simplefilter("ignore")
for line in str(reg.summary()).splitlines():
if "_" in line:
interesting_lines.append(line[:38])
desired = ["Dep. Variable: x_sum ",
"y_sum 1.4595 0.209 ",
"y_amax 0.2432 0.035 "]
assert_equal(sorted(desired), sorted(interesting_lines))
示例3: test_ols_summary_rsquared_label
# 需要导入模块: from statsmodels.regression.linear_model import OLS [as 别名]
# 或者: from statsmodels.regression.linear_model.OLS import summary [as 别名]
def test_ols_summary_rsquared_label():
# Check that the "uncentered" label is correctly added after rsquared
x = [1, 5, 7, 3, 5, 2, 5, 3]
y = [6, 4, 2, 7, 4, 9, 10, 2]
reg_with_constant = OLS(y, add_constant(x)).fit()
r2_str = 'R-squared:'
with pytest.warns(UserWarning):
assert r2_str in str(reg_with_constant.summary2())
with pytest.warns(UserWarning):
assert r2_str in str(reg_with_constant.summary())
reg_without_constant = OLS(y, x, hasconst=False).fit()
r2_str = 'R-squared (uncentered):'
with pytest.warns(UserWarning):
assert r2_str in str(reg_without_constant.summary2())
with pytest.warns(UserWarning):
assert r2_str in str(reg_without_constant.summary())
示例4: test_fvalue_only_constant
# 需要导入模块: from statsmodels.regression.linear_model import OLS [as 别名]
# 或者: from statsmodels.regression.linear_model.OLS import summary [as 别名]
def test_fvalue_only_constant():
# if only constant in model, return nan see #3642
nobs = 20
np.random.seed(2)
x = np.ones(nobs)
y = np.random.randn(nobs)
from statsmodels.regression.linear_model import OLS, WLS
res = OLS(y, x).fit(cov_type='hac', cov_kwds={'maxlags': 3})
assert_(np.isnan(res.fvalue))
assert_(np.isnan(res.f_pvalue))
res.summary()
res = WLS(y, x).fit(cov_type='HC1')
assert_(np.isnan(res.fvalue))
assert_(np.isnan(res.f_pvalue))
res.summary()
示例5: test_fvalue_implicit_constant
# 需要导入模块: from statsmodels.regression.linear_model import OLS [as 别名]
# 或者: from statsmodels.regression.linear_model.OLS import summary [as 别名]
def test_fvalue_implicit_constant():
nobs = 100
np.random.seed(2)
x = np.random.randn(nobs, 1)
x = ((x > 0) == [True, False]).astype(int)
y = x.sum(1) + np.random.randn(nobs)
from statsmodels.regression.linear_model import OLS, WLS
res = OLS(y, x).fit(cov_type='HC1')
assert_(np.isnan(res.fvalue))
assert_(np.isnan(res.f_pvalue))
res.summary()
res = WLS(y, x).fit(cov_type='HC1')
assert_(np.isnan(res.fvalue))
assert_(np.isnan(res.f_pvalue))
res.summary()
示例6: test_summary_as_latex
# 需要导入模块: from statsmodels.regression.linear_model import OLS [as 别名]
# 或者: from statsmodels.regression.linear_model.OLS import summary [as 别名]
def test_summary_as_latex():
# GH#734
import re
dta = longley.load_pandas()
X = dta.exog
X["constant"] = 1
y = dta.endog
res = OLS(y, X).fit()
with pytest.warns(UserWarning):
table = res.summary().as_latex()
# replace the date and time
table = re.sub("(?<=\n\\\\textbf\\{Date:\\} &).+?&",
" Sun, 07 Apr 2013 &", table)
table = re.sub("(?<=\n\\\\textbf\\{Time:\\} &).+?&",
" 13:46:07 &", table)
expected = """\\begin{center}
\\begin{tabular}{lclc}
\\toprule
\\textbf{Dep. Variable:} & TOTEMP & \\textbf{ R-squared: } & 0.995 \\\\
\\textbf{Model:} & OLS & \\textbf{ Adj. R-squared: } & 0.992 \\\\
\\textbf{Method:} & Least Squares & \\textbf{ F-statistic: } & 330.3 \\\\
\\textbf{Date:} & Sun, 07 Apr 2013 & \\textbf{ Prob (F-statistic):} & 4.98e-10 \\\\
\\textbf{Time:} & 13:46:07 & \\textbf{ Log-Likelihood: } & -109.62 \\\\
\\textbf{No. Observations:} & 16 & \\textbf{ AIC: } & 233.2 \\\\
\\textbf{Df Residuals:} & 9 & \\textbf{ BIC: } & 238.6 \\\\
\\textbf{Df Model:} & 6 & \\textbf{ } & \\\\
\\bottomrule
\\end{tabular}
\\begin{tabular}{lcccccc}
& \\textbf{coef} & \\textbf{std err} & \\textbf{t} & \\textbf{P$> |$t$|$} & \\textbf{[0.025} & \\textbf{0.975]} \\\\
\\midrule
\\textbf{GNPDEFL} & 15.0619 & 84.915 & 0.177 & 0.863 & -177.029 & 207.153 \\\\
\\textbf{GNP} & -0.0358 & 0.033 & -1.070 & 0.313 & -0.112 & 0.040 \\\\
\\textbf{UNEMP} & -2.0202 & 0.488 & -4.136 & 0.003 & -3.125 & -0.915 \\\\
\\textbf{ARMED} & -1.0332 & 0.214 & -4.822 & 0.001 & -1.518 & -0.549 \\\\
\\textbf{POP} & -0.0511 & 0.226 & -0.226 & 0.826 & -0.563 & 0.460 \\\\
\\textbf{YEAR} & 1829.1515 & 455.478 & 4.016 & 0.003 & 798.788 & 2859.515 \\\\
\\textbf{constant} & -3.482e+06 & 8.9e+05 & -3.911 & 0.004 & -5.5e+06 & -1.47e+06 \\\\
\\bottomrule
\\end{tabular}
\\begin{tabular}{lclc}
\\textbf{Omnibus:} & 0.749 & \\textbf{ Durbin-Watson: } & 2.559 \\\\
\\textbf{Prob(Omnibus):} & 0.688 & \\textbf{ Jarque-Bera (JB): } & 0.684 \\\\
\\textbf{Skew:} & 0.420 & \\textbf{ Prob(JB): } & 0.710 \\\\
\\textbf{Kurtosis:} & 2.434 & \\textbf{ Cond. No. } & 4.86e+09 \\\\
\\bottomrule
\\end{tabular}
%\\caption{OLS Regression Results}
\\end{center}
Warnings: \\newline
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified. \\newline
[2] The condition number is large, 4.86e+09. This might indicate that there are \\newline
strong multicollinearity or other numerical problems."""
assert_equal(table, expected)
示例7: test_summary
# 需要导入模块: from statsmodels.regression.linear_model import OLS [as 别名]
# 或者: from statsmodels.regression.linear_model.OLS import summary [as 别名]
def test_summary():
# test 734
import re
dta = longley.load_pandas()
X = dta.exog
X["constant"] = 1
y = dta.endog
with warnings.catch_warnings(record=True):
res = OLS(y, X).fit()
table = res.summary().as_latex()
# replace the date and time
table = re.sub("(?<=\n\\\\textbf\{Date:\} &).+?&",
" Sun, 07 Apr 2013 &", table)
table = re.sub("(?<=\n\\\\textbf\{Time:\} &).+?&",
" 13:46:07 &", table)
expected = """\\begin{center}
\\begin{tabular}{lclc}
\\toprule
\\textbf{Dep. Variable:} & TOTEMP & \\textbf{ R-squared: } & 0.995 \\\\
\\textbf{Model:} & OLS & \\textbf{ Adj. R-squared: } & 0.992 \\\\
\\textbf{Method:} & Least Squares & \\textbf{ F-statistic: } & 330.3 \\\\
\\textbf{Date:} & Sun, 07 Apr 2013 & \\textbf{ Prob (F-statistic):} & 4.98e-10 \\\\
\\textbf{Time:} & 13:46:07 & \\textbf{ Log-Likelihood: } & -109.62 \\\\
\\textbf{No. Observations:} & 16 & \\textbf{ AIC: } & 233.2 \\\\
\\textbf{Df Residuals:} & 9 & \\textbf{ BIC: } & 238.6 \\\\
\\textbf{Df Model:} & 6 & \\textbf{ } & \\\\
\\bottomrule
\\end{tabular}
\\begin{tabular}{lccccc}
& \\textbf{coef} & \\textbf{std err} & \\textbf{t} & \\textbf{P$>$$|$t$|$} & \\textbf{[95.0\\% Conf. Int.]} \\\\
\\midrule
\\textbf{GNPDEFL} & 15.0619 & 84.915 & 0.177 & 0.863 & -177.029 207.153 \\\\
\\textbf{GNP} & -0.0358 & 0.033 & -1.070 & 0.313 & -0.112 0.040 \\\\
\\textbf{UNEMP} & -2.0202 & 0.488 & -4.136 & 0.003 & -3.125 -0.915 \\\\
\\textbf{ARMED} & -1.0332 & 0.214 & -4.822 & 0.001 & -1.518 -0.549 \\\\
\\textbf{POP} & -0.0511 & 0.226 & -0.226 & 0.826 & -0.563 0.460 \\\\
\\textbf{YEAR} & 1829.1515 & 455.478 & 4.016 & 0.003 & 798.788 2859.515 \\\\
\\textbf{constant} & -3.482e+06 & 8.9e+05 & -3.911 & 0.004 & -5.5e+06 -1.47e+06 \\\\
\\bottomrule
\\end{tabular}
\\begin{tabular}{lclc}
\\textbf{Omnibus:} & 0.749 & \\textbf{ Durbin-Watson: } & 2.559 \\\\
\\textbf{Prob(Omnibus):} & 0.688 & \\textbf{ Jarque-Bera (JB): } & 0.684 \\\\
\\textbf{Skew:} & 0.420 & \\textbf{ Prob(JB): } & 0.710 \\\\
\\textbf{Kurtosis:} & 2.434 & \\textbf{ Cond. No. } & 4.86e+09 \\\\
\\bottomrule
\\end{tabular}
%\\caption{OLS Regression Results}
\\end{center}"""
assert_equal(table, expected)
示例8: test_OLSsummary
# 需要导入模块: from statsmodels.regression.linear_model import OLS [as 别名]
# 或者: from statsmodels.regression.linear_model.OLS import summary [as 别名]
def test_OLSsummary(self):
# Test that latex output of regular OLS output still contains
# multiple tables
x = [1,5,7,3,5]
x = add_constant(x)
y1 = [6,4,2,7,4]
reg1 = OLS(y1,x).fit()
with warnings.catch_warnings():
warnings.simplefilter("ignore")
actual = reg1.summary().as_latex()
string_to_find = r'''\end{tabular}
\begin{tabular}'''
result = string_to_find in actual
assert(result is True)
示例9: regression
# 需要导入模块: from statsmodels.regression.linear_model import OLS [as 别名]
# 或者: from statsmodels.regression.linear_model.OLS import summary [as 别名]
def regression(aspects, dataset):
asps = list(set(dataset.columns).intersection(aspects))
asps.sort()
aspsMP = list()
for asp in asps:
minus = asp+'_minus'
dataset[minus] = dataset.apply(lambda x: 1 if x[asp] and x[asp+'sent'] == -1 else 0, axis=1)
aspsMP.append(minus)
plus = asp+'_plus'
dataset[plus] = dataset.apply(lambda x: 1 if x[asp] and x[asp+'sent'] == 1 else 0, axis=1)
aspsMP.append(plus)
# overall = 'a_'+asp
# dataset[overall] = dataset.apply(lambda x: x[asp]*x[asp+'sent'], axis=1)
# aspsMP.append(overall)
neutral = asp+'_neutral'
dataset[neutral] = dataset.apply(lambda x: 1 if x[asp] and x[asp+'sent'] == 0 else 0, axis=1)
aspsMP.append(neutral)
# aspsMP.append(asp+'sent')
# MINUS
# PLUS
aspsMP.sort()
dataset['intercept'] = np.ones(len(dataset))
aspsMP = ['intercept'] + aspsMP
# print(len(aspects),len(asps))
model = OLS(dataset['stars'], dataset[aspsMP]).fit()
# model.summary
# print(model.params)
# print(model.pvalues)
return model.summary()
示例10: split
# 需要导入模块: from statsmodels.regression.linear_model import OLS [as 别名]
# 或者: from statsmodels.regression.linear_model.OLS import summary [as 别名]
def split(self,X,Y,max_splits=1000):
"""
- we don't actually need X but we take it for consistency
"""
nsubs=len(Y)
# cycle through until we find a split that is good enough
runctr=0
best_pval=0.
while 1:
runctr+=1
cv=KFold(n_splits=self.nfolds,shuffle=True)
idx=N.zeros((nsubs,self.nfolds)) # this is the design matrix
folds=[]
ctr=0
for train,test in cv.split(Y):
idx[test,ctr]=1
folds.append([train,test])
ctr+=1
lm_y=OLS(Y-N.mean(Y),idx).fit()
if lm_y.f_pvalue>best_pval:
best_pval=lm_y.f_pvalue
best_folds=folds
if lm_y.f_pvalue>self.pthresh:
if self.verbose:
print(lm_y.summary())
return iter(folds)
if runctr>max_splits:
print('no sufficient split found, returning best (p=%f)'%best_pval)
return iter(best_folds)
示例11: test_regression_with_tuples
# 需要导入模块: from statsmodels.regression.linear_model import OLS [as 别名]
# 或者: from statsmodels.regression.linear_model.OLS import summary [as 别名]
def test_regression_with_tuples(self):
i = pandas.Series( [1,2,3,4]*10 , name="i")
y = pandas.Series( [1,2,3,4,5]*8, name="y")
x = pandas.Series( [1,2,3,4,5,6,7,8]*5, name="x")
df = pandas.DataFrame( index=i.index )
df = df.join( i )
endo = df.join( y )
exo = df.join( x )
endo_groups = endo.groupby( ("i",) )
exo_groups = exo.groupby( ("i",) )
exo_Df = exo_groups.agg( [np.sum, np.max] )
endo_Df = endo_groups.agg( [np.sum, np.max] )
reg = OLS(exo_Df[[("x", "sum")]],endo_Df).fit()
interesting_lines = []
for line in str( reg.summary() ).splitlines():
if "('" in line:
interesting_lines.append( line[:38] )
desired = ["Dep. Variable: ('x', 'sum') ",
"('y', 'sum') 1.4595 0.209 ",
"('y', 'amax') 0.2432 0.035 "]
self.assertEqual( desired, interesting_lines )
示例12: add_constant
# 需要导入模块: from statsmodels.regression.linear_model import OLS [as 别名]
# 或者: from statsmodels.regression.linear_model.OLS import summary [as 别名]
import numpy as np
from statsmodels.regression.linear_model import OLS, GLSAR
from statsmodels.tools.tools import add_constant
from statsmodels.datasets import macrodata
import statsmodels.regression.tests.results.results_macro_ols_robust as res
d2 = macrodata.load(as_pandas=False).data
g_gdp = 400*np.diff(np.log(d2['realgdp']))
g_inv = 400*np.diff(np.log(d2['realinv']))
exogg = add_constant(np.c_[g_gdp, d2['realint'][:-1]], prepend=False)
res_olsg = OLS(g_inv, exogg).fit()
print(res_olsg.summary())
res_hc0 = res_olsg.get_robustcov_results('HC1')
print('\n\n')
print(res_hc0.summary())
print('\n\n')
res_hac4 = res_olsg.get_robustcov_results('HAC', maxlags=4, use_correction=True)
print(res_hac4.summary())
print('\n\n')
tt = res_hac4.t_test(np.eye(len(res_hac4.params)))
print(tt.summary())
print('\n\n')
print(tt.summary_frame())
res_hac4.use_t = False
示例13: add_constant
# 需要导入模块: from statsmodels.regression.linear_model import OLS [as 别名]
# 或者: from statsmodels.regression.linear_model.OLS import summary [as 别名]
import numpy as np
from statsmodels.regression.linear_model import OLS, GLSAR
from statsmodels.tools.tools import add_constant
from statsmodels.datasets import macrodata
import statsmodels.regression.tests.results.results_macro_ols_robust as res
d2 = macrodata.load().data
g_gdp = 400*np.diff(np.log(d2['realgdp']))
g_inv = 400*np.diff(np.log(d2['realinv']))
exogg = add_constant(np.c_[g_gdp, d2['realint'][:-1]], prepend=False)
res_olsg = OLS(g_inv, exogg).fit()
print res_olsg.summary()
res_hc0 = res_olsg.get_robustcov_results('HC1')
print '\n\n'
print res_hc0.summary()
print '\n\n'
res_hac4 = res_olsg.get_robustcov_results('HAC', maxlags=4, use_correction=True)
print res_hac4.summary()
print '\n\n'
tt = res_hac4.t_test(np.eye(len(res_hac4.params)))
print tt.summary()
print '\n\n'
print tt.summary_frame()
res_hac4.use_t = False