简体   繁体   中英

Using dates in R for Theil-Sen

I am trying to use dates as my X variable in a Theil-Sen slope estimation and I am having difficulty using the R package 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)

And then I get the following error:

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

I am also trying to use zyp.trend.dataframe(df, 1) and I get essentially nothing:

  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

I am assuming that I am accessing zyp incorrectly. Does anyone know what I am doing wrong ?

TIA

Converting Dates to their underlying integer values should succeed:

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  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM