多項式回歸分析 (Polynomial Regression)


套路29: 多項式回歸分析 (Polynomial Regression)

1. 使用時機: 以自變項的多項式預測一個因變項。
2. 分析類型: 回歸分析(regression analysis)
3. 範例資料: 咪路以河口為起點測量不同距離採樣點河水中的鉛含量,資料如下。
Sample
Dist. (km)
Conc. (mg/L)
1
1.22
40.9
2
1.34
41.8
3
1.51
42.4
4
1.66
43.0
5
1.72
43.4
6
1.93
43.9
7
2.14
44.3
8
2.39
44.7
9
2.51
45.0
10
2.78
45.1
11
2.97
45.4
12
3.17
46.2
13
3.32
47.0
14
3.50
48.6
15
3.53
49.0
16
3.85
49.7
17
3.95
50.0
18
4.11
50.8
19
4.18
51.1
求多項式回歸方程式。

4. 建立資料
import numpy as np
import pandas as pd
x = np.array([1.22, 1.34, 1.51, 1.66, 1.72, 1.93, 2.14, 2.39, 2.51, 2.78, 2.97, 3.17, 3.32, 3.5, 3.53, 3.85, 3.95, 4.11, 4.18])
y = np.array([40.9, 41.8, 42.4, 43, 43.4, 43.9, 44.3, 44.7, 45, 45.1, 45.4, 46.2, 47, 48.6, 49, 49.7, 50, 50.8, 51.1])
df = pd.DataFrame(columns=['y', 'x'])
df['x'] = x
df['y'] = y

5. 執行回歸
import statsmodels.formula.api as smf
d1 = 2
w1 = np.polyfit(x, y, d1)
model1 = np.poly1d(w1)
res = smf.ols(formula='y ~ model(x)', data=df).fit()
res.summary()
結果:
OLS Regression Results                           
==============================================================================
Dep. Variable:                      y   R-squared:                       0.982
Model:                            OLS   Adj. R-squared:                  0.981
Method:                 Least Squares   F-statistic:                     917.5
Date:                Mon, 29 Jul 2019   Prob (F-statistic):           3.11e-16
Time:                        15:26:23   Log-Likelihood:                -10.198
No. Observations:                  19   AIC:                             24.40
Df Residuals:                      17   BIC:                             26.29
Df Model:                           1                                        
Covariance Type:            nonrobust                                        
==============================================================================
                 coef    std err          t      P>|t|      [0.025      0.975]
------------------------------------------------------------------------------
Intercept  -1.634e-13      1.519  -1.08e-13      1.000      -3.205       3.205
model(x)       1.0000      0.033     30.291      0.000       0.930       1.070
==============================================================================
Omnibus:                        2.026   Durbin-Watson:                   0.745
Prob(Omnibus):                  0.363   Jarque-Bera (JB):                0.901
Skew:                           0.521   Prob(JB):                        0.637
Kurtosis:                       3.230   Cond. No.                         697.
==============================================================================



留言

這個網誌中的熱門文章

三因子變異數分析 (Three-Way ANOVA)

比較多組不同變異數獨立樣本平均值檢定 (Welch's Test for Analysis of Variance,parametric)

雙因子變異數分析 (Two-Way ANOVA)