python中回归分析_Python中statsmodels的回归分析

我是Python新手,正在学习如何用Python中的statsmodels进行回归分析(从R移到Python并以R的方式思考)。我的最低工作示例如下:Income = [80, 100, 120, 140, 160, 180, 200, 220, 240, 260]

Expend = [70, 65, 90, 95, 110, 115, 120, 140, 155, 150]

import pandas as pd

df1 = pd.DataFrame(

{'Income': Income,

'Expend': Expend

})

#regression with formula

import statsmodels.formula.api as smf

#instantiation

reg1 = smf.ols('Expend ~ Income', data = df1)

#members of reg object

print(dir(reg1))

['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_data_attr', '_df_model', '_df_resid', '_fit_ridge', '_get_init_kwds', '_handle_data', '_init_keys', '_setup_score_hess', 'data', 'df_model', 'df_resid', 'endog', 'endog_names', 'exog', 'exog_names', 'fit', 'fit_regularized', 'formula', 'from_formula', 'get_distribution', 'hessian', 'information', 'initialize', 'k_constant', 'loglike', 'nobs', 'predict', 'rank', 'score', 'weights', 'wendog', 'wexog', 'whiten']

#members of the object provided by the modelling.

print(dir(reg1.fit()))

['HC0_se', 'HC1_se', 'HC2_se', 'HC3_se', '_HCCM', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_cache', '_data_attr', '_get_robustcov_results', '_is_nested', '_wexog_singular_values', 'aic', 'bic', 'bse', 'centered_tss', 'compare_f_test', 'compare_lm_test', 'compare_lr_test', 'condition_number', 'conf_int', 'conf_int_el', 'cov_HC0', 'cov_HC1', 'cov_HC2', 'cov_HC3', 'cov_kwds', 'cov_params', 'cov_type', 'df_model', 'df_resid', 'eigenvals', 'el_test', 'ess', 'f_pvalue', 'f_test', 'fittedvalues', 'fvalue', 'get_influence', 'get_prediction', 'get_robustcov_results', 'initialize', 'k_constant', 'llf', 'load', 'model', 'mse_model', 'mse_resid', 'mse_total', 'nobs', 'normalized_cov_params', 'outlier_test', 'params', 'predict', 'pvalues', 'remove_data', 'resid', 'resid_pearson', 'rsquared', 'rsquared_adj', 'save', 'scale', 'ssr', 'summary', 'summary2', 't_test', 'tvalues', 'uncentered_tss', 'use_t', 'wald_test', 'wald_test_terms', 'wresid']

我想了解print(dir(reg1))和{}的输出。在哪里可以得到这些组件的文档和这些组件的示例?在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
StatsmodelsPython用于统计建模和计量经济学的库,它提供了各种统计模型,包括线性回归、时间序列分析等。在时间序列分析,ARIMA模型是一种常用的模型。 ARIMA模型是自回归移动平均模型的缩写,它是一种广义的线性模型,常用于描述时间序列数据的自相关结构和随机性。ARIMA模型可以分为AR(自回归)、MA(移动平均)和差分(I)三部分,其AR是指用当前值的前几个值来预测当前值,MA是指用当前误差的前几个值来预测当前误差,差分是指对时间序列进行差分处理,使其变得平稳。 在Python,使用Statsmodels的ARIMA模型进行时间序列分析可以分为以下几个步骤: 1. 导入相关库 ```python import pandas as pd import numpy as np import statsmodels.api as sm import matplotlib.pyplot as plt ``` 2. 读取数据 ```python data = pd.read_csv("data.csv", index_col=0, parse_dates=True) ``` 3. 绘制时间序列图 ```python plt.plot(data) plt.show() ``` 4. 确定模型阶数 可以使用ACF和PACF图来确定ARIMA模型的阶数。ACF图展示了时间序列与其滞后版本之间的自相关性,PACF图展示了当前时间序列与其滞后版本之间的部分自相关性。根据ACF和PACF图的信息,可以确定ARIMA模型的p、d和q参数。 ```python fig, ax = plt.subplots(2,1) sm.graphics.tsa.plot_acf(data, lags=30, ax=ax[0]) sm.graphics.tsa.plot_pacf(data, lags=30, ax=ax[1]) plt.show() ``` 5. 拟合模型 根据确定的ARIMA模型阶数,使用ARIMA()函数拟合时间序列数据。 ```python model = sm.tsa.ARIMA(data, order=(p,d,q)) results = model.fit() ``` 6. 模型诊断 使用plot_diagnostics()函数进行模型诊断,检查残差是否符合白噪声假设。 ```python results.plot_diagnostics(figsize=(15, 12)) plt.show() ``` 7. 预测 使用forecast()函数进行预测。 ```python forecast = results.forecast(steps=10) ``` 以上就是使用PythonStatsmodels包进行时间序列分析ARIMA模型的步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值