C.4 Heartbeat dynamics

| jump to assignment |

library(rio)
TS1 <- rio::import("https://github.com/FredHasselman/DCS/raw/master/assignmentData/RelativeRoughness/TS1.xlsx", col_names=FALSE)
TS2 <- rio::import("https://github.com/FredHasselman/DCS/raw/master/assignmentData/RelativeRoughness/TS2.xlsx", col_names=FALSE)
TS3 <- rio::import("https://github.com/FredHasselman/DCS/raw/master/assignmentData/RelativeRoughness/TS3.xlsx", col_names=FALSE)

The Excel files did not have any column names, so let’s create them in the data.frame

colnames(TS1) <- "TS1"
colnames(TS2) <- "TS2"
colnames(TS3) <- "TS3"
# Create a function for RR
RR <- function(ts){
# lag.max = n gives autocovariance of lags 0 ... n, 
VAR  <- acf(ts, lag.max = 1, type = 'covariance', plot=FALSE)
# RR formula
RelR   <- 2*(1-VAR$acf[2] / VAR$acf[1])
# Add some attributes to the output
attributes(RelR) <- list(localAutoCoVariance = VAR$acf[2], globalAutoCoVariance = VAR$acf[1]) 
return(RelR)
}

# Look at the results
for(ts in list(TS1,TS2,TS3)){
  relR <- RR(ts[,1])
  cat(paste0(colnames(ts),": RR = ",round(relR,digits=3), " = 2*(1-",
         round(attributes(relR)$localAutoCoVariance, digits = 4),"/",
         round(attributes(relR)$globalAutoCoVariance,digits = 4),")\n"))
  }
TS1: RR = 0.485 = 2*(1-0.0016/0.0021)
TS2: RR = 0.118 = 2*(1-0.0018/0.0019)
TS3: RR = 2.052 = 2*(1--1e-04/0.002)

Use Figure 4.2 to lookup which value of \(RR\) corresponds to which type of noise:

TS1: Pink noise TS2: Brownian noise TS3: White noise

C.4.1 Randomise

To randomize the data you may use the function sample (which is easier than randperm)

library(pracma)
# randperm()
TS1Random <- TS1$TS1[randperm(length(TS1$TS1))]

# sample()
TS1Random <- sample(TS1$TS1, length(TS1$TS1))

plot.ts(TS1Random)
lines(ts(TS1$TS1),col="red3")

If you repeat this for TS2 and TS3 and compute the Relative Roughness of each randomized time series, the outcomes should be around 2, white noise! This makes sense, you destroyed all the correlations in the data by removing the temporal order with which values were observed.

C.4.2 Integrate

Normalize the white noise time series

TS3Norm <- scale(TS3$TS3)

Now integrate it, which just means, ‘take the cumulative sum’.

TS3Int <- cumsum(TS3Norm)
plot.ts(TS3Int)
lines(ts(TS3Norm),col="red3")

If you compute the Relative Roughness of the integrated time series, the outcome should be close to 0, Brownian noise.

RR(TS3Int)
[1] 0.02783704
attr(,"localAutoCoVariance")
[1] 35.69734
attr(,"globalAutoCoVariance")
[1] 36.2012