0

I have the task to generate surrogate time series by a swap model: choose a random point in your time series and swap the order of the two segments. Do that 100 times and that is one surrogate time series.

My problem is that it doesn't seem to matter how many times do I swap the time series. Even if I do it 1000 times I get a very similar series with the original one. I was expecting to fully randomize it after this many times.

I think I have a problem with my algorithm, but I can't figure out where. Can anyone help? Here is my short code:

'''

swap_model <- function(ts,times,nr_surr){
#choose a random pozition in time series (ts) and swap the two segments 'times' times

n<-length(ts)

l <- seq(1,n,1)
sws <-data.frame(l)

for (j in 1:nr_surr){
tts <- ts
for (i in 1:times){
  poz <- sample(1:n,1,replace = F) 
  tts <- c(tail(tts,n-poz),head(tts,poz))
}
sws <- cbind(sws,tts[1:n])
}
sws$l <- NULL
return(sws)
}

'''

PaulDirac
  • 137
  • 5
  • 1
    If you add a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). you could make it easier for others to find and test a answer to your question. That way you can help others to help you! – dario Mar 08 '20 at 10:22
  • 1
    Your code will swap only once, no matter how many times you keep doing it. You may even get the original series every now and then. What do you expect to get? – Edward Mar 08 '20 at 10:23
  • I don't understand why it swaps only once since I have used the same variable tts – PaulDirac Mar 08 '20 at 10:26
  • 1
    Imagine a pack of playing cards. Or go and get one. Take out A, 2, 3, ..., 10. Sort them. Then cut in half. Keep cutting in half. The order doesn't change. Only the starting point. – Edward Mar 08 '20 at 10:29
  • Yes, bur crucially I don't repeatedly cut in half. Every time I choose a different initial position – PaulDirac Mar 08 '20 at 10:35
  • Ok. I think I got. Thank you guys. – PaulDirac Mar 08 '20 at 10:49
  • ;) so what should the swap surrogate model actually supposed to be doing? – Edward Mar 08 '20 at 11:23

0 Answers0