statsmodels.regression.linear_model.OLSResults.t_test¶
-
OLSResults.
t_test
(r_matrix, cov_p=None, scale=None, use_t=None)¶ Compute a t-test for a each linear hypothesis of the form Rb = q
Parameters: r_matrix : array-like, str, tuple
- array : If an array is given, a p x k 2d array or length k 1d array specifying the linear restrictions. It is assumed that the linear combination is equal to zero.
- str : The full hypotheses to test can be given as a string. See the examples.
- tuple : A tuple of arrays in the form (R, q). If q is given, can be either a scalar or a length p row vector.
cov_p : array-like, optional
An alternative estimate for the parameter covariance matrix. If None is given, self.normalized_cov_params is used.
scale : float, optional
An optional scale to use. Default is the scale specified by the model fit.
use_t : bool, optional
If use_t is None, then the default of the model is used. If use_t is True, then the p-values are based on the t distribution. If use_t is False, then the p-values are based on the normal distribution.
Returns: res : ContrastResults instance
The results for the test are attributes of this results instance. The available results have the same elements as the parameter table in summary().
Examples
>>> import numpy as np >>> import statsmodels.api as sm >>> data = sm.datasets.longley.load() >>> data.exog = sm.add_constant(data.exog) >>> results = sm.OLS(data.endog, data.exog).fit() >>> r = np.zeros_like(results.params) >>> r[5:] = [1,-1] >>> print(r) [ 0. 0. 0. 0. 0. 1. -1.]
r tests that the coefficients on the 5th and 6th independent variable are the same.
>>> T_test = results.t_test(r) >>> print(T_test) Test for Constraints ============================================================================== coef std err t P>|t| [0.025 0.975] ------------------------------------------------------------------------------ c0 -1829.2026 455.391 -4.017 0.003 -2859.368 -799.037 ============================================================================== >>> T_test.effect -1829.2025687192481 >>> T_test.sd 455.39079425193762 >>> T_test.tvalue -4.0167754636411717 >>> T_test.pvalue 0.0015163772380899498
Alternatively, you can specify the hypothesis tests using a string
>>> from statsmodels.formula.api import ols >>> dta = sm.datasets.longley.load_pandas().data >>> formula = 'TOTEMP ~ GNPDEFL + GNP + UNEMP + ARMED + POP + YEAR' >>> results = ols(formula, dta).fit() >>> hypotheses = 'GNPDEFL = GNP, UNEMP = 2, YEAR/1829 = 1' >>> t_test = results.t_test(hypotheses) >>> print(t_test) Test for Constraints ============================================================================== coef std err t P>|t| [0.025 0.975] ------------------------------------------------------------------------------ c0 15.0977 84.937 0.178 0.863 -177.042 207.238 c1 -2.0202 0.488 -8.231 0.000 -3.125 -0.915 c2 1.0001 0.249 0.000 1.000 0.437 1.563 ==============================================================================