本文整理汇总了Python中statsmodels.regression.linear_model.OLS.t_test方法的典型用法代码示例。如果您正苦于以下问题:Python OLS.t_test方法的具体用法?Python OLS.t_test怎么用?Python OLS.t_test使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类statsmodels.regression.linear_model.OLS
的用法示例。
在下文中一共展示了OLS.t_test方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: setupClass
# 需要导入模块: from statsmodels.regression.linear_model import OLS [as 别名]
# 或者: from statsmodels.regression.linear_model.OLS import t_test [as 别名]
def setupClass(cls):
R = np.zeros(7)
R[4:6] = [1,-1]
data = longley.load()
data.exog = add_constant(data.exog, prepend=False)
res1 = OLS(data.endog, data.exog).fit()
cls.Ttest1 = res1.t_test(R)
示例2: setupClass
# 需要导入模块: from statsmodels.regression.linear_model import OLS [as 别名]
# 或者: from statsmodels.regression.linear_model.OLS import t_test [as 别名]
def setupClass(cls):
# if skipR:
# raise SkipTest, "Rpy not installed"
# try:
# r.library('car')
# except RPyRException:
# raise SkipTest, "car library not installed for R"
R = np.zeros(7)
R[4:6] = [1,-1]
# self.R = R
data = longley.load()
data.exog = add_constant(data.exog)
res1 = OLS(data.endog, data.exog).fit()
cls.Ttest1 = res1.t_test(R)
示例3: get_tvalue_with_alternative_library
# 需要导入模块: from statsmodels.regression.linear_model import OLS [as 别名]
# 或者: from statsmodels.regression.linear_model.OLS import t_test [as 别名]
def get_tvalue_with_alternative_library(tested_vars, target_vars, covars=None):
"""Utility function to compute tvalues with linalg or statsmodels
Massively univariate linear model (= each target is considered
independently).
Parameters
----------
tested_vars: array-like, shape=(n_samples, n_regressors)
Tested variates, the associated coefficient of which are to be tested
independently with a t-test, resulting in as many t-values.
target_vars: array-like, shape=(n_samples, n_targets)
Target variates, to be approximated with a linear combination of
the tested variates and the confounding variates.
covars: array-like, shape=(n_samples, n_confounds)
Confounding variates, to be fitted but not to be tested
Returns
-------
t-values: np.ndarray, shape=(n_regressors, n_targets)
"""
### set up design
n_samples, n_regressors = tested_vars.shape
n_targets = target_vars.shape[1]
if covars is not None:
n_covars = covars.shape[1]
design_matrix = np.hstack((tested_vars, covars))
else:
n_covars = 0
design_matrix = tested_vars
mask_covars = np.ones(n_regressors + n_covars, dtype=bool)
mask_covars[:n_regressors] = False
test_matrix = np.array([[1.] + [0.] * n_covars])
### t-values computation
try: # try with statsmodels if available (more concise)
from statsmodels.regression.linear_model import OLS
t_values = np.empty((n_targets, n_regressors))
for i in range(n_targets):
current_target = target_vars[:, i].reshape((-1, 1))
for j in range(n_regressors):
current_tested_mask = mask_covars.copy()
current_tested_mask[j] = True
current_design_matrix = design_matrix[:, current_tested_mask]
ols_fit = OLS(current_target, current_design_matrix).fit()
t_values[i, j] = np.ravel(ols_fit.t_test(test_matrix).tvalue)
except: # use linalg if statsmodels is not available
from numpy import linalg
lost_dof = n_covars + 1 # fit all tested variates independently
t_values = np.empty((n_targets, n_regressors))
for i in range(n_regressors):
current_tested_mask = mask_covars.copy()
current_tested_mask[i] = True
current_design_matrix = design_matrix[:, current_tested_mask]
invcov = linalg.pinv(current_design_matrix)
normalized_cov = np.dot(invcov, invcov.T)
t_val_denom_aux = np.diag(
np.dot(test_matrix, np.dot(normalized_cov, test_matrix.T)))
t_val_denom_aux = t_val_denom_aux.reshape((-1, 1))
for j in range(n_targets):
current_target = target_vars[:, j].reshape((-1, 1))
res_lstsq = linalg.lstsq(current_design_matrix, current_target)
residuals = (current_target
- np.dot(current_design_matrix, res_lstsq[0]))
t_val_num = np.dot(test_matrix, res_lstsq[0])
t_val_denom = np.sqrt(
np.sum(residuals ** 2, 0) / float(n_samples - lost_dof)
* t_val_denom_aux)
t_values[j, i] = np.ravel(t_val_num / t_val_denom)
return t_values