Before you start

You will need the ‘lubridate’ and ‘nparACT’ packages to complete the analyses. Run this code to install the packages and load them. After the packages have finished installing, the function will return a ‘TRUE’ value to verify the packages have been installed. You will only need to run this once, as the packages are installed locally on each machine. If you change the computer you use for the analysis you will need to re-run the code.

ipak <- function(pkg){
  new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
  if (length(new.pkg)) 
    install.packages(new.pkg, dependencies = TRUE)
  sapply(pkg, require, character.only = TRUE)
}

packages <- c("lubridate", "nparACT")
ipak(packages)

Folder preparation

Create a folder named ‘Actigraphy_Processing’, and make sure all the .csv’s you need to process are located there. Set your R working directory as that folder. Create another folder within ‘Actigraphy_Processing’ named ‘nonpar’.

MAKE SURE YOUR FILES ARE NAMED AS THE PATIENT/PARTICIPANT’s ID!

File preparation

Missing data imputation/data cleaning must occur before being processed. Instructions on how to impute missing values in actigraphy data can be found here. This processing includes trimming the file to full days only.

Batch Processing

The following code will produce one excel file, labelled ‘npar_Results’. Each row will be labelled the filename, which should be participant ID.

Note: You will need to set the working directory to your folder which contains the .csv’s.

# Assign all spreadsheets in wd() 
files <- Sys.glob("*.csv")
# Lists the files in the directory - make sure this is correct! 
files
# Non-parametric analysis of actigraphy data
# Load packages
library(nparACT)
library(lubridate)

# Loop function

for (filename in files) {
  # Read data:
  data <- read.csv(filename, header = T, sep = ",")
  
  # Create data.frame for nparACT:
  data$npar_time <- parse_date_time(data$Time, '%I:%M:%S %p')
  npar <- data.frame(data$npar_time, data$data.Activity)
  
  # nparact function:
  npar_output <- nparACT_base("npar", 2/60, cutoff = 1, plot = F, fulldays = F)
  
  # Write new data to a new excel file:
  write.table(npar_output, "npar_Results.csv", 
              append = TRUE, sep = ",", row.names = paste(filename), col.names = FALSE)
  
}

# Label the columns in the data with the following outputs
npar_output

Label your columns

In each spreadsheet, label your columns as per the output in the console for each test. Your files should look like this:

npar_Results (https://drive.google.com/open?id=0B-GlEoOZsCBCS1daZV9VWkRxdDg)

Interpreting and reporting your data

The following article in Sleep Science (2014) provides an overview of the nonparametric analysis of actigraphy data that we are using, and describes it’s clinical utility: (https://drive.google.com/open?id=0B-GlEoOZsCBCQjFhZjdheUJsVHc).

Also see: (https://drive.google.com/open?id=0B-GlEoOZsCBCS0lmQko5TVVYcTQ)