R逐月增长百分比XTS对象的增长率

时间:2011-07-29 17:43:32

标签: r time-series xts

我如何绘制以下数据的逐月增长情况:

             A
2008-07-01   0
2008-08-01  87
2008-09-01 257
2008-10-01 294
2008-11-01 325
2008-12-01 299

(在输入格式之前,在约书亚追捕我并在我的睡眠中谋杀我之前):

structure(c(0L, 87L, 257L, 294L, 325L, 299L), .indexCLASS = c("POSIXt", 
"POSIXct"), .indexTZ = "", index = structure(c(1214884800, 1217563200, 
1220241600, 1222833600, 1225512000, 1228107600), tzone = "", tclass = c("POSIXt", 
"POSIXct")), .Dim = c(6L, 1L), .Dimnames = list(NULL, "A"), class = c("xts", 
"zoo"))

2 个答案:

答案 0 :(得分:3)

定义增长:第一个区别?百分比?在任何一种情况下,只需计算然后绘制:

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

enter image description here

您还可以使用标准线图:

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

enter image description here

并更改此图的type=参数以显示条形图等。通常会绘制百分比变化但是给定您的第一个数据点这是不允许的(如Gavin所述)所以差异这是为了说明。

答案 1 :(得分:1)

使用对象dput()中的dat数据,假设您的月份百分比变化超过上个月,那么:

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

(忘了情节)

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

enter image description here

不确定如何最好地处理第二个月 - %增长是无限的......