| clumper | R Documentation |
clumper makes sure each homogeneous segment in a sequence is at least
minLength long. Called by getIntegerRandomWalk(), addSubh(), naiveBayes(),
etc. Algorithm: find the epochs shorter than minLength, merge max 1/4 of them
with the largest neighbor, and repeat recursively until all epochs are at
least minLength long. minLength can be a vector, in which case it is assumed
to change over time.
clumper(x, minLength, n = length(x))
clumper2(x, minLength, n = length(x))
x |
a vector: anything that can be converted into an integer to call diff(): factors, integers, characters, booleans |
minLength |
the minimum length of a segment (interger or vector) |
Returns the original sequence x transformed to homogeneous segments of required length, with the original class (e.g. character or factor).
s = c(1,3,2,2,2,0,0,4,4,1,1,1,1,1,3,3)
soundgen:::clumper(s, 2)
soundgen:::clumper(s, 3)
soundgen:::clumper(1:5, 10)
soundgen:::clumper(c('a','a','a','b','b','c','c','c','a','c'), 3)
soundgen:::clumper(x = c(1,2,1,2,1,1,1,1,3,1), minLength = c(1, 1, 1, 3))
soundgen:::clumper(as.factor(c('A','B','B','C')), 2)
## clumper vs clumper2
# clumper2 is faster for wiggly inputs
s = rpois(1000, 2)
plot(s, type = 'l')
plot(soundgen:::clumper(s, minLength = c(2, 3, 10)), type = 'l')
plot(soundgen:::clumper2(s, minLength = c(2, 3, 10)), type = 'l')
system.time(for (i in 1:10) soundgen:::clumper(s, minLength = c(2, 3, 10)))
system.time(for (i in 1:10) soundgen:::clumper2(s, minLength = c(2, 3, 10)))
# clumper1 is faster for step-like inputs
s2 = approx(c(2,4,3,2,0), n = 1001, method = 'constant')$y[1:1000]
plot(s2, type = 'l')
plot(soundgen:::clumper(s2, minLength = c(2, 3, 10)), type = 'l')
plot(soundgen:::clumper2(s2, minLength = c(2, 3, 10)), type = 'l')
system.time(for (i in 1:10) soundgen:::clumper(s2, minLength = c(2, 3, 10)))
system.time(for (i in 1:10) soundgen:::clumper2(s2, minLength = c(2, 3, 10)))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.