首页 > 解决方案 > R 中的滚动计算,季度数据,但滚动应按年度计算,cumprod

问题描述

我有以下数据

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循环,问题是我必须为成千上万的产品做这个。

有什么建议么?

标签: rdplyr

解决方案


-- 更新:意识到这个答案会产生不正确的结果 -- @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))

请让我知道,如果你有任何问题!


推荐阅读