python 回归分析

pwd
‘d:\\python\\exerise-df\\df-data-analysis’
from scipy import stats
import pandas as pd
import numpy as np
from statsmodels.formula.api import ols
import statsmodels.api as sm
from statsmodels.stats.anova import anova_lm
from statsmodels.stats.multicomp import pairwise_tukeyhsd
import matplotlib.pyplot as plt

单变量分析分析

dat = pd.read_csv("simple-resgreesion.csv")
dat.head()
Nweight
058115
159117
260120
361123
462126
model = ols('weight ~ N',dat).fit()
print(model.summary())
                            OLS Regression Results                            
==============================================================================
Dep. Variable:                 weight   R-squared:                       0.991
Model:                            OLS   Adj. R-squared:                  0.990
Method:                 Least Squares   F-statistic:                     1433.
Date:                Wed, 27 Sep 2017   Prob (F-statistic):           1.09e-14
Time:                        14:49:40   Log-Likelihood:                -26.541
No. Observations:                  15   AIC:                             57.08
Df Residuals:                      13   BIC:                             58.50
Df Model:                           1                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [95.0% Conf. Int.]
------------------------------------------------------------------------------
Intercept    -87.5167      5.937    -14.741      0.000      -100.343   -74.691
N              3.4500      0.091     37.855      0.000         3.253     3.647
==============================================================================
Omnibus:                        2.396   Durbin-Watson:                   0.315
Prob(Omnibus):                  0.302   Jarque-Bera (JB):                1.660
Skew:                           0.789   Prob(JB):                        0.436
Kurtosis:                       2.596   Cond. No.                         982.
==============================================================================

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

多项式回归分析

dat2 = pd.read_csv("duoxiangshi.csv")
dat2.head()
Nweight
058115
159117
260120
361123
462126
mod = ols('weight ~ N + I(N**2)',dat2).fit()
print(mod.summary())
                            OLS Regression Results                            
==============================================================================
Dep. Variable:                 weight   R-squared:                       0.999
Model:                            OLS   Adj. R-squared:                  0.999
Method:                 Least Squares   F-statistic:                 1.139e+04
Date:                Wed, 27 Sep 2017   Prob (F-statistic):           2.13e-20
Time:                        14:59:57   Log-Likelihood:                -5.2563
No. Observations:                  15   AIC:                             16.51
Df Residuals:                      12   BIC:                             18.64
Df Model:                           2                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [95.0% Conf. Int.]
------------------------------------------------------------------------------
Intercept    261.8782     25.197     10.393      0.000       206.979   316.777
N             -7.3483      0.778     -9.449      0.000        -9.043    -5.654
I(N ** 2)      0.0831      0.006     13.891      0.000         0.070     0.096
==============================================================================
Omnibus:                        2.449   Durbin-Watson:                   1.144
Prob(Omnibus):                  0.294   Jarque-Bera (JB):                1.033
Skew:                           0.049   Prob(JB):                        0.597
Kurtosis:                       1.718   Cond. No.                     1.09e+06
==============================================================================

多变量回归分析

dat = pd.read_csv("mul-regression.csv")
dat.head()
x1x2x3x4y
030.833.050.090520.8
123.633.628.064195.0
231.534.036.682424.0
319.832.036.070213.5
427.726.047.274403.3
mod = ols('y ~ x1 + x2 + x3 + x4',dat).fit()
print(mod.summary())
                            OLS Regression Results                            
==============================================================================
Dep. Variable:                      y   R-squared:                       0.894
Model:                            OLS   Adj. R-squared:                  0.866
Method:                 Least Squares   F-statistic:                     31.78
Date:                Wed, 27 Sep 2017   Prob (F-statistic):           3.66e-07
Time:                        14:52:33   Log-Likelihood:                -97.454
No. Observations:                  20   AIC:                             204.9
Df Residuals:                      15   BIC:                             209.9
Df Model:                           4                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [95.0% Conf. Int.]
------------------------------------------------------------------------------
Intercept   -625.3583    114.378     -5.467      0.000      -869.150  -381.566
x1            15.1962      2.127      7.146      0.000        10.663    19.729
x2             7.3785      1.889      3.907      0.001         3.353    11.404
x3             9.5034      1.342      7.082      0.000         6.643    12.364
x4            -0.8468      1.493     -0.567      0.579        -4.029     2.335
==============================================================================
Omnibus:                        0.492   Durbin-Watson:                   1.620
Prob(Omnibus):                  0.782   Jarque-Bera (JB):                0.578
Skew:                          -0.294   Prob(JB):                        0.749
Kurtosis:                       2.409   Cond. No.                     1.38e+03
==============================================================================
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值