Question: Plotting time against quantitative variables in R
gravatar for nalexandre
13 days ago by
nalexandre10 wrote:


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 <-"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[!$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.

ecology R time-series • 89 views
ADD COMMENTlink modified 12 days ago by Asaf7.0k • written 13 days ago by nalexandre10
gravatar for Asaf
12 days ago by
Asaf7.0k wrote:

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.

ADD COMMENTlink written 12 days ago by Asaf7.0k

Thank you so much! This is perfect.

ADD REPLYlink written 12 days ago by nalexandre10
Please log in to add an answer.


Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1553 users visited in the last hour