报告了几个季度的季度股息年度化

问题描述 投票:1回答:3

在过去的五年中,我有一个假设公司的季度股息支付历史。这是可重现的代码:

Date<-as.Date(c("2013-11-01", "2014-02-01", "2014-05-01", "2014-08-01", "2014-11-01", "2015-02-01", "2015-05-01", "2015-08-01", "2015-11-01", "2016-02-01", "2016-05-01", "2016-08-01", "2016-11-01", "2017-02-01", "2017-05-01", "2017-08-01", "2017-11-01", "2018-02-01", "2018-05-01", "2018-08-01"))
Dividend<-c(0.08, 0.10, 0.10, 0.10, 0.10, 0.11, 0.00, 0.11, 0.11, 0.13, 0.13, 0.13, 0.13, 0.14, 0.14, 0.00, 0.16, 0.15, 0.15, 0.15)
data.frame(Date,Dividend)

以下输出:

         Date Dividend
1  2013-11-01     0.08
2  2014-02-01     0.10
3  2014-05-01     0.10
4  2014-08-01     0.10
5  2014-11-01     0.10
6  2015-02-01     0.11
7  2015-05-01     0.00
8  2015-08-01     0.11
9  2015-11-01     0.11
10 2016-02-01     0.13
11 2016-05-01     0.13
12 2016-08-01     0.13
13 2016-11-01     0.13
14 2017-02-01     0.14
15 2017-05-01     0.14
16 2017-08-01     0.00
17 2017-11-01     0.16
18 2018-02-01     0.15
19 2018-05-01     0.15
20 2018-08-01     0.15

我的问题是如何将其转换为显示每个COMPLETE年度支付股息的产出,如果它们不完整(如本例中为2013年和2018年)而忽略了第一年和最后几年,并且不假设年度股息总是等于季度股息x 4(在我的例子中,2015年和2017年没有相同数量的季度股息)。

所以输出看起来像这样:

Date    Dividend
2014    0.40
2015    0.33
2016    0.52
2017    0.44
r quantmod
3个回答
1
投票

你如何定义不完整?要么你知道公司每年有4个div支付或2个或1个(或12个)。根据你的推理,没有其他答案是正确的,因为他们只是假设第一年和最后一年不应该被考虑在内,但是在2018年11月第四次付款时会发生什么?

由于您使用的是quantmod,因此数据应采用xts格式。使用apply.yearly将数据汇总到年度行,使用数据集中每年的最后一个可用日期。我使用函数的FUN部分返回2列,1列有累计红利,1表示年内支付的红利数。由于公司倾向于进行结构性股息支付(1,2,4或12),您可以使用最大数量的div支付来过滤掉不符合此要求的年份。

当你有像年度特别股息这样的特殊股息时,这可能会失败。支付股息的开始并不总是符合规则。微软在2013年开始支付股息,在季度支付之后只支付了2笔款项。

df1 <- data.frame(Date = as.Date(c("2013-11-01", "2014-02-01", "2014-05-01", "2014-08-01", "2014-11-01", "2015-02-01", "2015-05-01", "2015-08-01", "2015-11-01", "2016-02-01", "2016-05-01", "2016-08-01", "2016-11-01", "2017-02-01", "2017-05-01", "2017-08-01", "2017-11-01", "2018-02-01", "2018-05-01", "2018-08-01")),
                  Dividend = c(0.08, 0.10, 0.10, 0.10, 0.10, 0.11, 0.00, 0.11, 0.11, 0.13, 0.13, 0.13, 0.13, 0.14, 0.14, 0.00, 0.16, 0.15, 0.15, 0.15))

# data in xts form since quantmod is being used.
my_xts <- xts(df1$Dividend, order.by = df1$Date)

annual_data <- apply.yearly(my_xts, function(x) as.matrix(data.frame(sum(x), length(x)))  )
names(annual_data) <- c("total_divs", "no_divs")

# filter data to include only maximum dividens
annual_data[annual_data$no_divs == max(annual_data$no_divs)]

           total_divs no_divs
2014-11-01       0.40       4
2015-11-01       0.33       4
2016-11-01       0.52       4
2017-11-01       0.44       4

1
投票

我们可以从数据帧中删除最大和最小year,然后按year分组并进行总和。

library(dplyr)
library(lubridate)

df %>%
  filter(year(Date) != min(year(Date)) & year(Date) != max(year(Date))) %>%
  group_by(year = year(Date)) %>%
  summarise(Dividend = sum(Dividend))

#   year Dividend
#  <dbl>    <dbl>
#1  2014     0.4 
#2  2015     0.33
#3  2016     0.52
#4  2017     0.44

它的基础R等价物

df$Year <- as.numeric(format(df$Date, "%Y"))
aggregate(Dividend~Year, df[with(df, Year != min(Year) & Year != max(Year)), ],sum)

#  Year Dividend
#1 2014     0.40
#2 2015     0.33
#3 2016     0.52
#4 2017     0.44

1
投票

有了data.table你可以试试

df<-data.frame(Date,Dividend)
library(data.table)
setDT(df)[,.(TotDiv=sum(Dividend)),
          by=year(Date), ][-c(which.min(year),which.max(year))]
   year TotDiv
1: 2014   0.40
2: 2015   0.33
3: 2016   0.52
4: 2017   0.44
© www.soinside.com 2019 - 2024. All rights reserved.