統計検定にコレログラムという、時系列グラフを1日ずらした自己相関を可視化するグラフについての問題が出題されます。
python で コレログラムを描くライブラリがないか検索したところ、statsmodels を使えばできそうでしたので、実際に試してみました。
結果を以下に記載します。
入力データ、実施内容について
-
入力データ
Google Search Console のデータを使用し、16月間の表示数 の変遷の相関を評価します。 -
実施内容
- 時系列データの日次でのコレログラムを描画する。
- 時系列データの月次でのコレログラムを描画する。
時系列データの日次でのコレログラムを描画する。
Google Search Console のデータを取得。
ライブラリ searchconsole を使って、データを取得します。
import pandas as pd
import searchconsole
# サービスアカウント のキーを指定して、認証する
account = searchconsole.authenticate(service_account='./client_secrets.json')
# データの取得
web_property = account['https://www.monotalk.xyz/']
report = web_property.query.range('today', days=-480).dimension('date').limit(25000).get()
# pandas データフレームに変換し、必要な項目のみにする
df = report.to_dataframe()
df = df.loc[:,['date','impressions']]
# 日付文字列を日付形式に変換
df['date'] = pd.to_datetime(df['date'])
df = df.set_index('date')
コレログラムを描画する
plot_acf、plot_pacf を使うと、コレログラム と、棄却限界値を示す直線を描画することができます。
plot_acf は、自己相関係数 のコレログラム、plot_pacf は 偏自己相関係数 のコレログラム を描画できます。
import statsmodels.api as sm
import matplotlib.pyplot as plt
fig, (ax1, ax2) = plt.subplots(nrows=2, sharex=True, figsize=(14,12))
sm.graphics.tsa.plot_acf(df['impressions'], lags=21, ax=ax1)
sm.graphics.tsa.plot_pacf(df['impressions'], lags=21, ax=ax2)
グラフからわかること
あたり前な気がしますが、グラフから以下のことがいえそうです。
* 前日のデータとは強い相関がある。
* 増加傾向にある。
* 7日で周期性がある。
時系列データの月次でのコレログラムを描画する。
Google Search Console のデータを取得。Index を付与。
日付ごとのデータを、月次でグラフ描画するため、DatetimeIndex の month と year をindex 付与します。
import pandas as pd
import searchconsole
# サービスアカウント のキーを指定して、認証する
account = searchconsole.authenticate(service_account='./client_secrets.json')
# データの取得
web_property = account['https://www.monotalk.xyz/']
report = web_property.query.range('today', days=-480).dimension('date').limit(25000).get()
# pandas データフレームに変換し、必要な項目のみにする
df = report.to_dataframe()
df = df.loc[:,['date','impressions']]
# 日付文字列を日付形式に変換
df['date'] = pd.to_datetime(df['date'])
df = df.set_index('date')
# 月と、年をindex に付与、集計する
df2 = df.set_index([df.index.year, df.index.month, df.index])
df2.index.names = [ 'year', 'month', 'date']
df2 = df2.sum(level=['year', 'month'])
df2 = df2.query('not(year >= 2018 and month >= 8)')
# 時系列のグラフ描画
%matplotlib inline
df2.plot()
df2
impressions | ||
---|---|---|
year | month | |
2017 | 4 | 18983.0 |
5 | 46722.0 | |
6 | 46320.0 | |
7 | 52608.0 | |
8 | 62658.0 | |
9 | 80690.0 | |
10 | 93186.0 | |
11 | 97685.0 | |
12 | 94242.0 | |
2018 | 1 | 107181.0 |
2 | 107336.0 | |
3 | 171040.0 | |
4 | 219303.0 | |
5 | 254596.0 | |
6 | 262584.0 | |
7 | 271160.0 |
%matplotlib osx
import statsmodels.api as sm
import matplotlib.pyplot as plt
fig, (ax1, ax2) = plt.subplots(nrows=2, sharex=True, figsize=(14,12))
sm.graphics.tsa.plot_acf(df2['impressions'], lags=8, ax=ax1)
sm.graphics.tsa.plot_pacf(df2['impressions'], lags=8, ax=ax2)
グラフからわかること
前月のデータとは強い相関があることは、わかりましたが、勝手に期待していたようなコレログラムは描画されませんでした。
正直、描画結果をあまり理解できてないのですが、時系列で、impressions は、基本的に増加傾向ではあるのですが、停滞、減少する時期があって、その後また、上昇する時期がきています。
ラグ数が少なくて、周期がわかりません。集計の間隔を2週間とか1週間単位で見ると、よかったりするのかもしれません。
参考
実施時に、以下の記事を参考にしました。
- 時系列解析_理論編 | Logics of Blue
- サイトのPV数をARMAモデルで推定してみる – JBClub
- SARIMAで時系列データの分析(PV数の予測) - kumilog.net
- 偏自己相関関数(PACF)を解釈する - Minitab
以上です。
コメント