## ----setup, include=FALSE----------------------------------------------------- knitr::opts_chunk$set(collapse = TRUE, comment = "#>", fig.width = 7, fig.height = 5) library(stream) set.seed(1000) ## ----benchmark---------------------------------------------------------------- stream <- DSD_Benchmark(1) stream ## ----snapshots, fig.height=7-------------------------------------------------- old_par <- par(mfrow = c(2, 2)) for (i in seq_len(4)) { plot(stream, n = 250, xlim = c(0, 1), ylim = c(0, 1), main = paste("Starting at point", 1 + (i - 1) * 1650)) arrows(0.15, 0.85, 0.85, 0.15, col = "gray70", lwd = 2) arrows(0.15, 0.15, 0.85, 0.85, col = "gray70", lwd = 2) if (i < 4) invisible(get_points(stream, n = 1400)) } par(old_par) ## ----static-pitfall----------------------------------------------------------- set.seed(0) stream <- DSD_Memory(DSD_Benchmark(1), n = 5000) model <- DSC_DBSTREAM(r = 0.05, lambda = 0.01) update(model, stream, n = 1000) # new points right after the training data evaluate_static(model, stream, n = 200, measure = c("purity", "crand"), type = "macro", assign = "micro") # points from late in the stream; the model has not seen the movement invisible(get_points(stream, n = 3000)) evaluate_static(model, stream, n = 200, measure = c("purity", "crand"), type = "macro", assign = "micro") ## ----fixed-stream------------------------------------------------------------- set.seed(0) stream <- DSD_Memory(DSD_Benchmark(1), n = 5000) ## ----models------------------------------------------------------------------- algorithms <- list( "Sample + k-means" = DSC_TwoStage( micro = DSC_Sample(k = 100, biased = TRUE), macro = DSC_Kmeans(k = 2)), "Window + k-means" = DSC_TwoStage( micro = DSC_Window(horizon = 100, lambda = 0.01), macro = DSC_Kmeans(k = 2)), "D-Stream" = DSC_DStream(gridsize = 0.1, lambda = 0.01), "DBSTREAM" = DSC_DBSTREAM(r = 0.05, lambda = 0.01) ) ## ----evaluation--------------------------------------------------------------- evaluation <- lapply(algorithms, function(model) { reset_stream(stream) evaluate_stream(model, stream, horizon = 100, n = 5000, measure = c("purity", "crand"), type = "macro", assign = "micro") }) head(evaluation[["DBSTREAM"]]) ## ----score-trajectories------------------------------------------------------- position <- evaluation[[1]][, "points"] scores <- sapply(evaluation, function(x) x[, "cRand"]) matplot(position, scores, type = "l", lwd = 1, xlab = "Points processed", ylab = "Adjusted Rand index") legend("bottomleft", legend = names(algorithms), col = seq_along(algorithms), lty = seq_along(algorithms), cex = 0.8) ## ----score-distributions------------------------------------------------------ boxplot(scores, las = 2, cex.axis = 0.8, ylab = "Adjusted Rand index") colMeans(scores) ## ----purity------------------------------------------------------------------- purity <- sapply(evaluation, function(x) x[, "purity"]) colMeans(purity) ## ----lambda------------------------------------------------------------------- lambdas <- c(0, 0.001, 0.01, 0.1) lambda_eval <- lapply(lambdas, function(l) { reset_stream(stream) evaluate_stream(DSC_DBSTREAM(r = 0.05, lambda = l), stream, horizon = 100, n = 5000, measure = "crand", type = "macro", assign = "micro") }) lambda_scores <- sapply(lambda_eval, function(x) x[, "cRand"]) colnames(lambda_scores) <- paste("lambda =", lambdas) matplot(position, lambda_scores, type = "l", lwd = 1, xlab = "Points processed", ylab = "Adjusted Rand index") legend("bottomleft", legend = colnames(lambda_scores), col = seq_along(lambdas), lty = seq_along(lambdas), cex = 0.8) colMeans(lambda_scores, na.rm = TRUE) ## ----moving-animation, eval=knitr::is_html_output() && requireNamespace("gifski", quietly=TRUE) && requireNamespace("animation", quietly=TRUE), fig.show='animate', animation.hook='gifski', interval=0.1, aniopts='loop', dev='png', fig.width=6.4, fig.height=6.8, dpi=100, fig.retina=1, fig.alt='DBSTREAM following two moving clusters'---- set.seed(1000) animation_stream <- DSD_Benchmark(1) animation_model <- DSC_DBSTREAM(r = 0.05, lambda = 0.01) res <- animate_cluster(animation_model, animation_stream, measure = "cRand", horizon = 100, n = 5000, wait = 0, # for use in a notebook plot.args = list(xlim = c(0, 1), ylim = c(0, 1), type = "both"))