First, I load the data logged by my Emfit sleep monitor device, using this library.
# load library for importing JSON data
library(importEmfit)
## Loading required package: jsonlite
##
## Attaching package: 'jsonlite'
##
## The following object is masked from 'package:utils':
##
## View
# import data
filename <- "~/Dropbox/DataSets/Emfit/device-10-presence-report-31260.json"
data <- importEmfitJSON(filename, timezone="Europe/Ljubljana")
# a summary of the data
summary(data)
tail(data, 100)
# create a heart rate time series
# sampling frequency for heart rate is one measurement per 2 seconds
heartRate <- ts(data$heart_rate, frequency = 1/2)
Load the libraries
# load library TSclust
library(TSclust)
## Loading required package: wmtsa
## Loading required package: splus2R
## Loading required package: ifultools
## Loading required package: MASS
## Loading required package: pdc
## Loading required package: cluster
# load ggplot2 for plotting time series
library(ggplot2)
# load sewave library
library(seewave)
##
## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
## Welcome to seewave !
## The package is regularly updated, please check for new version [http://rug.mnhn.fr/seewave]
## Thanks to use the right reference when citing seewave in publications
## See citation('seewave')
## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# plot the heart rate using ggplot2
plot <- ggplot(data = data) + geom_point(aes(x=timestamp, y=heart_rate), color="blue") + theme_bw()
plot
# plot the heart rate time series as SaX
# using alphabet size of 4 and word length of 100
# NOTE: the col.ser parameter seems a bit buggy or perhaps I'm not using it correctly
SAX.plot(series=heartRate, w=100, alpha = 4, col.ser=c("black","blue","red"))
## Warning: Series length is not multiple of w (number of frames), plotting
## the dimensionality reduced series
# do a PAA on the heart rate time series
PAA(heartRate, w=100)
## [1] 45.298 65.045 60.690 52.272 68.554 72.016 72.025 71.315 71.054 70.805
## [11] 52.054 62.359 71.113 71.798 72.526 72.692 71.371 69.404 68.013 68.939
## [21] 71.723 71.445 69.840 66.279 65.582 64.752 64.844 64.338 65.483 64.785
## [31] 64.558 65.438 64.301 64.248 58.198 57.345 53.909 59.790 59.539 63.521
## [41] 63.896 64.733 64.026 64.136 63.327 64.108 62.986 63.208 63.571 63.562
## [51] 63.472 61.852 64.866 51.725 63.120 63.138 61.953 63.485 58.634 58.429
## [61] 57.813 57.667 58.169 57.886 57.650 61.224 57.299 57.331 57.747 54.734
## [71] 58.032 55.492 46.077 8.837 33.818 53.993 58.027 48.581 39.612 55.551
## [81] 56.775 56.680 58.201 55.942 51.817 51.265 53.147 55.293 56.613 49.766
## [91] 37.802 51.184 56.043 56.846 33.219 51.029 50.924 52.577 46.108 39.167
# code below does not produce a symbol, but a number
# if they were at least different, I would understand
# but at the moment I believe the function is not working properly...
convert.to.SAX.symbol(PAA(heartRate,w=100), alpha = 4)
## [1] 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
## [36] 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
## [71] 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
The plot is nice, the PAA will be useful. The SAX word should not be a number vector… perhaps a bug?
# produce a SaX of the heart rate time series
# using alphabet size of 4 and word lenght 100
SAX( x=heartRate, alphabet_size = 4, PAA_number = 100 )
## [1] "a" "c" "c" "b" "d" "d" "d" "d" "d" "d" "b" "c" "d" "d" "d" "d" "d"
## [18] "d" "c" "d" "d" "d" "d" "c" "c" "c" "c" "c" "c" "c" "c" "c" "c" "c"
## [35] "b" "b" "b" "c" "c" "c" "c" "c" "c" "c" "c" "c" "c" "c" "c" "c" "c"
## [52] "c" "c" "b" "c" "c" "c" "c" "b" "b" "b" "b" "b" "b" "b" "c" "b" "b"
## [69] "b" "b" "b" "b" "a" "a" "a" "b" "b" "a" "a" "b" "b" "b" "b" "b" "b"
## [86] "b" "b" "b" "b" "a" "a" "b" "b" "b" "a" "b" "b" "b" "a" "a"
The SAX word seems to agree with the graph above. Nice!
Although the TSclust function for producing the SaX word (convert.to.SAX.symbol()) does not seem to work, the graph produced by SAX.plot() is nice and seems to agree (by eye inspection) with the SaX word produced by seewave’s SAX() function.