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

发布于 2024-11-26 19:01:13 字数 532 浏览 1 评论 0原文

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

             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

以 dput 格式,在约书亚追捕我并在睡梦中谋杀我之前):

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"))

How might I plot month to month growth for the following data:

             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

(In dput format, before Joshua hunts me down and murders me in my sleep):

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"))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鹿! 2024-12-03 19:01:13

定义增长:第一个区别?百分比?无论哪种情况,只需计算然后绘图:

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

在此处输入图像描述

您还可以使用标准线图:

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

在此处输入图像描述

并更改此图的 type= 参数以显示条形图等页。通常 将绘制百分比变化,但考虑到您的第一个数据点在这里是不可接受的(正如 Gavin 所指出的),因此它对于这个插图来说是不同的。

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")

enter image description here

You can also use a standard line plot:

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

enter image description here

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.

乖不如嘢 2024-12-03 19:01:13

使用对象 dat 中的 dput() 数据,并假设您的意思是与上个月相比的月份百分比变化,那么:(

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)")

在此处输入图像描述

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

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)")

enter image description here

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文