在 Python 中使用 statsmodels 进行回归分析
Regression Analysis with statsmodels in Python
我是 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))
和 print(dir(reg1.fit()))
的输出。我在哪里可以获得这些组件的文档和这些部分的示例?
>>> reg1.__module__
'statsmodels.regression.linear_model'
谷歌搜索为我提供了页面 http://www.statsmodels.org/dev/generated/statsmodels.regression.linear_model.OLS.html,其中包括 link 到 fit
。
我不知道这里有你需要的一切。我希望这是一条腿。
dir() 用于列出模块中的所有属性、方法和变量,就像在 R 中一样 as library(lme4)
方法(class = "merMod")
您也可以尝试 reg1.dict
伙计,这很简单 "googling" / 阅读文档页面。可能令人困惑的是 statsmodels.formula.api
的使用。这是为了提供entering R-style formulas的可能性。
statsmodels 的文档位于此处:StatsModels Index Page. Scroll down until you reach "Table of Contents". There click on Linear Regression。向下滚动到 Module Reference
,有 link 到 Model Classes
和 Result Classes
。
@Bill Bell: it is OLS 已经指出了正确的模型 class。在 methods
下方,您可以找到 fit
文档的 link,其中指出 fit
returns 一个 RegressionResults
对象。
RegressionResults doc page 解释了您感兴趣的属性。
注意:
- 属性 starting/ending 双下划线
__
,例如__class__
等是Python special attributes.
- 您可以通过附加
?
在 Python 解释器内部获得帮助,例如通过键入 reg1?
(很像在 R
中 pre-pend 和 ?
)
关于 Python 的一些要点。
Python 在 python 中有 built-in 离线文档 在 python 解释器中尝试命令 help
>>> help(dir)
>>> help(help)
如果想在线看,可以访问pydocs for generic help. And for package specific help, visit pypi(Python包索引)
现在具体针对你的问题。帮助 statsmodels. which redirects to Homepage
最后,这是一个您可能感兴趣的页面:Fitting models using R-style formulas。
我是 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))
和 print(dir(reg1.fit()))
的输出。我在哪里可以获得这些组件的文档和这些部分的示例?
>>> reg1.__module__
'statsmodels.regression.linear_model'
谷歌搜索为我提供了页面 http://www.statsmodels.org/dev/generated/statsmodels.regression.linear_model.OLS.html,其中包括 link 到 fit
。
我不知道这里有你需要的一切。我希望这是一条腿。
dir() 用于列出模块中的所有属性、方法和变量,就像在 R 中一样 as library(lme4) 方法(class = "merMod") 您也可以尝试 reg1.dict
伙计,这很简单 "googling" / 阅读文档页面。可能令人困惑的是 statsmodels.formula.api
的使用。这是为了提供entering R-style formulas的可能性。
statsmodels 的文档位于此处:StatsModels Index Page. Scroll down until you reach "Table of Contents". There click on Linear Regression。向下滚动到 Module Reference
,有 link 到 Model Classes
和 Result Classes
。
@Bill Bell: it is OLS 已经指出了正确的模型 class。在 methods
下方,您可以找到 fit
文档的 link,其中指出 fit
returns 一个 RegressionResults
对象。
RegressionResults doc page 解释了您感兴趣的属性。
注意:
- 属性 starting/ending 双下划线
__
,例如__class__
等是Python special attributes. - 您可以通过附加
?
在 Python 解释器内部获得帮助,例如通过键入reg1?
(很像在R
中 pre-pend 和?
)
关于 Python 的一些要点。
Python 在 python 中有 built-in 离线文档 在 python 解释器中尝试命令
help
>>> help(dir) >>> help(help)
如果想在线看,可以访问pydocs for generic help. And for package specific help, visit pypi(Python包索引)
现在具体针对你的问题。帮助 statsmodels. which redirects to Homepage
最后,这是一个您可能感兴趣的页面:Fitting models using R-style formulas。