# Lags of ADF Unit Root Test with ur.df {urca} in R

I try to use the function ur.df in package urca to perform the ADF unit root test. However I got confused with its parameters lags and selectlags. I supposed that selectlags could find the optimal number of lags by AIC or BIC information criteria. Now I have some quesions：

1. if I set selectlags="AIC", what does lags do ? R document said the maximum number of lags considered is set by lags, but I don't understand what it means.
2. With codes below, a@testreg outputs 3 diff.lag terms, does it mean that AIC information criteria finds 3 the optimal number of lags ? If so, why does b@testreg not match a@testreg and generate different test statistics ?

code :

library(urca)
set.seed(111)
x <- rnorm(1000)
y <- cumsum(x)
a <- ur.df(y, type = "trend", lags = 10, selectlags = "AIC")
b <- ur.df(y, type = "trend", lags = 3)


Both a and b have 3 diff.lag terms.

> a@testreg

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept)  1.294e-02  6.360e-02   0.203   0.8388
z.lag.1     -1.589e-02  5.552e-03  -2.862   0.0043 **
tt           3.834e-05  1.101e-04   0.348   0.7277
z.diff.lag1  9.969e-03  3.182e-02   0.313   0.7541
z.diff.lag2  4.338e-03  3.182e-02   0.136   0.8916
z.diff.lag3  7.545e-02  3.181e-02   2.372   0.0179 *
---


test statistic of a = -2.8617

> b@testreg

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -3.901e-03  6.263e-02  -0.062  0.95035
z.lag.1     -1.547e-02  5.537e-03  -2.794  0.00531 **
tt           6.247e-05  1.089e-04   0.574  0.56625
z.diff.lag1  1.316e-02  3.163e-02   0.416  0.67754
z.diff.lag2  6.172e-03  3.163e-02   0.195  0.84535
z.diff.lag3  7.943e-02  3.164e-02   2.510  0.01221 *
---


test statistic of b = -2.7939