x0=np.array(sn.iloc[:,0:n-1])
y0=np.array(sn.iloc[:,n-1])
results = sm.OLS(y0, np.c_[np.ones((m,1)),x0]).fit()
print(results.summary())
OLS Regression Results
==============================================================================
Dep. Variable: y R-squared: 0.982
Model: OLS Adj. R-squared: 0.974
Method: Least Squares F-statistic: 111.5
Date: Fri, 31 Jul 2020 Prob (F-statistic): 4.76e-07
Time: 01:16:14 Log-Likelihood: -26.918
No. Observations: 13 AIC: 63.84
Df Residuals: 8 BIC: 66.66
Df Model: 4
Covariance Type: nonrobust
==============================================================================
coef std err t P>|t| [0.025 0.975]
------------------------------------------------------------------------------
const 62.4054 70.071 0.891 0.399 -99.179 223.989
x1 1.5511 0.745 2.083 0.071 -0.166 3.269
x2 0.5102 0.724 0.705 0.501 -1.159 2.179
x3 0.1019 0.755 0.135 0.896 -1.638 1.842
x4 -0.1441 0.709 -0.203 0.844 -1.779 1.491
==============================================================================
Omnibus: 0.165 Durbin-Watson: 2.053
Prob(Omnibus): 0.921 Jarque-Bera (JB): 0.320
Skew: 0.201 Prob(JB): 0.852
Kurtosis: 2.345 Cond. No. 6.06e+03
==============================================================================
Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
[2] The condition number is large, 6.06e+03. This might indicate that there are
strong multicollinearity or other numerical problems.
D:\Program Files\anaconda\lib\site-packages\scipy\stats\stats.py:1450: UserWarning: kurtosistest only valid for n>=20 ... continuing anyway, n=13
"anyway, n=%i" % int(n))
r=np.corrcoef(x0.T)
xd=zscore(x0)
yd=zscore(y0)
pca=PCA()
pca.fit(xd)
PCA(copy=True, iterated_power='auto', n_components=None, random_state=None,
svd_solver='auto', tol=0.0, whiten=False)
pca.explained_variance_ratio_#计算各个变量的贡献率
array([5.58926009e-01, 3.94016518e-01, 4.66515373e-02, 4.05936433e-04])
num=3#由于我们使用jupyter演示程序,可能不好交互式
pca_=PCA(n_components=num)
xd_=pca_.fit_transform(xd)
results = sm.OLS(yd, np.c_[np.ones((m,1)),xd_]).fit()
print(results.summary())
#原MATLAB程序中还做了使系数全为正的处理,在此报告中,x1为负,但是其实只要转成正的就行
OLS Regression Results
==============================================================================
Dep. Variable: y R-squared: 0.982
Model: OLS Adj. R-squared: 0.976
Method: Least Squares F-statistic: 164.9
Date: Fri, 31 Jul 2020 Prob (F-statistic): 3.50e-08
Time: 01:16:16 Log-Likelihood: 7.7143
No. Observations: 13 AIC: -7.429
Df Residuals: 9 BIC: -5.169
Df Model: 3
Covariance Type: nonrobust
==============================================================================
coef std err t P>|t| [0.025 0.975]
------------------------------------------------------------------------------
const 2.012e-16 0.045 4.52e-15 1.000 -0.101 0.101
x1 -0.6570 0.030 -22.045 0.000 -0.724 -0.590
x2 0.0083 0.035 0.234 0.820 -0.072 0.089
x3 0.3028 0.103 2.935 0.017 0.069 0.536
==============================================================================
Omnibus: 0.246 Durbin-Watson: 1.943
Prob(Omnibus): 0.884 Jarque-Bera (JB): 0.416
Skew: 0.162 Prob(JB): 0.812
Kurtosis: 2.186 Cond. No. 3.46
==============================================================================
Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
D:\Program Files\anaconda\lib\site-packages\scipy\stats\stats.py:1450: UserWarning: kurtosistest only valid for n>=20 ... continuing anyway, n=13
"anyway, n=%i" % int(n))
xback=np.dot(xd_,pca.components_[:3])
results = sm.OLS(yd, np.c_[np.ones((m,1)),xback]).fit()
print(results.summary())
OLS Regression Results
==============================================================================
Dep. Variable: y R-squared: 0.982
Model: OLS Adj. R-squared: 0.976
Method: Least Squares F-statistic: 164.9
Date: Fri, 31 Jul 2020 Prob (F-statistic): 3.50e-08
Time: 01:16:23 Log-Likelihood: 7.7143
No. Observations: 13 AIC: -7.429
Df Residuals: 9 BIC: -5.169
Df Model: 3
Covariance Type: nonrobust
==============================================================================
coef std err t P>|t| [0.025 0.975]
------------------------------------------------------------------------------
const 2.012e-16 0.045 4.52e-15 1.000 -0.101 0.101
x1 0.5130 0.073 6.992 0.000 0.347 0.679
x2 0.2787 0.039 7.078 0.000 0.190 0.368
x3 -0.0608 0.070 -0.866 0.409 -0.220 0.098
x4 -0.4229 0.030 -13.871 0.000 -0.492 -0.354
==============================================================================
Omnibus: 0.246 Durbin-Watson: 1.943
Prob(Omnibus): 0.884 Jarque-Bera (JB): 0.416
Skew: 0.162 Prob(JB): 0.812
Kurtosis: 2.186 Cond. No. 8.35e+15
==============================================================================
Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
[2] The smallest eigenvalue is 4.17e-31. This might indicate that there are
strong multicollinearity problems or that the design matrix is singular.