Augmented Dickey-Fuller test performed regression in package urca. Problem with lags

I'm having troubles in understanding the steps performed by the ur.df function in R. I'm performing an Augmented Dickey-Fuller Test with drift to assess if my series has a unit root or not. The first time i chose to set maximum lags equal to 9 and the second time i set the maximum lag equal to 5. The function should select the best lag order through the AIC. I don't know why the statistics of the test where i set the maximum lag=9 is different from the test where i set the maximum lag equal to five. They both should derive from the regression with just two lags because the regression with two lags is the best one according to the AIC. Why are they different? I'm missing something.

These are the code and the results of the test:

    test=ur.df(myseries,type="drift",lags=9,selectlags = "AIC")
summary(test)
###############################################
# Augmented Dickey-Fuller Test Unit Root Test #
###############################################

Test regression drift

Call:
lm(formula = z.diff ~ z.lag.1 + 1 + z.diff.lag)

Residuals:
Min      1Q  Median      3Q     Max
-5.6558 -0.8559 -0.1850  0.9438  3.3900

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept)   0.3351     0.4166   0.804   0.4282
z.lag.1      -0.3662     0.2171  -1.687   0.1032
z.diff.lag1  -0.1267     0.2032  -0.624   0.5381
z.diff.lag2  -0.3465     0.1834  -1.890   0.0696 .
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 1.744 on 27 degrees of freedom
Multiple R-squared:  0.3679,    Adjusted R-squared:  0.2976
F-statistic: 5.238 on 3 and 27 DF,  p-value: 0.005583

Value of test-statistic is: -1.6868 1.5055


It seems that the program set the best lag to 2 according to the AIC. If i set the maximum lag equal to 5 it chooses again the lag 2 as the best one but now the p-value is different. Here are the results

    test=ur.df(myseries,type="drift",lags=5,selectlags = "AIC")

###############################################
# Augmented Dickey-Fuller Test Unit Root Test #
###############################################

Test regression drift

Call:
lm(formula = z.diff ~ z.lag.1 + 1 + z.diff.lag)

Residuals:
Min      1Q  Median      3Q     Max
-5.5543 -0.9269 -0.2216  0.9126  3.4027

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept)  0.44241    0.39802   1.112   0.2749
z.lag.1     -0.45811    0.19678  -2.328   0.0266 *
z.diff.lag1 -0.01737    0.17767  -0.098   0.9227
z.diff.lag2 -0.29120    0.16638  -1.750   0.0900 .
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 1.7 on 31 degrees of freedom
Multiple R-squared:  0.3694,    Adjusted R-squared:  0.3083
F-statistic: 6.052 on 3 and 31 DF,  p-value: 0.002287

Value of test-statistic is: -2.3281 2.9333

• What is your question? – whuber Jan 16 '17 at 16:07
• I don't know why the statistics of the test where i set the maximum lag=9 is different from the test where i set the maximum lag equal to five. They both should derive from the regression with just two lags because the regression with two lags is the best one according to the AIC and so the best regression should provide one test statistic wich should be independent from the maximum lag chosen – Spritz Jan 16 '17 at 16:23
• Maybe when setting the maximum lag to $k$, all models are estimated on a sample where $k$ initial observations are discarded, even though it is technically possible to estimate models with non-maximum $k$ on samples longer than that. (But you need the same sample to be able to compare AIC values.) So probably the underlying data set gets truncated to a different degree for different maximum $k$, and thus the estimates and the test statistics change even if the selected model remains the same. – Richard Hardy Jan 17 '17 at 10:42
• You're right. If i look at the degree of freedom in the output they are different. Thank you, i'll try to replicate these regression manually to check if it's true – Spritz Jan 17 '17 at 11:14
• @RichardHardy Of course!your answer was exactly what I was looking for! – Spritz Jan 21 '17 at 9:08

Perhaps when setting the maximum lag to $k$, all models are estimated on a sample where $k$ initial observations are not considered (but they are used when lags of the original time series are formed), even though it is technically possible to estimate models with non-maximum $k$ on samples longer than that. (Recall that you need the exact same sample to be able to compare AIC values.)
So the underlying data set gets truncated to a different degree for different maximum $k$, and thus the estimates and the test statistics change even if the selected model remains the same.