lundi 5 juillet 2021

R: Problems Adjusting R-Loop for new purpose (CRQA)

I am currently running an CRQA on my sample. For that I want to keep the recurrence rate of each time series pair in a certain range, which is achieved by dynamically adjusting the radius for each time series pair. In order to do so, I am trying to adjust the following code for my own purposes.

What the following loop does is adjusting the radius parameter in steps of +/- 0.01, if the extracted mean recurrence rate is above 6% or below 5%:

library(crqa)
ts1 = arima.sim(list(order = c(1,1,0), ar = 0.2), n = 1000)
ts2 = arima.sim(list(order = c(1,1,0), ar = 0.4), n = 1000)
ts3 = arima.sim(list(order = c(1,1,0), ar = 0.8), n = 1000)
list1 = list(ts1, ts2, ts3)

ts4 = arima.sim(list(order = c(1,1,0), ar = 0.9), n = 1000)
ts5 = arima.sim(list(order = c(1,1,0), ar = 0.1), n = 1000)
ts6 = arima.sim(list(order = c(1,1,0), ar = 0.4), n = 1000)
list2 = list(ts4, ts5, ts6)

meanREC = 0
radiusPar <- 0.80
REC = 0; DET = 0; NLINE = 0; maxL = 0; AvL = 0; ENTR = 0; rENTR = 0; LAM = 0; TT = 0

# loop through radius parameters contingent recurrence rate
while(meanREC < 5 | meanREC > 6){
  
  # adjust radius parameter
  if(meanREC < 5) {
    radiusPar <- radiusPar + 0.01    
  }
  else {
    radiusPar <- radiusPar - 0.01  
  }
  # loop through data sets 
  for(i in 1:length(list1)) {
    temp <- crqa(ts1=list1[[i]], ts2= list2[[i]],
                 radius = radiusPar, embed = 3, delay = 70,
                 normalize = 2, rescale = 0,
                 mindiagline = 15, minvertline = 15, tw = 0, whiteline = FALSE,
                 side = "both", method = "crqa",
                 metric = "euclidean", datatype = "continuous")
    REC[i] <- temp$RR
    DET[i] <- temp$DET
    NLINE[i] <- temp$NRLINE
    maxL[i] <- temp$maxL
    AvL[i] <- temp$L
    ENTR[i] <- temp$ENTR 
    rENTR[i] <- temp$rENTR
    LAM[i] <- temp$LAM
    TT[i] <- temp$TT
}
  # check currrent state of radius span and RR
  meanREC <- mean(REC)
  print(meanREC)
}
crqa_res <- data.frame(REC, DET, maxL, AvL, ENTR, rENTR, LAM, TT)

I am trying now to adjust this in a way that the loop does not adjust the recurrence rate depending on the meanREC of the overall sample, but adjust the radius parameter for each individual time series pair until it falls within the desired 5-6% recurrennce rate range. I tried the following, unfortunately without any success:

Rate = 0
radiusPar <- 0.80
REC = 0; DET = 0; NLINE = 0; maxL = 0; AvL = 0; ENTR = 0; rENTR = 0; LAM = 0; TT = 0

# loop through radius parameters contingent recurrence rate
while(Rate < 5 | Rate > 6){
  
  # adjust radius parameter
  if(Rate < 5) {
    radiusPar <- radiusPar + 0.01    
  }
  else {
    radiusPar <- radiusPar - 0.01  
  }
  # loop through data sets 
  for(i in 1:length(list1)) {
    temp <- crqa(ts1= list1[[i]], ts2= list2[[i]],
                 radius = radiusPar, embed = 3, delay = 70,
                 normalize = 2, rescale = 0,
                 mindiagline = 10, minvertline = 10, tw = 0, whiteline = FALSE,
                 side = "both", method = "crqa",
                 metric = "euclidean", datatype = "continuous")
    REC[i] <- temp$RR
    DET[i] <- temp$DET
    NLINE[i] <- temp$NRLINE
    maxL[i] <- temp$maxL
    AvL[i] <- temp$L
    ENTR[i] <- temp$ENTR 
    rENTR[i] <- temp$rENTR
    LAM[i] <- temp$LAM
    TT[i] <- temp$TT
}
  # check currrent state of radius span and RR
  Rate <- REC[i]
  print(radiusPar)
  print(Rate)
}
crqa_res <- data.frame(REC, DET, maxL, AvL, ENTR, rENTR, LAM, TT)

The problem is that my adjusted version only takes into account the reccurence rate and then adjust the previous one too. Perhaps someone with more experience in using R-loops can help me out here.

I appreciate any tips. Please let me know if you need more information.

Best, Johnson

Aucun commentaire:

Enregistrer un commentaire