๐ท residual plot์์์ error๊ด๋ จ ์ฑ์ง๋ก auto-corrleation์ ๋ฌด์กฐ๊ฑด ์ง๊ณ ๋์ด๊ฐ๋ ๊ฒฝ์ฐ๊ฐ ๋ง์๋ฐ, ํด๋น corrleation์ด ๋ฌด์์ธ์ง ์์๋ณด๊ณ
๐ท ํด๋น auto-correlation์ด ์กด์ฌํ๋ ์ง ์์๋ณด๊ธฐ ์ํ ๋ ๊ฐ์ง ๋ฐฉ๋ฒ - Durbin-Watson test & Breusch-Godfrey test๋ฅผ ์์๋ณด์
* linear regression model ๊ฐ์ → auto-correlation concepts
๐ ์ ์ ์ 2๋ฒ์์ ์์ฐจ - residual์ ๋ํด ์ค๋ช ํ ๋, ์์ฐจ๋ฅผ ์๊ฐํํ residual plot์์ ์ฌ๋ฌ error terms ์ค์ฐจํญ์ด ํ์ฑํ๋ ์ผ์ข ์ pattern์ด ์กด์ฌํ๋ค๋ฉด ์๊ธฐ์๊ด์ฑ auto-correlation์ด ์กด์ฌํ๋ค๊ณ ํ์๋ค.
๐ ์ ๋ฒ coursera ๊ฐ์ข์์ ์ ๊น ๋ค๋ฃฌ ๋ด์ฉ์ ํ์ธํ๋ฉด,,
→ residual plot์ residual plot์ ๋ถํฌ์ ๋ฐ๋ผ ํด๋น linear model์ ์ ์ ์ฑ์ ํ์ธํ ์ ์๋ค.
๐ ์ฆ ์ ๋ ๊ทธ๋ํ์์ ์ ์ํ residual plot์์ error term์ด ์๋ก ์๊ด๊ด๊ณ๋ฅผ ๊ฐ์ง ๋ auto-correlation(๋๋ ์๊ณ์ด ์๋ฃ์์ ๋ง์ด ์ฐ์ฌ serial correlation์ผ๋ก๋ ์ฐ์ธ๋ค.)์ด ์กด์ฌํ๋ค๊ณ ํ๋ฉฐ, ์ผ์ ํ ์๊ฐ๊ฐ๊ฒฉ์ผ๋ก ์ค์ฐจํญ์ด ์๋ก ๋์ผํ ํํ(์๋ผ๋ฆฌ, ๋๋ ์๋ผ๋ฆฌ)๋ฅผ ๋ณด์ด๋ฉด 'positive auto-correlation'์ด๋ผ ํ๊ณ , ์ค์ฐจํญ๋ค์ด ์๋ก ๋ฐ๋ ํํ(์๊ณผ ์)๋ฅผ ๋ณด์ธ๋ค๋ฉด 'negative auto-correlation'์ด๋ผ๊ณ ํ๋ค. ์ด๋ ์ ๋ ์๋ ์๊ด์ฑ์ ๋ณด์ด์ง ์๋๋ค๊ณ ํ๋ฉด 'uncorrelated'๋์๋ค๊ณ ํ๋ฉฐ ์๊ธฐ์๊ด์ฑ์ด ์กด์ฌํ์ง ์๋๋ค๊ณ ํ๋จํ๋ค
(์ฆ, ํ error term $e_k$๊ฐ ์(+)์ผ ๋, ์์ $e_{k+1}$ํญ๋ ์(+)์ ๋ณด์ด๋, ์ฆ ์์ฐจ๋ค์ด ์ผ์ข
์ pattern trend๋ฅผ ํ์ฑํ๋ฉด ์๊ธฐ์๊ด์ฑ)
๐ ์ด๋ ๊ฒ ์๊ธฐ์๊ด์ฑ์ ํ๋จํ๋ ๋ฐฉ๋ฒ์ ํฌ๊ฒ ๋ ๊ฐ์ง๋ก, โ Durbin-Watson test์ โกBreusch-Godfrey test๊ฐ ์กด์ฌํ๋ค (์๋์์ ๋ค๋ฃฐ ์์ !)
๐ ์ฆ ์ ํ๋ชจ๋ธ์์ ๊ฐ์ ์ผ๋ก ๋ ๋, ์๊ธฐ์๊ด์ฑ์ด ์กด์ฌํด์๋ ์๋๋ค๋ ๊ฐ์ ์ด ๊ผญ ๋ค์ด๊ฐ๋ค. ๋น์ฐํ ์ค์ฐจํญ๋ผ๋ฆฌ ์๋ก ์ฐ๊ด์ ๋ณด์ด๋ pattern์ด ์กด์ฌํ๋ค๋ฉด, ๋ณ์๋ค๋ผ๋ฆฌ ๋ชจ๋ ๋ ๋ฆฝ์ ์ด๋ผ๊ณ ๊ฐ์ ํ๊ณ regression modeling์ ์งํํ๋ ๋ฐ์ ํ๋ ธ๊ธฐ ๋๋ฌธ!
(๋ฐ๋ผ์ detection tests๋ก ๋ฐ๋์ ์๊ธฐ์๊ด์ฑ ์ ๋ฌด๋ฅผ ํ์ ํ๋๋ก ํ์)
๐ ์ฃผ๋ก ์๊ณ์ด ์๋ฃ์ ์ ๋ง ๋ง์ด ํ์ฉ๋๋ค. ์ผ์ ํ ์๊ฐ ๊ฐ๊ฒฉ์ ๋ data๋ผ๋ฆฌ ์ฐ๊ด์ฑ์ ๋ณด์ด๋, ์ผ์ข ์ pattern์ด๋ trend๋ฅผ ๋ณด์ด๋ ๊ฒฝ์ฐ๊ฐ ๋งค์ฐ ๋ง๊ธฐ์, ์ฃผ์ด์ง ์๊ณ์ด data์์ ์์ผ๋ก๋ฅผ ์์ธกํ๊ธฐ ์ ์ ์๊ธฐ์๊ด์ฑ์ ๊ผญ ํ์ธํ๊ณ ๊ฐ๋ ๊ฒฝ์ฐ๊ฐ ๋ง๋ค.
๐ Q. ์๊ธฐ์๊ด์ฑ์ด ์กด์ฌํ๋ค๋ฉด?
→ regression์ coefficients ๊ณ์๋ง ์ํฅ์ ๋ฐ์ง ์์ ๋ฟ, standard error๋ p-value๋ ์ฌ์ฉํ ์ ์๋ค. ๋ฐ๋ผ์ ์ฐ๋ฆฌ๋ p-value๋ก ๊ฒฐ๋ก ์ ๋ด๋ฆฌ๋, '์ด๋ค ํน์ ๋ ๋ฆฝ๋ณ์๊ฐ ์ข ์๋ณ์์ ์ํฅ์ ์ค๋ค'๋ผ๊ณ ๊ฒฐ๋ก ์ ๋ด๋ฆด ์ ์๋ค๋ ๋ป!
→ ์ฆ ์ฐ๋ฆฌ๋ ์๊ธฐ์๊ด์ฑ์ ๋ฐ๊ฒฌํ๋ฉด ํด๋น ์ฑ์ง์ ์์จ ์ ์๋๋ฐ, ๋น ์ง ๋ณ์๋ ํจ์ term์ด ๋น ์ง ๊ฒฝ์ฐ๊ฐ ์์ผ๋ฏ๋ก ํด๋น ๋ณ์๋ term์ ์ถ๊ฐํด์ ํด๊ฒฐํ๋ ๊ฒฝ์ฐ๋ ์๋ค.
1> Durbin-Watson test
๐น ๋๋น-์์จ์์ ์์์ผ ํ ์ฑ์ง ์ค์ ํ๋๋ '๊ทผ์ฒ residual term๋ผ๋ฆฌ๋ง' test๋๋ค๋ ์ฑ์ง
(only looks at successive error terms)
๐น Durbin-Watson d-statistic
$d_w = \cfrac{(e_2 - e_1)^2 + (e_3 - e_2)^2 + ... + (e_n - e_{n-1})^2}{e_1^2 + e_2^2 + ... + e_n^2}$
๐น $d_w$๊ฐ์ 0์์ 4๊น์ง์ ๋ฒ์๋ฅผ ๊ฐ์ง๊ณ 2 ๋ถ๊ทผ์ ๊ฐ์ด ๋์ค๋ฉด uncorrelated ๋์๋ค ํ๋จ, 4์ ๊ฐ๊น์ฐ๋ฉด negatively correlated, 0์ ๊ฐ๊น์ฐ๋ฉด positively correlated ๋์๋ค ํ๋จํ๋ค.
๐น range 0 from 4 ($d_w$)
๐น ์ฃผ์ด์ง ๊ด์ธก์น์ ์ n & intercept๋ฅผ ์ ์ธํ regressor ์(์ฆ x ๋ณ์ ๊ฐ์) k ๋ ๊ฐ์ significance level์ ์ฐธ์กฐํ์ฌ ์์ $d_u$์ $d_L$๊ฐ์ ์ ํ๋ค.
(์๋ durbin-watson table์ ์ง์ ๊ตฌ๊ธ๋งํ๋ฉด ์ฐพ์ ์ ์๋ค. ์๋ ์ถ์ฒ ์ฐธ๊ณ )
๐น ์ table๊ณผ ๊ฐ์ด alpha๊ฐ 0.05์ผ ๋๋ฅผ ๋ง์ด ์ฌ์ฉํ๊ณ , k๋ ์ฃผ๋ก 10์ดํ์ธ case๊ฐ ๊ฐ์ฅ ๋น๋ฒํ๊ฒ ์ฌ์ฉ๋๋ค. ($d_L, d_u$ ์์)
* ์ค์ต>
โ time-series data kaggle dataset ์ธ ๊ฐ ์ค๋น & import
- ์ค์คํธ๋ฆฌ์ ์๋ณ ๋งฅ์ฃผ ์๋น๋
- ์ดํธ ์๋น๋
- ์ ๊ธฐ ์์ฐ๋
import numpy as np
import pandas as pd
import matplotlib
import seaborn as sns
import matplotlib.pyplot as pyplot
from matplotlib import pyplot as plt
from statsmodels.formula.api import ols #ols model - residual calculation
from statsmodels.stats.stattools import durbin_watson #durbin_watson method
import statsmodels.tsa.api as smt #visualizing residuals
df = pd.read_csv("monthly-beer-production-in-austr.csv")
df.isnull().sum()
df['Month'] = pd.to_datetime(df['Month'])
โก ์๊ฐํ ๋ฐ dataframe ์์ฑ
plt.figure(figsize=(10,5))
plt.title("Monthly beer production in Austria", fontsize=15)
plt.plot(df["Month"], df["Monthly beer production"], "-", color='#A69037')
plt.grid()
plt.xticks(rotation=90)
plt.show()
lst = list(range(1,476+1))
production = df["Monthly beer production"].to_list()
new = pd.DataFrame({'time':lst, 'production':production})
โข ์ฃผ์ด์ง dataframe์ independent, dependent variable ์ ํ + d-ํต๊ณ๋ ์ถ๋ ฅ + autocorrelation(residual plot) ๊ทธ๋ํ ์๊ฐํ ํจ์ ์์ฑ
def durbin_watson_func(y, X, df, n):
# y - dependent variable
# X - independent variables
# df - dataframe of y and X
# n - (a number of data points - 1)
#fit multiple linear regression model
model = ols(formula = f'{y} ~ {X}', data=df).fit()
print('durbin_watson d statistic: ', durbin_watson(model.resid))
acf = smt.graphics.plot_acf(model.resid, lags=n , alpha=0.05)
โฃ ์ด ์ธ ๊ฐ์ dataset์ time-series ์๊ฐํ์ durbin-watson ๊ฒฐ๊ณผ๋ฅผ ๋น๊ตํ๋ฉด ์๋์ ๊ฐ๋ค.
โค ๊ฒฐ๊ณผ๋ถ์
โ๏ธ ๋งฅ์ฃผ์ ์ ๊ธฐ ์์ฐ๋ ๊ทธ๋ํ๋ the number of data points๊ฐ 300๊ฐ๊ฐ ๋๋๋ค. ๋ฐ๋ผ์ ์๊ณ์ด ๋ฐ์ดํฐ ํน์ฑ์, ๊ทธ๋ฆฌ๊ณ ์๊ฐํ ๊ทธ๋ํ๋ฅผ ๋ณด์์ ๋ ๋์ฒด์ ์ธ ํธ๋ ๋๋ฅผ ์ฝ์ ์ ์์ผ๋ฏ๋ก, ํน์ ๋ฐ์ดํฐ ๊ธฐ์ค ์๋ค๋ก ์๋ก ์ํฅ์ ๋ฐ๋๋ค๊ณ ํ ์ ์๋ค. ์ฆ ์๊ธฐ์๊ด์ฑ์ด ๋๋ค๊ณ ์์ธก์ด ๊ฐ๋ฅํ๋ฐ, ์ค์ ๋ก ํ๋จ autocorrelation ๊ทธ๋ํ๋ฅผ ๋ณด๋ฉด ํน์ ๋ฌด๋ฌ๋ฅผ ๋๋ pattern ํ์ธ์ด ๊ฐ๋ฅํจ. durbin-watson d-ํต๊ณ๋๋ dL๊ฐ๋ณด๋ค ์ ์ ๊ฐ์ผ๋ก ์ธก์ ๋์ด 'positive auto-correlation'์ ๊ฐ๋๋ค๊ณ ๋งํ ์ ์๋ค.
โ๏ธ ๋ฐ๋ฉด์ ์ค๊ฐ ๊ทธ๋ํ ์ดํธ ํ๋งค๋์ ๊ฒฝ์ฐ the number of data points๋ 35๊ฐ๋ฐ์ ์๋๋ค. ์๊ณ์ด ๊ทธ๋ํ์ด๊ธฐ๋ ํ์ง๋ง data ์ ์์ฒด๊ฐ ์ ์ด ์๊ธฐ์๊ด์ด ์์ฉํ๋ค๊ณ ๋ณด๊ธฐ ์ด๋ ค์ธ ๊ฑฐ๋ผ๊ณ ์ถ์ ํ ์ ์๋ค. ์ค์ ๋ก ํ๋จ autocorrelation ๊ทธ๋ํ์์ ๋์ ๋๋ ํจํด์ ์ฐพ๊ธฐ ํ๋ค๊ณ , ์ค์ ๋ก durbin-watson d-ํต๊ณ๋๋ 2์ ๋งค์ฐ ๊ฐ๊น์ด ๊ฐ์ผ๋ก ์๊ธฐ์๊ด์ด ์กด์ฌํ์ง ์๋๋ค๊ณ ๋งํ ์ ์๋ค.
++
breusch-godfrey test ์ถํ ์ฐธ์กฐ
* ์ถ์ฒ1) autocorrelation + two detections + remedies ์ค๋ช https://www.youtube.com/watch?v=UFvDSX3jsYg
* ์ถ์ฒ2) ์๊ธฐ์๊ด์ฑ ์ค๋ช https://m.blog.naver.com/PostView.naver?isHttpsRedirect=true&blogId=yonxman&logNo=220960992282
* ์ถ์ฒ3) auttocorrleation wiki https://en.wikipedia.org/wiki/Autocorrelation
* ์ถ์ฒ4) autocorrelation ๊ฐ๋ https://corporatefinanceinstitute.com/resources/knowledge/other/autocorrelation/
* ์ถ์ฒ5) durbin-watson table https://www.real-statistics.com/statistics-tables/durbin-watson-table/
* ์ถ์ฒ6) ์๊ธฐ์๊ด๊ณ์ ์ค๋ช https://otexts.com/fppkr/graphics-autocorrelation.html
* ์ถ์ฒ7) autocorrelation ์๊ฐํ ์ฐธ์กฐ https://www.youtube.com/watch?v=FiBBpscb6es
* ์ถ์ฒ8) ์ markdown ์ผ๋ถ ์ฐธ์กฐ https://github.com/bhattbhavesh91/durbin-watson-test-python/blob/master/durbin-watson-notebook.ipynb
* code ์ผ๋ถ ์ฐธ์กฐ) https://yganalyst.github.io/etc/visual_2/
* time series dataset(beer production in Austria) ์ถ์ฒ) https://www.kaggle.com/datasets/podsyp/time-series-starter-dataset
'Statistics > Concepts(+codes)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
MLE for the normal distribution (1) | 2022.06.27 |
---|---|
Maximum Likelihood Estimation(MLE) (0) | 2022.06.26 |
Bayesian Theorem (0) | 2022.05.07 |
distributionใpoisson distribution (ํฌ์์ก๋ถํฌ) (0) | 2022.05.06 |
distributionใbinomial distribution (์ดํญ๋ถํฌ) (0) | 2022.05.06 |
๋๊ธ