The TwoRegression Package

Paul R. Hibbing

2018-03-16

Vignette Info

The TwoRegression package is designed to make it easy to quickly and accurately apply two-regression algorithms to data from wearable research devices. In conjunction with common batch processing syntax in R (e.g. mapply or lapply), the TwoRegression package presents a powerful tool for handling data from large studies. This vignette will demonstrate how to utilize the core features of the package, to set you on your way towards processing your data.

The basics

To use the two-regression algorithms of Hibbing et al. (2018), you need to use the hibbing18_twoReg_process function, which requires:

Additionally, you can set verbose = TRUE to view updates during processing.

By default, the function will ignore IMU files when Algorithm 1 is the only selection. If you want to use Algorithm 1, but still see the IMU varibles, set IMU_ignore_A1 = FALSE.

library(TwoRegression)
#> package 'TwoRegression' was built under R version 3.4.3
RAW <- system.file("extdata", "TestID_LeftWrist_RAW.csv", package = "TwoRegression")
IMU <- 
  system.file("extdata", "TestID_LeftWrist_IMU.csv", package = "TwoRegression") #Set this to NULL if no IMU file exists
Wear_Location <- "Left Wrist"
PID <- "Test"
Algorithm <- 3 #Will be automatically set to 1 if no IMU file is present
verbose <- FALSE
IMU_ignore_A1 <- TRUE

test_data <-
  hibbing18_twoReg_process(RAW, IMU, Wear_Location, PID, Algorithm, verbose, IMU_ignore_A1)

print(head(test_data))
#>    PID file_source_PrimaryAccel date_processed_PrimaryAccel
#> 1 Test TestID_LeftWrist_RAW.csv         2018-03-16 20:30:32
#> 2 Test TestID_LeftWrist_RAW.csv         2018-03-16 20:30:32
#> 3 Test TestID_LeftWrist_RAW.csv         2018-03-16 20:30:32
#> 4 Test TestID_LeftWrist_RAW.csv         2018-03-16 20:30:32
#> 5 Test TestID_LeftWrist_RAW.csv         2018-03-16 20:30:32
#> 6 Test TestID_LeftWrist_RAW.csv         2018-03-16 20:30:32
#>            file_source_IMU  date_processed_IMU           Timestamp
#> 1 TestID_LeftWrist_IMU.csv 2018-03-16 20:30:33 2015-11-12 15:45:00
#> 2 TestID_LeftWrist_IMU.csv 2018-03-16 20:30:33 2015-11-12 15:45:01
#> 3 TestID_LeftWrist_IMU.csv 2018-03-16 20:30:33 2015-11-12 15:45:02
#> 4 TestID_LeftWrist_IMU.csv 2018-03-16 20:30:33 2015-11-12 15:45:03
#> 5 TestID_LeftWrist_IMU.csv 2018-03-16 20:30:33 2015-11-12 15:45:04
#> 6 TestID_LeftWrist_IMU.csv 2018-03-16 20:30:33 2015-11-12 15:45:05
#>   day_of_year minute_of_day     ENMO Gyroscope_VM_DegPerS
#> 1         316           945 91.81064            0.6985270
#> 2         316           945 63.90690            0.6976330
#> 3         316           945 63.70351            0.6806886
#> 4         316           945 64.24208            0.6806373
#> 5         316           945 63.70059            0.6926852
#> 6         316           945 63.58346            0.6989126
#>   mean_abs_Gyroscope_x_DegPerS mean_abs_Gyroscope_y_DegPerS
#> 1                    0.2207365                    0.5846348
#> 2                    0.2344190                    0.5713514
#> 3                    0.2176725                    0.5628797
#> 4                    0.2382042                    0.5606505
#> 5                    0.2274410                    0.5743000
#> 6                    0.2290555                    0.5746244
#>   mean_abs_Gyroscope_z_DegPerS mean_magnetometer_direction GVM_CV10s
#> 1                    0.3059072                           S 1.1974519
#> 2                    0.3203365                           S 1.0903678
#> 3                    0.3101393                           S 1.0106332
#> 4                    0.3001943                           S 1.0106332
#> 5                    0.3083593                           S 1.0106332
#> 6                    0.3191014                           S 0.9701746
#>   Direction Left_Wrist_Algorithm3_Classification
#> 1        NA                                  SED
#> 2        NA                                  SED
#> 3         0                                  SED
#> 4         0                                  SED
#> 5         0                                  SED
#> 6         0                                  SED
#>   Left_Wrist_Algorithm3_METs
#> 1                       1.25
#> 2                       1.25
#> 3                       1.25
#> 4                       1.25
#> 5                       1.25
#> 6                       1.25

If you aren’t familiar with R commands that enable batch processing, consider the below hypothetical example. The RAW <- and IMU <- commands should give two vectors that contain paths to primary accelerometer and IMU files, respectively. The vectors should be the same length, and be index-matched, such that the first file in RAW corresponds with the first file in IMU, and so on.

The PID variable can be manually defined to establish a vector of PIDs to assign to the data files in RAW and IMU. Alternatively, you can use string functions (e.g. substring) to identify PIDs automatically if your files have a consistent naming convention.

# RAW <- list.files(pattern = "RAW.csv", full.names = TRUE)
# IMU <- list.files(pattern = "IMU.csv", full.names = TRUE)
# PID <- paste("Test", seq(RAW), sep = "_")
# 
# test_data <-
#   mapply(
#   hibbing18_twoReg_process,
#   RAW = RAW,
#   IMU = IMU,
#   PID = PID,
#   MoreArgs = list(Wear_Location = "Left Wrist", Algorithm = c(1, 2)),
#   SIMPLIFY = FALSE
#   )

This returns your processed files as separate items in a list. To combine them, use combined_data <- do.call(rbind, test_data). You can then export your data in csv format if you prefer to deal with it in another platform. A good recommendation is to use the fwrite function from the data.table package, which is substantially faster than write.csv.