library(tseries)

(x1 <- as.factor(rep(c(0, 1), each = 10)))
(x2 <- as.factor(rep(c(0, 1), time = 10)))

runs.test(x1)
runs.test(x2)


(x3 <- as.factor(sample(x = 0:1, size = 20, replace = T)))

runs.test(x3)

Times <- 1000000
Perm <- vector(length = Times)
for (i in 1:Times) {
  x_s <- sample(c(1, 0), 20, replace = T)
  Perm[i] <- sum(abs(as.numeric(x_s[1:19]) - as.numeric(x_s[c(2:20)])))
}
sum(Perm >= 19) / Times 
