2017年3月5日日曜日

Calculate rolling 4 quarter's sum of eps and plot its graph.


when eps_xts is like below.

>eps_xts
           ope  rep
1988-01-01 5.48 5.53
1988-04-01 6.05 6.22
      <SKIP>
2016-01-01 23.97 21.72
2016-04-01 25.70 23.28
2016-07-01 28.69 25.39

calculate rolling 4 quarter sum of eps from data and remove N/A.

  na.trim(filter(eps_xts$ope,rep(1,4))

the length of the result and convert it to # of quarters.

  (length(na.trim(filter(eps_xts$ope,rep(1,4))))-1)*3

the last quarter is..

  as.Date(index(last(eps_xts)))

the starting quarter is calculated from data above. "last" to get the last entry of eps_xts, "index" to pick up date data from the entry and "mondate" to convert date to calculate-able data.

  as.Date(mondate(index(last(eps_xts))) - (length(na.trim(filter(eps_xts$ope,rep(1,4))))-1)*3)

get sequence of dates and plot the graph with seq and data.

plot(seq(as.Date(mondate(index(last(eps_xts))) - (length(na.trim(filter(eps_xts$ope,rep(1,4))))-1)*3),as.Date(index(last(eps_xts))),by="quarters"),na.trim(filter(eps_xts$ope,rep(1,4))),type="h")



0 件のコメント: