You can do it all in combn .
If you just want to run a regression on all combinations and extract the second coefficient, which you could do
fun <- function(x) coef(lm(paste(x, collapse="~"), data=iris))[2] combn(names(iris[1:4]), 2, fun)
Then you can expand the function to calculate the spread
fun <- function(x) { est <- coef(lm(paste(x, collapse="~"), data=iris))[2] spread <- iris[,x[1]] - est*iris[,x[2]] adf.test(spread) } out <- combn(names(iris[1:4]), 2, fun, simplify=FALSE) out[[1]]
Compare results to run first manually
est <- coef(lm(Sepal.Length ~ Sepal.Width, data=iris))[2] spread <- iris[,"Sepal.Length"] - est*iris[,"Sepal.Width"] adf.test(spread)
source share