statsmodels 는 통계 분석을 위한 Python 패키지다. statsmodels의 메인 웹사이트는 다음과 같다.
statsmodels에서 제공하는 통계 분석 기능은 꽤 방대한 편이다.
통계 (Statistics)
회귀 분석 (Linear Regression)
시계열 분석 (Time Series Analysis)
특히 선형 회귀분석의 경우 R-style 모형 기술을 가능하게 하는 patsy 패키지를 포함하고 있어 기존에 R을 사용하던 사람들도 쉽게 statsmodels를 쓸 수 있게 되었다.
statsmodels를 사용하여 선형 회귀 분석을 수행하는 간단한 예를 보인다.
In [9]:
import statsmodels.api as sm
import statsmodels.formula.api as smf
# 데이터 로드
dat = sm.datasets.get_rdataset("Guerry", "HistData").data
dat.tail()
Out[9]:
In [10]:
# 회귀 분석
results = smf.ols('Lottery ~ Literacy + np.log(Pop1831)', data=dat).fit()
# 결과 출력
print(results.summary())