Question: Plotting time against quantitative variables in R
gravatar for selplat21
7 months ago by
selplat2110 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 • 163 views
ADD COMMENTlink modified 7 months ago by Asaf8.4k • written 7 months ago by selplat2110
gravatar for Asaf
7 months ago by
Asaf8.4k 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 7 months ago by Asaf8.4k

Thank you so much! This is perfect.

ADD REPLYlink written 7 months ago by selplat2110
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: 982 users visited in the last hour