LeoFr
LeoFr

Reputation: 131

get specific lags from plot_acf that are over the confidence interval

How do I get from this plot

import pandas as pd
import matplotlib.pyplot as plt
import statsmodels.api as sm

dta = sm.datasets.sunspots.load_pandas().data
dta.index = pd.Index(sm.tsa.datetools.dates_from_range('1700', '2008'))
del dta["YEAR"]
sm.graphics.tsa.plot_acf(dta.values.squeeze(), lags=40)
plt.show()

the specific lags that are over/under the confidence interval?

Upvotes: 0

Views: 1494

Answers (1)

Kevin S
Kevin S

Reputation: 2803

You can use acf rather than the plot interface to get the numerical values. The key step is to center the confidence interval by subtracting the ACF from the confidence interval so that it is centered at 0. The CI that is returned from acf is centered around the estimated ACF value.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import statsmodels.api as sm
from statsmodels.tsa.stattools import acf

dta = sm.datasets.sunspots.load_pandas().data
dta.index = pd.Index(sm.tsa.datetools.dates_from_range('1700', '2008'))
nlags = 40
alpha = 0.05
fft=True
adjusted = False
missing = "none"
bartlett_confint = True
x = dta.SUNACTIVITY
a, ci = acf(x, nlags=40, alpha=0.05)
# Key step
centered_ci = ci - a[:,None]
outside = np.abs(a) >= centered_ci[:,1]
inside = ~outside

print(outside)

which shows

[ True  True  True False  True  True  True False False  True  True  True
  True False False  True  True  True False False  True  True  True False
 False False  True  True False False False False False False False False
 False False False False False]

Upvotes: 3

Related Questions