Tim S
Tim S

Reputation: 33

statsmodel AR model answers wrong

I am trying to replicate the following R code in Python

set.seed(1)
x<-w<-rnorm(100)
for (t in 2:100) x[t] = 0.6 * x[t-1] + w[t]
x.ar = ar(x, method="mle")
x.ar$ar
[1] 0.5231187

In python I have the following code.

import scipy.stats as stats
import matplotlib.pylab as plt
import statsmodels.tsa.ar_model as ar_model
x = w = stats.norm.rvs(loc=0, scale=1, size=100)
for i in range(1,100):
    x[i] = 0.6* x[i-1] + w[i]


ar = ar_model.AR(x)
model_ar = ar.fit(method='mle')
print(model_ar.params)
[  9.26969930e-04   8.58915676e-01   2.74538400e+00  -1.49505968e+00
-3.47150385e+00   9.64130378e-02   2.68088493e+00   1.64061441e+00
-1.38189445e+00  -1.65265872e+00   6.33895141e-01   5.68494490e-01
-2.23487269e-01]

In python it seems to fit an order 13 model. How can I make it fit the simplest model?

Upvotes: 2

Views: 5159

Answers (1)

Stefan
Stefan

Reputation: 42905

See docs: for statsmodels.tsa.ar_model.AR.fit(), you either select an information criterion (parameter ic) to determine the number of lags:

Criterion used for selecting the optimal lag length. aic - Akaike Information Criterion bic - Bayes Information Criterion t-stat - Based on last lag hqic - Hannan-Quinn Information Criterion If any of the information criteria are selected, the lag length which results in the lowest value is selected. If t-stat, the model starts with maxlag and drops a lag until the highest lag has a t-stat that is significant at the 95 % level.

or provide a maxlag value. If the latter is missing, statsmodels uses default round(12*(nobs/100.)**(1/4.)) to determine the number of lags to use.

Upvotes: 4

Related Questions