11

を使用して不規則な時系列のイベント (投稿)xtsがあり、ローリング ウィークリー ウィンドウ (または隔週、または 3 日など) で発生するイベントの数を計算したいと考えています。データは次のようになります。

                    postid
2010-08-04 22:28:07    867
2010-08-04 23:31:12    891
2010-08-04 23:58:05    901
2010-08-05 08:35:50    991
2010-08-05 13:28:02   1085
2010-08-05 14:14:47   1114
2010-08-05 14:21:46   1117
2010-08-05 15:46:24   1151
2010-08-05 16:25:29   1174
2010-08-05 23:19:29   1268
2010-08-06 12:15:42   1384
2010-08-06 15:22:06   1403
2010-08-07 10:25:49   1550
2010-08-07 18:58:16   1596
2010-08-07 21:15:44   1608

次のようなものを生成する必要があります

                    nposts
2010-08-05 00:00:00     10
2010-08-06 00:00:00      9
2010-08-07 00:00:00      5

2 日間のウィンドウの場合。rollapplyapply.rollingfromなどを調べましたがPerformanceAnalytics、それらはすべて定期的な時系列データを想定しています。すべての時間を投稿が発生した日に変更し、毎日ddplyグループ化するようなものを使用してみました。ただし、ユーザーが毎日投稿するとは限らないため、時系列は依然として不規則になります。ギャップを 0 で埋めることはできますが、データがかなり膨らむ可能性があり、すでにかなり大きくなっています。

私は何をすべきか?

4

3 に答える 3

5

xts を使用したソリューションは次のとおりです。

x <- structure(c(867L, 891L, 901L, 991L, 1085L, 1114L, 1117L, 1151L, 
  1174L, 1268L, 1384L, 1403L, 1550L, 1596L, 1608L), .Dim = c(15L, 1L),
  index = structure(c(1280960887, 1280964672, 1280966285, 
  1280997350, 1281014882, 1281017687, 1281018106, 1281023184, 1281025529, 
  1281050369, 1281096942, 1281108126, 1281176749, 1281207496, 1281215744),
  tzone = "", tclass = c("POSIXct", "POSIXt")), class = c("xts", "zoo"),
  .indexCLASS = c("POSIXct", "POSIXt"), tclass = c("POSIXct", "POSIXt"),
  .indexTZ = "", tzone = "")
# first count the number of observations each day
xd <- apply.daily(x, length)
# now sum the counts over a 2-day rolling window
x2d <- rollapply(xd, 2, sum)
# align times at the end of the period (if you want)
y <- align.time(x2d, n=60*60*24)  # n is in seconds
于 2014-02-12T14:14:01.197 に答える
4

これはうまくいくようです:

# n = number of days
n <- 30
# w = window width. In this example, w = 7 days
w <- 7

# I will simulate some data to illustrate the procedure
data <- rep(1:n, rpois(n, 2))

# Tabulate the number of occurences per day:
# (use factor() to be sure to have the days with zero observations included)
date.table <- table(factor(data, levels=1:n))  

mat <- diag(n)
for (i in 2:w){
  dim <- n+i-1
  mat <- mat + diag(dim)[-((n+1):dim),-(1:(i-1))]
  }

# And the answer is.... 
roll.mean.7days <- date.table %*% mat

遅すぎないようです(ただし、mat行列は次元n * nを取得します)。n=30をn=3000(900万要素のマトリックス= 72 MBを作成)に置き換えようとしましたが、それでも私のコンピューターではかなり高速でした。非常に大きなデータセットの場合は、最初にサブセットを試してください。マトリックスパッケージ(bandSparse)の一部の関数を使用してマトリックスを作成する方が高速matです。

于 2012-10-23T08:45:09.710 に答える