suppressWarnings(RNGversion("3.5.0"))
set.seed(42)

A simple example

data(diamonds, package = 'ggplot2')
diamonds <- data.frame(diamonds)
head(diamonds)
nrow(diamonds)

In this example we sample the diamonds data set and pick a subset of 100 individuals using the cLHS method. To reduce the length of the optimisation step to 1000 iterations to save computing time. This is controlled through the iter option. The progress bar is disabled because it doesn't renders well in the vignette. By default, the index of the selected individuals in the original object are returned.

library(clhs)
res <- clhs(diamonds, size = 100, use.cpp = TRUE)
str(res)

Tweaking the parameters

(work in progress)

Including existing samples in the sampling routine

This functionality is controlled by the include option, which can be used to specify the row indices of samples that needs to be included in the final sampled set.

suppressWarnings(RNGversion("3.5.0"))
set.seed(1)

candidates_samples <- data.frame(
  x = runif(500),
  y = rnorm(500, mean = 0.5, sd = 0.5)
)

existing_samples <- data.frame(
  x = runif(5),
  y = runif(5)
) 

res <- clhs(
  x = rbind(existing_samples, candidates_samples), 
  size = 10, 
  include = 1:5
)

In this case we have 5 individuals (red triangles) that need to be retained in the selected set of samples:

suppressPackageStartupMessages(library(ggplot2))

res_df <- rbind(existing_samples, candidates_samples)
res_df$mandatory_sample <- c(rep(TRUE, length.out = 5), rep(FALSE, length.out = 500))
res_df$selected_sample <- FALSE
res_df$selected_sample[res] <- TRUE

p0 <- ggplot(data = res_df, mapping = aes(x = x, y = y)) +
  geom_point(aes(colour = mandatory_sample, size = mandatory_sample, shape = mandatory_sample)) +
  scale_colour_manual(values = c("grey70", "red")) +
  scale_size_manual(values = c(2, 4)) +
  theme_bw()

print(p0)

The red individuals are the selected samples. Note the triangles showing the samples that were compulsory:

p1 <- ggplot(data = res_df, mapping = aes(x = x, y = y)) +
  geom_point(aes(colour = selected_sample, size = selected_sample, shape = mandatory_sample)) +
  scale_colour_manual(values = c("grey70", "red")) +
  scale_size_manual(values = c(2, 4)) +
  theme_bw()

print(p1)

Cost-constrained implementation

(work in progress)

diamonds$cost <- runif(nrow(diamonds))
res_cost <- clhs(diamonds, size = 100, progress = FALSE, iter = 1000, cost = 'cost')

DLHS

(work in progress)

Plotting the results

If you want to report on the cLHS results, e.g. plot the evolution of the objective function, or compare the distribution of attributes in the initial object and in the sampled subset, you need to switch the simple option to FALSE. Instead f simply returning a numeric vector giving the index of the sampled individuals in the original object, a specific, more complex will be returned. This object can be handled by a specific plot method:

res <- clhs(diamonds, size = 100,cost = "cost", simple = FALSE, progress = FALSE, iter = 2000)
plot(res,c("obj","cost"))

The default plotting method plots the evolution of the objective function with the number of iterations. However, you can get more details using the modes option, which controls which indicators are plotted. Three modes can be simultaneously plotted:

These modes should be given as a vector of characters.

res_cost <- clhs(diamonds, size = 100, progress = FALSE, iter = 1000, cost = 'cost', simple = FALSE, use.cpp = F)
plot(res_cost, c('obj', 'cost'))
plot(res_cost, c('obj', 'cost', 'box'))


pierreroudier/clhs documentation built on April 21, 2022, 2:05 p.m.