## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 6,
  fig.height = 4,
  fig.align = "center"
)
set.seed(2021)

## ----setup--------------------------------------------------------------------
library(lacunarity)

## ----ex1----------------------------------------------------------------------
x <- rbinom(2000, size = 1, prob = 0.8)
rx <- lac(x)
rx

## ----ex1-plot-----------------------------------------------------------------
plot(rx$s, rx$Ds, log = "xy", type = "b", pch = 19,
     xlab = "scale s", ylab = expression(Lambda(s)),
     main = "Lacunarity curve of a random binary series")
abline(h = 1, lty = 2, col = "grey50")

## ----ex2----------------------------------------------------------------------
block <- c(rep(c(rep(1, 8),  rep(0, 8)),  60),
           rep(c(rep(1, 16), rep(0, 16)), 60))
z <- block
w <- sample(block)               # same ones, gaps reshuffled

mean(z) == mean(w)               # identical density

lz <- lac(z)
lw <- lac(w)
c(clustered = lz$y, shuffled = lw$y)

## ----ex2-plot-----------------------------------------------------------------
ylim <- range(lz$Ds, lw$Ds)
plot(lz$s, lz$Ds, log = "xy", type = "b", pch = 19, ylim = ylim,
     xlab = "scale s", ylab = expression(Lambda(s)),
     main = "Same density, different lacunarity")
lines(lw$s, lw$Ds, type = "b", pch = 1, lty = 2)
legend("topright", c("clustered", "shuffled"),
       pch = c(19, 1), lty = c(1, 2), bty = "n")

## ----ex3----------------------------------------------------------------------
gz <- genlac(z)
gw <- genlac(w)

plot(gz$q, gz$yq, type = "b", pch = 19,
     xlab = "moment order q", ylab = expression(gamma(q)),
     main = "Generalized lacunarity spectrum",
     ylim = range(gz$yq, gw$yq))
lines(gw$q, gw$yq, type = "b", pch = 1, lty = 2)
legend("topleft", c("clustered", "shuffled"),
       pch = c(19, 1), lty = c(1, 2), bty = "n")

