Skip to main content

상관

상관분석

실습 데이터

df = pd.read_excel('car.xlsx')

pingouin 설치

!pip install pingouin

상관계수 구하기

import pingouin as pg
pg.corr(df.price, df.mileage)
  • n: 표본 크기
  • r: 상관계수
  • CI95: 95% 신뢰구간
  • p-val: p-value (0.05보다 작으면 "둘 사이에 상관관계가 없다"라는 귀무가설을 기각하고 "상관관계가 있다"라는 결론)
  • BF10: 베이즈 팩터 관계가 있을 가능성(1)과 없을 가능성(0)의 비율. 클수록 있을 가능성이 큼
  • power: 검정력. 비슷한 다른 실험에서도 상관관계를 찾을 수 있을 가능성

산점도

plt.scatter(df.mileage, df.price)

자기상관

기온 데이터

기온 데이터 불러오기

temp = pd.read_excel('washington_temp.xlsx')

컬럼으로 나눠진 연, 월, 일을 하나의 날짜로 합침

temp['date'] = pd.to_datetime(temp[['Year', 'Month', 'Day']])

날짜를 인덱스로

temp.set_index('date', inplace=True)

월평균 기온

monthly = temp.resample('M').AvgTemperature.mean()
monthly.plot()

ACF 구하기

from statsmodels.tsa.stattools import acf
acf(monthly)

ACF 그리기

from statsmodels.graphics.tsaplots import plot_acf
plot_acf(monthly)

12개월 차분(전년 동기 대비와 비슷한 개념)하고 NaN 삭제후 ACF

plot_acf(monthly.diff(12).dropna()) 

분기별 맥주 생산량 데이터

beer = pd.read_excel('beer.xlsx', parse_dates=True, index_col='quarter')
beer.production.plot()
plot_acf(beer.production)

차분 Differencing

  • 시계열 데이터를 일정 시점 간의 차이를 구하는 것
  • 시계열에서 선형적 추세를 제거
  • 비정상(non-stationary) 시계열을 정상 시계열로 돌릴 수 있음
beer.production.diff().plot()

차분한 값의 ACF는 계절성이 뚜렷해짐

plot_acf(beer.production.diff().dropna())

차분할 간격의 수정

4개 분기(=1년) 간격으로 차분

beer.production.diff(4).plot()

ACF를 시각화하면, 계절성과 추세가 모두 없어짐

plot_acf(beer.production.diff(4).dropna())