R中的滚动计算,季度数据,但滚动应按年度进行,cumprod

  • 本文关键字:滚动 cumprod 数据 计算 季度 r dplyr
  • 更新时间 :
  • 英文 :


>我有以下数据

PERIOD    GROWTH    PRICE
2011K1    0.88    0.88
2011K2    0.93    0.93
2011K3    0.96    0.96
2011K4    0.98    0.98
2012K1    1.13
2012K2    1.16
2012K3    1.12
2012K4    1.17
2013K1    1.07
2013K2    1.11
2013K3    1.03
2013K4    1.03
In 2011 PRICE = GROWTH
In 2012K1 PRICE = GROWTH[2012K1]*avg(PRICE in 2011)
In 2012K2 PRICE = GROWTH[2012K2]*avg(PRICE in 2011)
In 2012K3 PRICE = GROWTH[2012K3]*avg(PRICE in 2011)
In 2012K4 PRICE = GROWTH[2012K4]*avg(PRICE in 2011)
In 2013K1 PRICE = GROWTH[2013K1]*avg(PRICE in 2012)
In 2013K2 PRICE = GROWTH[2013K2]*avg(PRICE in 2012)
In 2013K3 PRICE = GROWTH[2013K3]*avg(PRICE in 2012)
In 2013K4 PRICE = GROWTH[2013K4]*avg(PRICE in 2012)

在每个季度中,上一季度的平均价格用于乘以该特定季度的增长,即同一年中的每个季度乘以相同的平均价格,即前一年的平均价格。

我尝试使用 cumprod((,但当我的数据是季度时,无法让它每年滚动一次。我可以做for循环,问题是我必须为成千上万的产品做这件事。

有什么建议吗?

给定最后注释中的数据计算季度,qtr然后循环计算 PRICE 的行。 不使用任何包。

run <- function(DF, k = 4) {
nr <- nrow(DF)
DF$qtr <- 1:k
for(i in (k+1):nr) DF$PRICE[i] <- DF$GROWTH[i] * mean(DF$PRICE[i-DF$qtr[i]-(k-1):0])
DF
}
run(DF)

给:

PERIOD GROWTH    PRICE qtr
1  2011K1   0.88 0.880000   1
2  2011K2   0.93 0.930000   2
3  2011K3   0.96 0.960000   3
4  2011K4   0.98 0.980000   4
5  2012K1   1.13 1.059375   1
6  2012K2   1.16 1.087500   2
7  2012K3   1.12 1.050000   3
8  2012K4   1.17 1.096875   4
9  2013K1   1.07 1.148578   1
10 2013K2   1.11 1.191516   2
11 2013K3   1.03 1.105641   3
12 2013K4   1.03 1.105641   4

注意

Lines <- "PERIOD    GROWTH    PRICE
2011K1    0.88    0.88
2011K2    0.93    0.93
2011K3    0.96    0.96
2011K4    0.98    0.98
2012K1    1.13
2012K2    1.16
2012K3    1.12
2012K4    1.17
2013K1    1.07
2013K2    1.11
2013K3    1.03
2013K4    1.03"
DF.orig <- read.table(text = Lines, header = TRUE, fill = TRUE, as.is = TRUE)

--更新:意识到此答案产生不正确的结果 -- @Rebecca

另一种选择:)

# I'll use tidyverse for this approach.
library(tidyverse)
# First, I'll generate a dataset similar to yours.
data <- tibble(year = rep(2011:2013, each=4),
quarter = rep(1:4, times=3),
growth_quarter = c(0.88,
0.93,
0.96,
0.98,
1.13,
1.16,
1.12,
1.17,
1.07,
1.11,
1.03,
1.03))
# Create a new tibble with desired output.
data_m <- data %>%
# Find the average growth per year.
group_by(year) %>%
mutate(growth_annual = mean(growth_quarter)) %>%
# Remove grouping by year for next calculations.
ungroup() %>%
# Organize by year and quarter to ensure consistent results for calculation in next step.
arrange(year, quarter) %>%
# Multiply current quarter's growth by last year's average growth.
mutate(growth_quarter*lag(growth_annual))

如果您有任何问题,请告诉我!

最新更新