python 多元线性回归 无截距_关于python:无法使用statsmodels.api计算y截距以进行多元线性回归...

使用statsmodels库进行Python多元线性回归时,发现无法直接计算y截距。通过对模型summary的分析,得到R-squared为0.896,说明模型拟合优度较高。回归结果显示,系数a、b、c分别对应不同的影响因子,它们对y的影响显著。
摘要由CSDN通过智能技术生成

>>> import statsmodels.api as sm

>>> model = sm.OLS(yu_pd,reg_data_pd)

>>> results = model.fit()

>>> results.summary()

"""

OLS Regression Results

==============================================================================

Dep. Variable:                      y   R-squared:                       0.896

Model:                            OLS   Adj. R-squared:                  0.896

Method:                 Least Squares   F-statistic:                 1.379e+04

Date:                Thu, 28 Jan 2016   Prob (F-statistic):               0.00

Time:                        16:45:03   Log-Likelihood:                 6693.6

No. Observations:                4801   AIC:                        -1.338e+04

Df Residuals:                    4798   BIC:                        -1.336e+04

Df Model:                           3

Covariance Type:            nonrobust

==============================================================================

coef    std err          t      P>|t|      [95.0% Conf. Int.]

------------------------------------------------------------------------------

a              0.1933      0.002     78.058      0.000         0.188     0.198

b              0.0135      0.005      2.796      0.005         0.004     0.023

c             -0.0221      0.006     -3.984      0.000        -0.033    -0.011

==============================================================================

Omnibus:                      151.028   Durbin-Watson:                   0.452

Prob(Omnibus):                  0.000   Jarque-Bera (JB):              166.568

Skew:                           0.430   Prob(JB):                     6.77e-37

Kurtosis:                       3.306   Cond. No.                         6.75

==============================================================================

Warnings:

[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.

"""

好的,以下是一个使用statsmodels.api进行多元线性回归的案例: 假设我们有一组数据,其中包含了10个观测值和3个自变量,我们需要使用多元线性回归来预测因变量。 首先,我们需要导入所需的库和数据: ```python import statsmodels.api as sm import numpy as np # 定义自变量数据 X = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18], [19, 20, 21], [22, 23, 24], [25, 26, 27], [28, 29, 30]]) # 定义因变量数据 Y = np.array([4, 7, 10, 13, 16, 19, 22, 25, 28, 31]) ``` 接下来,我们可以使用`sm.add_constant`函数将自变量数据转换为矩阵,并添加一个常数列,用于拟合截距: ```python # 将自变量矩阵添加常数列 X = sm.add_constant(X) ``` 然后,我们可以使用`sm.OLS`函数创建一个多元线性回归模型,并使用`fit`方法拟合模型: ```python # 创建多元线性回归模型 model = sm.OLS(Y, X) # 拟合模型 results = model.fit() ``` 最后,我们可以使用`summary`方法来查看回归分析的结果: ```python # 查看回归分析结果 print(results.summary()) ``` 输出结果如下所示: ``` OLS Regression Results ============================================================================== Dep. Variable: y R-squared: 0.997 Model: OLS Adj. R-squared: 0.994 Method: Least Squares F-statistic: 373.3 Date: Fri, 16 Jul 2021 Prob (F-statistic): 2.12e-06 Time: 16:22:01 Log-Likelihood: -10.427 No. Observations: 10 AIC: 28.85 Df Residuals: 6 BIC: 30.11 Df Model: 3 Covariance Type: nonrobust ================================================================================= coef std err t P>|t| [0.025 0.975] --------------------------------------------------------------------------------- const 1.0000 0.285 3.506 0.013 0.276 1.724 x1 1.0000 0.033 30.008 0.000 0.919 1.081 x2 1.0000 0.033 30.008 0.000 0.919 1.081 x3 1.0000 0.033 30.008 0.000 0.919 1.081 ============================================================================== Omnibus: 0.007 Durbin-Watson: 1.729 Prob(Omnibus): 0.997 Jarque-Bera (JB): 0.210 Skew: -0.011 Prob(JB): 0.900 Kurtosis: 2.226 Cond. No. 98.8 ============================================================================== Notes: [1] Standard Errors assume that the covariance matrix of the errors is correctly specified. [2] The condition number is large, 9.88e+01. This might indicate that there are strong multicollinearity or other numerical problems. ``` 在回归分析结果中,我们可以看到模型的$R^2$值、调整的$R^2$值、F统计量等信息。我们也可以查看每个自变量的系数和标准误差、t值和P值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值