如何绘制时间序列的第一个差异

时间:2017-12-10 23:04:28

标签: r difference

我知道这是非常基本的,但我似乎无法让代码工作。我有一个时间序列数据集,我试图通过采取第一个区别使其静止,但我不知道在R中使用什么代码。数据不在库中;我刚刚将它导入为csv文件。

我尝试的是plot(diff(data), type="o", main="first difference"),我收到了错误

  

r [i1]中的错误 - r [-length(r): - (length(r) - lag + 1L)]:       二元运算符的非数字参数

我是R的新手,所以我不知道这意味着什么。

> dput(hotel)
structure(list(Month = 1:168, Occupancy = c(501L, 488L, 504L, 
578L, 545L, 632L, 728L, 725L, 585L, 542L, 480L, 530L, 518L, 489L, 
528L, 599L, 572L, 659L, 739L, 758L, 602L, 587L, 497L, 558L, 555L, 
523L, 532L, 623L, 598L, 683L, 774L, 780L, 609L, 604L, 531L, 592L, 
578L, 543L, 565L, 648L, 615L, 697L, 785L, 830L, 645L, 643L, 551L, 
606L, 585L, 553L, 576L, 665L, 656L, 720L, 826L, 838L, 652L, 661L, 
584L, 644L, 623L, 553L, 599L, 657L, 680L, 759L, 878L, 881L, 705L, 
684L, 577L, 656L, 645L, 593L, 617L, 686L, 679L, 773L, 906L, 934L, 
713L, 710L, 600L, 676L, 645L, 602L, 601L, 709L, 706L, 817L, 930L, 
983L, 745L, 735L, 620L, 698L, 665L, 626L, 649L, 740L, 729L, 824L, 
937L, 994L, 781L, 759L, 643L, 728L, 691L, 649L, 656L, 735L, 748L, 
837L, 995L, 1040L, 809L, 793L, 692L, 763L, 723L, 655L, 658L, 
761L, 768L, 885L, 1067L, 1038L, 812L, 790L, 692L, 782L, 758L, 
709L, 715L, 788L, 794L, 893L, 1046L, 1075L, 812L, 822L, 714L, 
802L, 748L, 731L, 748L, 827L, 788L, 937L, 1076L, 1125L, 840L, 
864L, 717L, 813L, 811L, 732L, 745L, 844L, 833L, 935L, 1110L, 
1124L, 868L, 860L, 762L, 877L)), .Names = c("Month", "Occupancy"
), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-168L), spec = structure(list(cols = structure(list(Month = structure(list(), class = c("collector_integer", 
"collector")), Occupancy = structure(list(), class = c("collector_integer", 
"collector"))), .Names = c("Month", "Occupancy")), default = structure(list(), class = c("collector_guess", 
"collector"))), .Names = c("cols", "default"), class = "col_spec"))

3 个答案:

答案 0 :(得分:2)

TS

问题是你有一个R数据框

class(employment)
## [1] "tbl_df"     "tbl"        "data.frame"

虽然可以使用该表示,但如果使用R时间序列对象(例如ts对象或zoo对象(来自zoo包),则会更容易。< / p>

下面我们将数据框转换为ts对象,然后获取其日志的第一个差异并绘制它。由于时间是1,2,3 ......,这是默认值,我们不必指定创建ts对象empts

时的时间
empts <- ts(employment$employmentW)
plot(diff(log(empts)))

(图片后继续)

enter image description here

data.frame

如果您确实想要保留数据框表示,请尝试此操作。请注意,diff将长度减少一个,因此我们删除Month的第一个元素以保持x和y部分的长度相同,以便我们绘制日志的第一个差异对2,3,4,......这很可能什么是想要的。使用时间序列表示,这些都是自动处理的,但不是数据帧表示,因此我们必须手动完成。

plot(diff(log(employmentW)) ~ Month[-1], employment, type = "l")

答案 1 :(得分:1)

简化的基础R代码(使用log(),因为您在稍后几乎相同的帖子中请求它):

plot(diff(log(hotel$Occupancy)), type="o", main="first difference")

答案 2 :(得分:0)

这是使用tidyverse方法的解决方案。

原始数据:

head(hotel)
# # A tibble: 6 x 2
#   Month Occupancy
#   <int>     <int>
# 1     1       501
# 2     2       488
# 3     3       504
# 4     4       578
# 5     5       545
# 6     6       632

计算每月入住率的差异:

library(dplyr)
hotel_diff <- 
  hotel %>%
  mutate(Difference = Occupancy - lag(Occupancy)) %>%
  na.omit() # to remove the first row which has an NA

head(hotel_diff)
# # A tibble: 6 x 3
#   Month Occupancy Difference
#   <int>     <int>      <int>
# 1     2       488        -13
# 2     3       504         16
# 3     4       578         74
# 4     5       545        -33
# 5     6       632         87
# 6     7       728         96

简介:

library(ggplot2)
ggplot(hotel_diff, aes(Month, Difference)) + geom_line()

enter image description here

如果您是R的新手,我强烈建议您跳过base-R数据争用和绘图功能,直接转到tidyverse包,例如dplyrggplot2。你的生活会轻松得多。这里有关于此主题的优秀免费书籍:R for Data Science.