The object to compute statistics for. Can be an "AccData" object, a matrix, or a vector.
epoch.size
Numeric giving intervals to consider and aggregate. For "AccData" obj taken as seconds. Otherwise, considered as rows, or as individual readings.
incl.date
logical. If TRUE, include a column of times or original indices with the results.
FUN
A function to be applied to each epoch.
sqrt
logical. If TRUE, the square rooted svm will be used in computations instead.
lag
Autocorrelation lag to compute.
type
Type of autocorrelation, as used in acf.
quantiles
Sample quantiles of SVM to compute.
Details
These functions compute epochal summary statistics for "AccData" objects, matrices and vectors.
epoch.apply is the general function - according to the size of epoch.size, it splits up the obj into collections of consecutive rows, each with the same size. These are then successively supplied to FUN as its first argument. If the result of FUN is a single value, then the results are concatenated into a vector output. Otherwise, an array is formed with each row corresponding to a single epochal group. For AccData, the sampling frequency of the dataset is used to interpret the epoch size in seconds. Otherwise, the raw record indices are used. If incl.date is set, the original timestamp vector of the data, or the original indices, are downsampled and included as the first column of the output.
The remaining functions are wrappers that compute various commonly useful statistics – in particular, applied to "AccData" objects and arrays, they by default compute the epochal SVM mean, standard deviation, median, median absolute deviation, and autocorrelation, and sample quantiles respectively. (Arrays are treated as each column representing the x, y, and z components respectively.) Applied to vector input, processing will occur without the SVM calculation. This behaviour may be overridden by the sqrt setting, which will force the function to use the squared (default for arrays and "AccData") or original unit (default for vectors) values in the statistical analysis.
svm acts identically to 'epoch.mean', with the epoch set to the sampling period. In other words, it computes the instantaneous sum of vector magnitudes of the acceleration at each record point. The function takes "AccData", array and vector input. Note that if provided with an array with 4 or more columns, columns 2 to 4 are used – the first column is regard as a timestamp and hence ignored.
Value
A vector or array giving the computed epochal summaries. With incl.date = TRUE, the result is given as a data.frame suitable for plotting.
See Also
plot.AccData, summary.AccData, aggregate, acf
Examples
dat <- read.bin(system.file("binfile/TESTfile.bin", package = "GENEAread")[1]
, calibrate = TRUE)
#look for the epochs that exceed a certain threshold 50% of the time
plot(epoch.apply( dat, epoch.size = 3 ,
FUN = function(t) mean(abs(svm(t) -1)>0.2)> 0.5 ), type = "l")
plot(dat[,1], svm(dat), log = "y", pch = ".")
lines(epoch.mean(dat, incl.date = TRUE), lwd = 2)
lines(epoch.mean(dat, epoch.size = 30, incl.date = TRUE), col = 2, lwd = 2)
#this should give all the same results, but by a different way
lines(epoch.apply(dat, epoch.size = 30,
FUN = function(A) mean(svm(A, FALSE)), incl.date = TRUE), col = 3)
epsize = 30; lines(epoch.apply(dat, epoch.size = epsize,
FUN = function(t) median(t[,1])), epoch.apply(dat, epoch.size = epsize,
FUN = function(A) mean(svm(A, FALSE))), col = 4)
#note this is different
lines(epoch.apply(dat, epoch.size = epsize,
FUN = function(t) median(t[,1])),epoch.apply(dat, epoch.size = epsize,
FUN = function(A) mean(svm(A, sqrt = TRUE)))^2, col = 5)
#plot some statistics
par(mfrow = c(5,1), mar = c(1,4.5,1,1))
plot(epoch.sd(dat), type="l")
plot(epoch.median(dat), type= "l")
plot(epoch.mad(dat), type= "l")
plot(epoch.autocor(dat), type= "l")
tmp = epoch.quantile(dat, quantiles= c(0.1, 0.25, 0.5, 0.75, 0.9)); matplot(tmp, type = "l")
Results
R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> library(GENEAread)
Loading required package: bitops
GENEAread 1.1.1 loaded
> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/GENEAread/epoch.apply.Rd_%03d_medium.png", width=480, height=480)
> ### Name: epoch.apply
> ### Title: Compute epochal summary statistics.
> ### Aliases: epoch.apply epoch.mean epoch.sd epoch.median epoch.mad
> ### epoch.autocor epoch.quantile svm
> ### Keywords: ts
>
> ### ** Examples
>
>
> dat <- read.bin(system.file("binfile/TESTfile.bin", package = "GENEAread")[1]
+ , calibrate = TRUE)
Loading required package: mmap
Number of pages in binary file: 104
Calculated page references...
Processing...
================================================================================
Processing took: 0.083 secs .
Loaded 31200 records (Approx 2 MB of RAM)
12-05-23 16:47:50.000 (Wed) to 12-05-23 16:53:01.990 (Wed)
Warning message:
In FUN(newX[, i], ...) : NAs introduced by coercion
>
> #look for the epochs that exceed a certain threshold 50% of the time
> plot(epoch.apply( dat, epoch.size = 3 ,
+ FUN = function(t) mean(abs(svm(t) -1)>0.2)> 0.5 ), type = "l")
>
> plot(dat[,1], svm(dat), log = "y", pch = ".")
> lines(epoch.mean(dat, incl.date = TRUE), lwd = 2)
> lines(epoch.mean(dat, epoch.size = 30, incl.date = TRUE), col = 2, lwd = 2)
> #this should give all the same results, but by a different way
> lines(epoch.apply(dat, epoch.size = 30,
+ FUN = function(A) mean(svm(A, FALSE)), incl.date = TRUE), col = 3)
> epsize = 30; lines(epoch.apply(dat, epoch.size = epsize,
+ FUN = function(t) median(t[,1])), epoch.apply(dat, epoch.size = epsize,
+ FUN = function(A) mean(svm(A, FALSE))), col = 4)
> #note this is different
> lines(epoch.apply(dat, epoch.size = epsize,
+ FUN = function(t) median(t[,1])),epoch.apply(dat, epoch.size = epsize,
+ FUN = function(A) mean(svm(A, sqrt = TRUE)))^2, col = 5)
>
> #plot some statistics
> par(mfrow = c(5,1), mar = c(1,4.5,1,1))
> plot(epoch.sd(dat), type="l")
> plot(epoch.median(dat), type= "l")
> plot(epoch.mad(dat), type= "l")
> plot(epoch.autocor(dat), type= "l")
> tmp = epoch.quantile(dat, quantiles= c(0.1, 0.25, 0.5, 0.75, 0.9)); matplot(tmp, type = "l")
>
>
>
>
>
>
>
> dev.off()
null device
1
>