我可以使用非常规时间步长对 R 中的动物园对象执行自相关/滞后分析吗?如果是这样,怎么办?

发布于 2024-12-28 10:01:12 字数 378 浏览 5 评论 0原文

我可以使用非常规时间步长对 R 中的动物园对象执行自相关/滞后分析吗?如果是这样,怎么办?

我在这里找到的唯一一篇文章涉及常规时间序列。我以不规则的时间步长进行了一系列观察。例如,(t,y) = (0,2668), (36.62,2723), (42,2723),...,其中

  • t 是时间小时,
  • y 是(分类*)观察值。 ... *从原始帖子编辑

我想寻找每日(滞后= 24)和每周(滞后= 168)的滞后相关性,以查看某些类别的观察是否在这些滞后间隔处/附近重复。在 R 中有没有办法做到这一点?我为我的数据创建了一个动物园对象,但找不到任何有关如何执行此操作的文档。

Can I perform autocorrelation / lag analysis on a zoo object in R with non-regular time steps? If so, how?

The only other post I could find here dealt with regular time series. I have a sequence of observations taken at irregular time steps. For example, (t,y) = (0,2668), (36.62,2723), (42,2723),... where

  • t is the time in hours, and
  • y is the (categorical*) observation. ... *edited from original post

I would like to look for lag correlations daily (lag = 24) and weekly (lag = 168) to see whether certain categories of observation repeat at / near these lag intervals. Is there a way to do this in R? I created a zoo object for my data but have been unable to find any documentation concerning how to do this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾北清歌寒 2025-01-04 10:01:12

您可以使用aggregate将数据转换为每日数据和每日数据。每周间隔,然后使用对常规时间序列执行此操作的任何函数计算自相关(例如 acf)。例如:

# make a data set to play with
library(zoo)
ts <- sort(runif(100)*168*3) # 100 observations over 3 weeks
ys <- runif(100)       # y values
z <- zoo(ys, order.by=ts)

# ** convert to daily/weekly. ?aggregate.zoo
# NOTE: can use ts instead of index(z)
z.daily <- aggregate(z,index(z) %/% 24)    # has 21 elements (one per day)
z.weekly <- aggregate(z,index(z) %/% 168)  # has 3 elements (one per week)

# Now compute correlation, lag 1 (index in z.daily/weekly)
daily.acf <- acf(z.daily, lag.max=1)[1]
weekly.acf <- acf(z.weekly, lag.max=1)[1]

aggregatez 转换为每日或每周数据,您可以在其中对每天/每周发生的所有事件进行求和。它通过查看index(z) %/% 24(或168)来进行分组,这是观察时间除以24(即发生的日期)的整数部分。

然后,acf 函数计算自相关(lag 位于矢量索引上,而不是时间上)。

我对统计不太了解,我注意到的一件事是,如果您这样做:

weekly.acf <- acf(z.daily,lag.max=7)[7]

您会得到与从 z.weekly 计算自相关时不同的答案,因为它在 每日数据的滞后为 7,而不是每周数据的滞后为 1——所以我不确定我正在做的是否真的是您想要的。

You can use aggregate to convert your data into daily & weekly intervals, and then calculate the autocorrelation with whatever function does it for regular time series (say acf). e.g.:

# make a data set to play with
library(zoo)
ts <- sort(runif(100)*168*3) # 100 observations over 3 weeks
ys <- runif(100)       # y values
z <- zoo(ys, order.by=ts)

# ** convert to daily/weekly. ?aggregate.zoo
# NOTE: can use ts instead of index(z)
z.daily <- aggregate(z,index(z) %/% 24)    # has 21 elements (one per day)
z.weekly <- aggregate(z,index(z) %/% 168)  # has 3 elements (one per week)

# Now compute correlation, lag 1 (index in z.daily/weekly)
daily.acf <- acf(z.daily, lag.max=1)[1]
weekly.acf <- acf(z.weekly, lag.max=1)[1]

The aggregate converts z to daily or weekly data where you sum all occurences for each day/week. It does the grouping by looking at index(z) %/% 24 (or 168) which is the integer part of the hour of observation divided by 24 (ie, the day it occurs).

Then the acf function calculates autocorrelation (with the lag being on indices of the vector, not on time).

I don't really know much about statistics, and one thing I noticed was that if you do:

weekly.acf <- acf(z.daily,lag.max=7)[7]

you get a different answer from when you calculate autocorrelation from z.weekly, because it's doing autocorrelation on daily data with a lag of 7 as opposed to weekly data with a lag of 1 -- so I'm not sure if what I'm doing is actualy what you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文