0

我正在计算年龄(数字)向量和日期(POSIXct)向量之间的线性回归。转换日期以使cor满意的最方便的方法是什么?

4

1 回答 1

1

正如@Chase 的评论中提到的,cor如果您将日期转换为带有as.numeric.

#First some dummy data:
age<-ceiling(runif(20,min=25,max=45))
joindate<-sample(seq(as.Date("01/01/1990","%d/%m/%Y"), 
                     as.Date("31/12/2010","%d/%m/%Y"), by="day"), 20)
age
[1] 35 33 33 30 39 30 32 26 45 37 28 44 35 31 39 44 44 40 29 39
joindate
 [1] "1999-07-03" "2006-08-09" "2001-11-22" "2003-02-11" "1991-06-23" "2007-04-20" "1993-04-28" "1997-04-08" "1999-08-16"
[10] "2005-02-17" "2002-11-01" "1991-09-17" "2006-05-03" "1995-12-02" "2007-06-20" "2000-02-26" "2005-10-01" "1997-06-13"
[19] "2007-06-09" "1994-11-27"

as.numeric(joindate) 
# Dates are transformed into a number that corresponds to the number of days since the origin date (as a convention the 1970/01/01)
 [1] 10775 13369 11648 12094  7843 13623  8518  9959 10819 12831 11992  7929 13271  9466 13684 11013 13057 10025 13673  9096

cor.test(age, as.numeric(joindate))

    Pearson's product-moment correlation

data:  age and as.numeric(joindate) 
t = -0.9641, df = 18, p-value = 0.3478
alternative hypothesis: true correlation is not equal to 0 
95 percent confidence interval:
 -0.6048037  0.2449517 
sample estimates:
       cor 
-0.2215884 
于 2012-10-19T08:03:42.473 回答