首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >扩展/归因于R中的时间序列

扩展/归因于R中的时间序列
EN

Stack Overflow用户
提问于 2021-01-22 04:16:15
回答 1查看 38关注 0票数 0

我有一个时间序列,我需要在每周的基础上,但我目前有季度的数据。例如:

代码语言:javascript
复制
 R> test     
   Quarter       week totA totB totC totD
 1       1 2015-12-28 1745 1720   11 1714
 2       2 2016-03-28 1736 1718    7 1710
 3       3 2016-06-27 1777 1768    5 1750
 4       4 2016-09-26 1833 1815   13 1795
 5       1 2016-12-26 1708 1697    6 1677
 R> 

我想要的是每周一次的信息,每个总计(totAtotD)需要除以下一季度之前的周数(即13周,因为2016年该季度有13周-但非常偶然的是,如果有一年有53周,例如2015年,可能是14周),因此季度总计是相同的。因此,从上面的示例中,前26周变成:

代码语言:javascript
复制
 1            1      2015-12-28 134.9 132.3 0.8462 131.8
 2            2      2016-01-04 134.9 132.3 0.8462 131.8
 3            3      2016-01-11 134.9 132.3 0.8462 131.8
 4            4      2016-01-18 134.9 132.3 0.8462 131.8
 5            5      2016-01-25 134.9 132.3 0.8462 131.8
 6            6      2016-02-01 134.9 132.3 0.8462 131.8
 7            7      2016-02-08 134.9 132.3 0.8462 131.8
 8            8      2016-02-15 134.9 132.3 0.8462 131.8
 9            9      2016-02-22 134.9 132.3 0.8462 131.8
 10          10      2016-02-29 134.9 132.3 0.8462 131.8
 11          11      2016-03-07 134.9 132.3 0.8462 131.8
 12          12      2016-03-14 134.9 132.3 0.8462 131.8
 13          13      2016-03-21 134.9 132.3 0.8462 131.8
 14          14      2016-03-28 133.5 132.2 0.5385 131.5
 15          15      2016-04-04 133.5 132.2 0.5385 131.5
 16          16      2016-04-11 133.5 132.2 0.5385 131.5
 17          17      2016-04-18 133.5 132.2 0.5385 131.5
 18          18      2016-04-25 133.5 132.2 0.5385 131.5
 19          19      2016-05-02 133.5 132.2 0.5385 131.5
 20          20      2016-05-09 133.5 132.2 0.5385 131.5
 21          21      2016-05-16 133.5 132.2 0.5385 131.5
 22          22      2016-05-23 133.5 132.2 0.5385 131.5
 23          23      2016-05-30 133.5 132.2 0.5385 131.5
 24          24      2016-06-06 133.5 132.2 0.5385 131.5
 25          25      2016-06-13 133.5 132.2 0.5385 131.5
 26          26      2016-06-20 133.5 132.2 0.5385 131.5

 R>

这是通过以下方式获得的:

代码语言:javascript
复制
rbind(
    data.frame(Week_number=c(1:13),
               Week_commencing=seq(as.Date("2015-12-28"), by=7, len=13),
               totA=rep(1754/13,13),
               totB=rep(1720/13,13),
               totC=rep(11/13,13),
               totD=rep(1714/13,13)
               ),
    data.frame(Week_number=c(14:26),
               Week_commencing=seq(as.Date("2016-03-28"), by=7, len=13),
               totA=rep(1736/13,13),
               totB=rep(1718/13,13),
      totC=rep(7/13,13),
      totD=rep(1710/13,13)
      )
)

But there's clearly a better way of doing it rather than manually... The data set is, of course, much larger!

I've tried a few things, but other than creating a sequence of weeks and then filling it in manually as above, I'm going around in circles. I'm sure there's a way to do it in the tidyverse, but I can't figure out how (most of my R is self-taught, and from before when tidyverse was available). Any help would be appreciated!
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-22 06:11:25

由于此解决方案中使用的所有信息都来自数据,因此假设最后一个季度的天数(或周数)与上一个季度相同。所以你可能会想要检查一下...

代码语言:javascript
复制
library(dplyr)
library(lubridate)
library(purrr)
library(tidyr)

test %>%
  mutate(week = ymd(week),
         weekCount = if_else(week != max(week),
                             as.double(abs(week - lead(week)))/7,
                             as.double(abs(week - lag(week)))/7),
         weekInQuarter = map(weekCount, ~ seq_len(.)),
         across(totA:totD, ~ ./weekCount)) %>%
  unnest(weekInQuarter) %>%
  mutate(week = week + weeks(weekInQuarter - 1)) %>%
  select(- weekCount)

#     Quarter week        totA  totB  totC  totD weekInQuarter
#       <dbl> <date>     <dbl> <dbl> <dbl> <dbl>         <int>
#   1       1 2015-12-28  134.  132. 0.846  132.             1
#   2       1 2016-01-04  134.  132. 0.846  132.             2
#   3       1 2016-01-11  134.  132. 0.846  132.             3
#   4       1 2016-01-18  134.  132. 0.846  132.             4
#   5       1 2016-01-25  134.  132. 0.846  132.             5
#   6       1 2016-02-01  134.  132. 0.846  132.             6
#   7       1 2016-02-08  134.  132. 0.846  132.             7
#   8       1 2016-02-15  134.  132. 0.846  132.             8
#   9       1 2016-02-22  134.  132. 0.846  132.             9
#  10       1 2016-02-29  134.  132. 0.846  132.            10
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65835108

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档