Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a date field in a ts?

Tags:

r

time-series

I wonder how I can make use of an already existing date field when creating a ts in R. Sometimes you simply have a date before you have a ts object, e.g.

x <- as.Date("2008-01-01") + c(30,60,90,120,150)
# add some data to it    
df = data.frame(datefield=x,test=1:length(x))

Now, is there a way to use the datefield of the df to as an index when creating a ts object? Because:

   ts(df$test,start=c(2008,1,2),frequency=12)

(obviuously) completely ignores the date information I already have. Making use of ts methods like acf is the reason why I´d like to make it a ts object. I typcically use monthly an quarterly time series...

like image 973
Matt Bannert Avatar asked Mar 03 '11 14:03

Matt Bannert


People also ask

Is there a date type in TypeScript?

The Date object represents a date and time functionality in TypeScript. It allows us to get or set the year, month and day, hour, minute, second, and millisecond.

How do I cast a Date in TypeScript?

Use the Date() constructor to convert a string to a Date object in TypeScript, e.g. const date = new Date('2024-07-21') . The Date() constructor takes a valid date string as a parameter and returns a Date object. Copied! We used the Date() constructor to convert a string to a Date object.

What should be the type of the Date in TypeScript?

Use the Date type to type a Date object in TypeScript, e.g. const date: Date = new Date() . The Date() constructor returns an object that has a type of Date . The interface defines typings for all of the built-in methods on the Date object. Copied!


2 Answers

You don't necessarily need to create new types of objects from scratch; you can always coerce to other classes, including ts as you need to. zoo or xts are arguably to most useful and intuitive but there are others. Here is your example, cast as a zoo object, which we then coerce to class ts for use in acf().

## create the data
x <- as.Date("2008-01-01") + c(30,60,90,120,150)
df = data.frame(datefield=x,test=1:length(x))

## load zoo
require(zoo)
## convert to a zoo object, with order given by the `datefield`
df.zoo <- with(df, zoo(test, order.by = x))
## or to a regular zoo object
df.zoo2 <- with(df, zooreg(test, order.by = x))

Now we can easily go to a ts object using the as.ts() method:

> as.ts(df.zoo)
Time Series:
Start = 13920 
End = 14040 
Frequency = 0.0333333333333333 
[1] 1 2 3 4 5
> ## zooreg object:
> as.ts(df.zoo2)
Time Series:
Start = 13909 
End = 14029 
Frequency = 1 
  [1]  1 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
 [21] NA NA NA NA NA NA NA NA NA NA  2 NA NA NA NA NA NA NA NA NA
 [41] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
 [61]  3 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
 [81] NA NA NA NA NA NA NA NA NA NA  4 NA NA NA NA NA NA NA NA NA
[101] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
[121]  5

Notice the two ways in which the objects are represented (although we could have made the zooreg version the same as the standard zoo object by setting the frequency argument to 0.03333333):

> as.ts(with(df, zooreg(test, order.by = datefield, 
+                       frequency = 0.033333333333333)))
Time Series:
Start = 13920.0000000001 
End = 14040.0000000001 
Frequency = 0.033333333333333 
[1] 1 2 3 4 5

We can use the zoo/zooreg object in acf() and it will get the correct lags (daily observations but every 30 days):

acf(df.zoo)

acf figure

Whether this is intuitive to you or not depends on how you view the time series. We can do the same thing in terms of a 30-day interval via:

acf(coredata(df.zoo))

where we use coredata() to extract the time series itself, ignoring the date information.

acf figure 2

like image 158
Gavin Simpson Avatar answered Sep 18 '22 10:09

Gavin Simpson


I don't know exactly what you're trying to do, but acf also works on simple vectors, given off course it represents a regular time series (i.e. even spaced). Otherwise the result is just bollocks.

>acf(df$test)

enter image description here

Regarding the ts object :

The "dates" you see are just from the print.ts function, so they're not inherent to the ts object. The ts object has no date information in it. You can set the option calender=FALSE to get the standard print out of the ts object.

> ts(df$test,start=2008,frequency=12)
     Jan Feb Mar Apr May
2008   1   2   3   4   5
> print(ts(df$test,start=2008,frequency=12),calendar=F)
Time Series:
Start = c(2008, 1) 
End = c(2008, 5) 
Frequency = 12 
[1] 1 2 3 4 5

Now, the vector you construct looks like :

> x
[1] "2008-01-31" "2008-03-01" "2008-03-31" "2008-04-30" "2008-05-30"

which is or isn't regular, depending on how you see it. If you extract the months, then you have 1 observation for january, 2 for march, 1 for april...: not regular. You have an observation every 30 days : regular. If you have an observation every 30 days, you shouldn't bother about the dates as 365 is not dividable through 30. Hence, one year you'll have 12 observations, another one you'll have 13 observations. So you can't set the frequency in ts in a consequent correct way.

So I'd refrain from using a ts all together, as James already indicated in the comments.

like image 37
Joris Meys Avatar answered Sep 22 '22 10:09

Joris Meys