Batch cosinor processing

You will need several packages installed before you can complete this analysis. Run the code below to make sure you have the packages installed. Each package should return a ‘TRUE’ value if installed.

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('tidyverse', 'lubridate', 'psych')
ipak(packages)

Generate a list of the files you would like to process

# Assign all spreadsheets in wd() 
files <- Sys.glob("*.csv")
# Lists the files in the directory - make sure this is correct! 
files

Make sure the object files contains the files you wish to process.

Run the loop

The loop will generate two separate files. A .csv file containing the cosinor statistics, and a report containing two plots of the cosinor function.

cosinor_batch <- lapply(files, function(f) {

library(tidyverse)
library(lubridate)
library(psych)
  
data <- read.csv(f, header = T, sep = ",")

data$Date <- dmy(data$Date)
split_summary <- data.frame(split(data$data.Activity, data$Date))
split_summary <- split_summary %>%
  transmute(split_means = rowMeans(split_summary))

data$Time <- as.POSIXct(data$Time, format = "%I:%M:%S %p", tz = "GMT")
data$Time <- strftime(data$Time, format = "%H:%M:%S", tz = "GMT")
data$Time <- sapply(strsplit(data$Time,":"),
                    function(x) {
                      x <- as.numeric(x)
                      x[1]+x[2]/60
                    })

split_time <- data.frame(split(data$Time, data$Date))

split_time <- split_time %>%
  transmute(time_day = rowMeans(split_time))

Activity_Rhythm <- split_summary$split_means
Angle <- split_time$time_day
cosinor_df <- data.frame(Angle, Activity_Rhythm)

cosinor_results <- cosinor(cosinor_df, hours = T, na.rm = F, period = 24)


# Save them to a .csv with filename as filename
write.csv(cosinor_results, paste0(f, "_Cosinor.csv"))

pdf(file = paste0(f, "_Cosinor_plots.pdf"))

print(cosinor.plot(cosinor_df, hours = T, na.rm = F, period = 24, xlab = "Dual-day plot of averaged 24-hour activity rhythm with fitted curve",
             ylim = c(0, 300), ylab = "Activity intensity", cex = 0.1, col = "brown4"))

print(cosinor.plot(cosinor_df, hours = T, na.rm = F, period = 24, xlab = "Dual-day plot of averaged 24-hour activity rhythm",
                   ylim = c(0, 300), ylab = "Activity intensity", cex = 0.8, col = "brown4"))

dev.off()

print(paste0(f, "    has been processed"))

})