Load the packages and data
library(tidyverse)
library(lubridate)
library(psych)
data <- read.csv(file.choose(), header = T)
Obtain the activity averages for each day, and compile into one 24hr period. Also wrangle the time to numeric.
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))
Create a data frame with the necessary variabes, and perform the analysis.
Activity_Rhythm <- split_summary$split_means
Angle <- split_time$time_day
cosinor_df <- data.frame(Angle, Activity_Rhythm)
cosinor(cosinor_df, hours = T, na.rm = F, period = 24)
## phase fit amplitude sd mean intercept
## Angle 17.99167 0.7796976 0.7796976 6.929405 11.99167 11.97988
## Activity_Rhythm 15.62536 0.8051276 0.8051276 55.573738 74.29669 48.23774
Plot the results. “ID” Indicates the acrophase as numeric time of the day.
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")
Plot Indicating more clearly the cosinor
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")