This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Plotting time against quantitative variables in R

Hello,

I am running a series of linear regressions between time and some quantitative trait. In order to do so, I need to convert date to some numeric value.

I have tried the following:

anna$date <- as.POSIXct(anna$date, origin="1861.11.20", format="%Y.%m.%d")

But when I try cor.test, I get the error 'x is not a numeric vector.

The following is my previous code up to this point:

anna <- as.data.frame(read.csv("Anna_total.csv", header=T))

anna$date <- as.POSIXct(anna$date, origin="1861.11.20", format="%Y.%m.%d")

###Note: I have also tried POSIXlt

ml <- filter(anna, sex == "M")

male_length <- ml[!is.na(ml$length),]

cor.test(male_length$date, male_length$length)

Any help that would allow me to run a regression between time and a trait would be very helpful.

r time-series ecology

Please reformat your code using the code button in the formatting toolbox

Here you go. The above post was merging two commands. It should be as follows, but I still get the same errors:

anna <- as.data.frame(read.csv("Anna_total.csv", header = T))

anna$date <- as.POSIXct(anna$date, origin = "1861.11.20", format = "%Y.%m.%d")

ml <- filter(anna, sex == "M")

male_length <- ml[!is.na(ml$length), ]

cor.test(male_length$date, male_length$length)

Please use the formatting bar (especially the code option) to present your post better.
code_formatting

Thank you!

I reformatted as you said. Thanks for the tip.

1 answer

You can use lm for linear regression, try something like:

lm(length ~ date, male_length)

It will not treat the date as a continuous values so if you have different dates it won't work. Your other option is to treat date as a continuous variable and refrain from converting it to POSIX.

Thank you so much! This is perfect.

Log in to answer this question.