Plotting time against quantitative variables in R
1
0
Entering edit mode
4.8 years ago
selplat21 ▴ 20

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 • 866 views
ADD COMMENT
0
Entering edit mode
4.8 years ago
Asaf 10k

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 COMMENT
0
Entering edit mode

Thank you so much! This is perfect.

ADD REPLY

Login before adding your answer.

Traffic: 1535 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6