在 ts 中使用日期字段? [英] Using a date field in a ts?

查看:26
本文介绍了在 ts 中使用日期字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在 R 中创建 ts 时如何利用已经存在的日期字段.有时您在拥有 ts 对象之前只是有一个日期,例如

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))

现在,有没有办法在创建 ts 对象时使用 df 的日期字段作为索引?因为:

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)

(显然)完全忽略了我已有的日期信息.使用像 acf 这样的 ts 方法是我想让它成为 ts 对象的原因.我通常使用每月和每季度的时间序列...

(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...

推荐答案

您不一定需要从头开始创建新类型的对象;你总是可以根据需要强制转换到其他类,包括 ts .zooxts 可以说是最有用和最直观的,但还有其他的.这是您的示例,将其转换为动物园对象,然后我们将其强制转换为 ts 类以在 acf() 中使用.

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))

现在我们可以使用 as.ts() 方法轻松转到 ts 对象:

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

注意对象的两种表示方式(尽管我们可以通过将 frequency 参数设置为 0.03333333 来使 zooreg 版本与标准动物园对象相同):

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

我们可以在 acf() 中使用 zoo/zooreg 对象,它将获得正确的滞后(每日观察,但每 30 天):

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)

这对您来说是否直观取决于您如何查看时间序列.我们可以通过以下方式以 30 天为间隔做同样的事情:

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))

我们使用 coredata() 提取时间序列本身,忽略日期信息.

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

这篇关于在 ts 中使用日期字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆