-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhrv_triggers.Rmd
84 lines (65 loc) · 2.29 KB
/
hrv_triggers.Rmd
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
76
77
78
79
80
81
82
83
---
title: "Dealing with Triggers in ECG data"
author: "Daniel N. Albohn"
date: "11/08/2017"
output:
html_document:
theme: cosmo
css: style.css
editor_options:
chunk_output_type: console
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE, error = FALSE, eval = FALSE)
```
Usually when we analyze HRV data, we are only interested in computing statistics for certain parts
of the HRV time series. Usually a raw ECG file will have an additional channel that outputs a small
electrical charge when certain events are happening. This is done in order to denote which parts of
the sequence are important for analysis.
```{r}
source('R/hrv_tutorial/make_events.R')
make_events('sub1101.RDS', 'data/hrv_tutorial')
```
If we examine the source code for the `make_events()` function, we can see that there are
a few things we can tweak, if need be:
```{r}
make_events <- function(file, path){
wd <- getwd()
setwd(path)
name <- sub("*.RDS", "", file)
events <- readRDS(file)[c(1,3)]
# Set value variable
#colnames(events)[1] <- "Value"
# Make InitTime variable
op <- options(digits.secs=3)
#events$InitTime <- format( (as.POSIXct(Sys.Date())+events$`Time(ms)`/1000)-72000, "%H:%M:%OS")
events$InitTime <- events$time
# Make type variable
values <- unique(events$trigger)
condition <- NULL
for (i in seq_along(values)){
condition[[i]] <- paste0('event_type_', values[[i]])
}
duration <- rep(10, length(values))
match <- cbind.data.frame(values, condition, duration)
events$Type1 <- match(events$trigger,match$values)
events$Type <- match[events$Type1,2]
events$Duration <- match[events$Type1,3]
# Reorder and save
# events <- events[c(3,5,6,1,2,4)]
# events <- events[,c(-5,-6)]
events <- events[complete.cases(events$Type),]
events <- events[!rev(duplicated(rev(events$Type))),]
InitTime <- events$InitTime
Type <- events$Type
Duration <- events$Duration
Value <- events$trigger
#rm(events,match,condition,duration,Duration,file,InitTime,op,path,Type,Value,values)
episodes <- list(InitTime=InitTime,Type=Type,Duration=Duration,Value=Value)
save(episodes, file = paste0(name,"_trigger.RData"))
# Clean up
#rm(match,condition,op,values,duration)
setwd(wd)
rm(list = ls())
}
```