0

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

user918967
  • 2,049
  • 4
  • 28
  • 43
  • never used the zyp package, but I'd look at the documentation for zyp.sen. The error says it doesn't like date objects, so check what types are valid for the method. – DMT Oct 03 '14 at 21:49
  • 1
    Just looked at the docs, not very descriptive but I'd pass the dates in as integers, then convert back after if you want to keep the dates. http://cran.r-project.org/web/packages/zyp/zyp.pdf – DMT Oct 03 '14 at 21:50
  • Hmm. From the zyp documentation I interpret "zyp.trend.dataframe: trend the Sen’s slope (trend) per unit time" as time (e.g. date) do you think that is wrong ? – user918967 Oct 03 '14 at 21:52
  • That's the output, which could be a date, but I don't know. You've got to get to the output though, so you've got to change something to fix your input. Since it complains about myDates being a date object, you'll have to try some other type. Since the example in the documentation uses integers, that's why I suggested it. It is unfortunate they don't specify what type of input is expected – DMT Oct 03 '14 at 22:00

1 Answers1

1

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  
user918967
  • 2,049
  • 4
  • 28
  • 43
IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • Turns out we had a similar idea. I changed it from dates to days between samples with the following code: myDateDay <- c(0,myDates[2]-myDates[1],myDates[3]-myDates[1],myDates[4]-myDates[1],myDates[5]-myDates[1]) – user918967 Oct 04 '14 at 02:24