展开/估算 R 中的时间序列
Expand / impute time series in R
我有一个每周需要的时间序列,但我目前有一个季度的数据。例如:
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>
我想要的是每周获取信息,每个总数(totA
到 totD
)都需要除以下个季度(即13,因为 2016 年的季度有 13 周 - 但如果有一年有 53 周,例如 2015 年,偶尔可能是 14 周)这样季度总数是相同的。因此,根据上面的示例,前 26 周变为:
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>
这是使用以下方法获得的:
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!
由于此解决方案中使用的所有信息均来自数据,因此假定最后一个季度的天数(或周数)与上一季度相同。所以你可能想检查一下......
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
我有一个每周需要的时间序列,但我目前有一个季度的数据。例如:
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>
我想要的是每周获取信息,每个总数(totA
到 totD
)都需要除以下个季度(即13,因为 2016 年的季度有 13 周 - 但如果有一年有 53 周,例如 2015 年,偶尔可能是 14 周)这样季度总数是相同的。因此,根据上面的示例,前 26 周变为:
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>
这是使用以下方法获得的:
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!
由于此解决方案中使用的所有信息均来自数据,因此假定最后一个季度的天数(或周数)与上一季度相同。所以你可能想检查一下......
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