R Month by Month Percent Growth on an XTS objects

耗尽温柔 提交于 2019-12-04 10:59:29

Define growth: first difference? Percentages? In either case just compute and then plot:

R> index(KB) <- as.Date(index(KB))    ## what you have are dates, not datetimes
R> barplot(diff(KB), ylab="Change in value", main="Growth")

You can also use a standard line plot:

R> plot(diff(KB), type='b', ylab="Change in value", main="Growth")

and change the type= argument of this plot to show bars etc pp. Normally would plot percentage changes but given your first datapoint this is inadmissible here (as noted by Gavin) so diffs it is for this illustration.

Using your dput() data in object dat, and assuming you mean the month percentage change over previous month, then:

R> (diff(dat) / lag(dat)) * 100
                            A
2008-07-01 05:00:00        NA
2008-08-01 05:00:00       Inf
2008-09-01 05:00:00 195.40230
2008-10-01 05:00:00  14.39689
2008-11-01 04:00:00  10.54422
2008-12-01 05:00:00  -8.00000

(Forgot the plot)

plot((diff(dat) / lag(dat)) * 100, main = "",
     ylab = "% growth (for some definition of % growth)")

Not sure how best to handle the second month - % growth was infinite...

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!