-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecurrence.R
75 lines (51 loc) · 2.13 KB
/
recurrence.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
######################################################################################
# Rhythm Analysis Methods in R
# Transfering script from Matlab to R
# author: Dr. Lara S. Burchardt
#
# date: 26.11.2021, version: 1.0
#
# recurrence plots
######################################################################################
recurrence <- function(data){
## 00a: load packages-----------
library(tidyverse)
library(vegan) #to calculate euclidean distance
library(corrplot)
library(plotly)
## 00b: load example data ------
#data input from function
## 01: data to ioi sequence ------------------------
ioi_seq <- data.frame() # set up empty dataframe to store ioi values in
#for (a in filenumber){ #start of loop for number of files, needs to be added, maybe better add in main!
for (x in 1:nrow(data)) { # start of loop through rows of data to calculate iois
z = x+1
ioi_seq[x,1] <- data[z,1]-data[x,1]
}
## 02: recurrence matrix ---------------------
#euclidian distance matrix (when using phillips way)
eucl_dist <- (as.matrix(vegdist(ioi_seq, "euclidean", na.rm = TRUE)))
eucl_dist <- eucl_dist[1:(nrow(eucl_dist)-1),1:(nrow(eucl_dist)-1) ]
threshold <- mean(ioi_seq$X1, na.rm = TRUE)*0.1
eucl_dist[eucl_dist < threshold] <- 0
# transform matrix as to be able to plot it with ggplot as tile plot
#https://stackoverflow.com/questions/14290364/heatmap-with-values-ggplot2
levels <- 1:(nrow(eucl_dist))
eucl_dist_2 <- eucl_dist %>%
tibble::as_tibble() %>%
rownames_to_column('Var1') %>%
gather(Var2, value, -Var1) %>%
mutate(
Var1 = factor(Var1, levels = levels),
Var2 = factor(gsub("V", "", Var2), levels = levels)
)
## 03: recurrence plot -----------------
p <- ggplot(eucl_dist_2, aes(Var1, Var2)) +
geom_tile(aes(fill = value)) +
#geom_text(aes(label = round(value, 1))) +
scale_fill_gradient(low = "white", high = "black")
ggplotly(p)
ggsave(paste('plots/doreco/recurrence_',savename, a,'.jpg', sep = ""),
dpi =300 ,
device = "jpg")
}