#'Create a Realistic Random Time Series
#'
#'(i.e. each point will not vary too far from its predecessor)
#'
#'@param length Numeric length of the time series
#'@param first Numeric first value of the time series
#'@param max.variability Maximum difference between any point and its predecessor
#'@param embed A numeric vector that will be embedded within the time series
#'@param interval A numeric time interval to be inserted between each point. This makes the
#'time series regular.
#'@export
createRandTimeSeries <- function(length, first, max.variability, embed, interval, floor, ceiling){
#TODO: This has a bug with the ceiling/floor where it drops to zero sometimes. Fix before adding to TSPAtternQuery
values <- vector("numeric",length = length)
values[1] <- first
for(i in 2:(length/2)){
if(sample(1:2, 1)%%2 == 0){
next.val <- values[i-1] + runif(1, min=0, max=1)*max.variability
if(!missing(ceiling) && next.val > ceiling){
i <- i-1
next()
}
values[i] <- next.val
} else{
next.val <- values[i-1] - runif(1, min=0, max=1)*max.variability
if(!missing(ceiling) && next.val < floor){
i <- i-1
next()
}
values[i] <- values[i-1] - runif(1, min=0, max=1)*max.variability
}
}
if(!missing(embed)){
for(i in 1:length(embed)){
values[length/2 + i] <- values[length/2] + embed[i]
}
}else{
embed <- vector()
}
length.after.embed = length/2 + length(embed) + 1
for(i in length.after.embed:length){
if(sample(1:2, 1)%%2 == 0){
next.val <- values[i-1] + runif(1, min=0, max=1)*max.variability
if(!missing(ceiling) && next.val > ceiling){
i <- i-1
next()
}
values[i] <- values[i-1] + runif(1, min=0, max=1)*max.variability
} else{
next.val <- values[i-1] - runif(1, min=0, max=1)*max.variability
if(!missing(ceiling) && next.val < floor){
i <- i-1
next()
}
values[i] <- values[i-1] - runif(1, min=0, max=1)*max.variability
}
point <- values[i]
}
if(!missing(interval)){
return(CreateCustomTimeSeries(
values,
seq(from=interval, to=interval, length.out = length(values)-1)
)
)
}
return(CreateCustomTimeSeries(values))
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.