使用R中的日期为Theil-Sen

时间:2014-10-03 21:33:22

标签: r time-series trend

我试图在Theil-Sen斜率估计中使用日期作为我的X变量,我在使用R包zyp时遇到困难

library(zyp)

myDates <- as.Date(c("2009-11-24","2009-12-03","2010-01-19","2010-02-18","2010-03-04"))
myData <- c(10.17,10.91,11.72,12.5,13.43)
a <- c("a","b","c","d","e")
df = data.frame(a,myData,myDates)
zyp.sen(myDates~myData, df)

然后我收到以下错误:

    Error in `+.Date`(y - slope * x, intercept) : 
      binary + is not defined for "Date" objects

我也在尝试使用zyp.trend.dataframe(df, 1)而我基本上什么都没有:

  a lbound trend trendp ubound tau sig nruns autocor valid_frac linear intercept
  1 a     NA    NA     NA     NA  NA  NA    NA      NA         NA     NA        NA
  2 b     NA    NA     NA     NA  NA  NA    NA      NA         NA     NA        NA
  3 c     NA    NA     NA     NA  NA  NA    NA      NA         NA     NA        NA
  4 d     NA    NA     NA     NA  NA  NA    NA      NA         NA     NA        NA
  5 e     NA    NA     NA     NA  NA  NA    NA      NA         NA     NA        NA

我假设我正在错误地访问zyp。有谁知道我做错了什么?

TIA

1 个答案:

答案 0 :(得分:1)

将日期转换为其基础整数值应该会成功:

myDates <- as.integer( 
as.Date(c("2009-11-24","2009-12-03","2010-01-19","2010-02-18","2010-03-04")))
myData <- c(10.17,10.91,11.72,12.5,13.43)
a <- c("a","b","c","d","e")
df = data.frame(a,myData,myDates)
 zyp.sen(myDates~myData, df)

Call:
NULL

Coefficients:
Intercept     myData  
 14204.66      36.12