What is the "criteria" to choose a good number of lags?
(instead of a comment) I would go for the minimization of information criteria either AIC or BIC, or any other just choose the parameter
k other than
require(urca) ## An example from example(ur.za) data(nporg) gnp <- na.omit(nporg[, "gnp.r"]) za.gnp <- ur.za(gnp, model="both", lag=2) AIC(eval(attributes(za.gnp)$testreg))  485.0148 BIC(eval(attributes(za.gnp)$testreg))  501.6351
For the efficient implementation you may consult on the stack-overflow, I think. A rough way to go is to run the
for cycle, but it may be too slow.
The original Zivot Andrews paper uses lags to address serial correlation. The number of lags used is determined individually for each potential breakpoint. For each potential breakpoint ZA starts with a common maximum number of lags. For each potential breakpoint the number of lags is reduced sequentially until the longest lag is statistically significant. See ZA for full development.
If your routines are not varying the number of lags for each potential breakpoint then I suggest they are not following ZA. Suggest reading the ZA paper may be useful.