 # (Beginner) Cosinor function - how to load time variable

I am working on completing some cosinor analysis on some activity data of mine. I am attempting to run the cosinor.lm command, yet keep getting this error which states that my time column is not in the correct format.

Portion of my data.set:

My code and error:

cosinor.lm(Activity ~ time(Time) + Bird + Sex, data=data.set, period = 24, na.action = na.omit)
"Error in 2 * pi * data[, timevar] :
non-numeric argument to binary operator"

I have tried converting the time column to numeric value, but because of the colons in the time column format, R returns the values as "NA". Does anybody know how I can fix this?

Any problem in `R` can be approached with advantage using the basic idea of school algebra: f(x) = y where the three objects are

x, called an argument, which is what is at hand, in this case a vector of strings representing HH:MM:SS

y the object that is desired

f a function, which may be composed to transform x to y.

There are regression methods meant to work with time series specifically that be considered if the time data are more variable. The `Time` variable might, in this case of ordinary least square regression be treated as a categorical variable since it seems, a priori, unlikely that Activity varies continuously with the passage of time. However, the conversion below assumes that seconds is the appropriate measure.

``````# LIBRARIES
suppressPackageStartupMessages({
library(dplyr)
library(purrr)
library(stringr)
})

# FUNCTIONS

mk_secs <- function(x) x*3600 + x * 60 + x * 1

# DATA

first_time <- rep("10:10:00",10)
second_time <- rep("10:11:00",10)
the_times <- c(first_time,second_time)

# MAIN

str_split(the_times,":") %>%
map(., as.numeric) %>%
map(., mk_secs) %>%
unlist(.)
#>   36600 36600 36600 36600 36600 36600 36600 36600 36600 36600 36660 36660
#>  36660 36660 36660 36660 36660 36660 36660 36660
``````

Created on 2020-10-23 by the reprex package (v0.3.0.9001)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.