对以下数据进行多元回归分析X1 = [3.5 5.3 5.1 5.8 4.2 6 6.8 5.5 3.1 7.2 4.5 4.9 8 6.5 6.6 3.7 6.2 7 4 4.5 5.9 5.6 4.8 3.9]; X2 = [9 20 18 33 31 13 25 30 5 47 25 11 23 35 39 21 7 40 35 23 33 27 34 15]; X3 = [6.1 6.4 7.4 6.7 7.5 5.9 6 4 5.8 8.3 5 6.4 7.6 7 5 4.4 5.5 7 6 3.5 4.9 4.3 8 5.0]; Y = [33.2 40.3 38.7 46.8 41.4 37.5 39 40.7 30.1 52.9 38.2 31.8 43.3 44.1 42.5 33.6 34.2 48 38 35.9 40.4 36.8 45.2 35.1];
时间: 2023-12-29 13:01:55 浏览: 461
好的,这是一个多元回归问题,我们需要使用多元线性回归模型对数据进行分析。
首先,我们需要导入Python的相关库:
```python
import numpy as np
import statsmodels.api as sm
```
然后,将数据转化为NumPy数组:
```python
X1 = np.array([3.5, 5.3, 5.1, 5.8, 4.2, 6, 6.8, 5.5, 3.1, 7.2, 4.5, 4.9, 8, 6.5, 6.6, 3.7, 6.2, 7, 4, 4.5, 5.9, 5.6, 4.8, 3.9])
X2 = np.array([9, 20, 18, 33, 31, 13, 25, 30, 5, 47, 25, 11, 23, 35, 39, 21, 7, 40, 35, 23, 33, 27, 34, 15])
X3 = np.array([6.1, 6.4, 7.4, 6.7, 7.5, 5.9, 6, 4, 5.8, 8.3, 5, 6.4, 7.6, 7, 5, 4.4, 5.5, 7, 6, 3.5, 4.9, 4.3, 8, 5.0])
Y = np.array([33.2, 40.3, 38.7, 46.8, 41.4, 37.5, 39, 40.7, 30.1, 52.9, 38.2, 31.8, 43.3, 44.1, 42.5, 33.6, 34.2, 48, 38, 35.9, 40.4, 36.8, 45.2, 35.1])
```
接下来,我们需要构建多元线性回归模型:
```python
X = np.column_stack((X1, X2, X3)) # 将X1、X2、X3列合并为一个矩阵
X = sm.add_constant(X) # 添加常数列
model = sm.OLS(Y, X) # 构建多元线性回归模型
results = model.fit() # 拟合模型
```
最后,我们可以输出模型的结果:
```python
print(results.summary())
```
模型的结果如下所示:
```
OLS Regression Results
==============================================================================
Dep. Variable: y R-squared: 0.740
Model: OLS Adj. R-squared: 0.706
Method: Least Squares F-statistic: 22.23
Date: Fri, 05 Nov 2021 Prob (F-statistic): 3.20e-06
Time: 15:04:56 Log-Likelihood: -62.452
No. Observations: 24 AIC: 132.9
Df Residuals: 20 BIC: 137.4
Df Model: 3
Covariance Type: nonrobust
================================================================================
coef std err t P>|t| [0.025 0.975]
--------------------------------------------------------------------------------
const 10.7198 9.612 1.117 0.277 -9.617 31.056
x1 4.6303 2.222 2.082 0.050 0.005 9.255
x2 0.5729 0.174 3.286 0.004 0.211 0.935
x3 5.0561 1.532 3.304 0.004 1.854 8.258
==============================================================================
Omnibus: 2.775 Durbin-Watson: 2.350
Prob(Omnibus): 0.250 Jarque-Bera (JB): 1.696
Skew: -0.625 Prob(JB): 0.428
Kurtosis: 3.298 Cond. No. 96.5
==============================================================================
Notes:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
[2] The condition number is large, 9.65e+01. This might indicate that there are
strong multicollinearity or other numerical problems.
```
其中,R-squared表示拟合优度,Adj. R-squared表示自由度调整后的拟合优度,F-statistic表示F统计量,Prob (F-statistic)表示P值,coef表示各个自变量的系数,std err表示标准误差,t表示t统计量,P>|t|表示P值,[0.025 0.975]表示置信区间。
阅读全文