R使包每月数据无效

时间:2018-10-01 20:30:38

标签: r time-series tibble

我正在复制R Anomalize package home page中的示例,但是似乎一旦将每日样本数据替换为Monthly,代码就会中断。

下面是我使用的代码,它产生了以下错误:

# load necessary packages
library(tidyverse)
library(anomalize)

set.seed(1234)
df = tibble(MONTH = rep(seq(from = as.Date("2000-01-01"), 
                            to = as.Date("2018-06-01"), 
                            by = "1 month"),2),
            variable = rep(c("A","B"), each = length(seq(from = as.Date("2000-01-01"), 
                                                         to = as.Date("2018-06-01"), 
                                                         by = "1 month"))),
            value = rnorm(2*length(seq(from = as.Date("2000-01-01"), 
                                       to = as.Date("2018-06-01"), 
                                       by = "1 month"))))
df %>% 
  time_decompose(value, merge = TRUE, method = "twitter") %>% 
  anomalize(remainder, method = "gesd")

这是错误消息

Error in mutate_impl(.data, dots) : 
  Evaluation error: Only year, quarter, month, week, and day periods are allowed for an index of class Date.

1 个答案:

答案 0 :(得分:0)

概述

通过加载tibbletime包,我能够将df转换为tbl_time对象。这使我可以将对象的周期设置为几个月。之后,您的代码将按预期运行。

注意:tibbletime软件包开发is on hold截至2018年8月。此答案使用version 0.1.1 from CRAN

代码

# load necessary packages -----
library(anomalize)
library(tibbletime)
library(tidyverse)

# set seed --------
set.seed(1234)

# load necessary data -----
df <- tibble(MONTH = rep(seq(from = as.Date("2000-01-01"), 
                             to = as.Date("2018-06-01"), 
                             by = "1 month"),2),
             variable = rep(c("A","B"), each = length(seq(from = as.Date("2000-01-01"), 
                                                          to = as.Date("2018-06-01"), 
                                                          by = "1 month"))),
             value = rnorm(2*length(seq(from = as.Date("2000-01-01"), 
                                        to = as.Date("2018-06-01"), 
                                        by = "1 month")))) %>%
  arrange(MONTH) %>%
  # convert tbl to tbl_time object
  as_tbl_time(index = MONTH) %>%
  # specify the tbl_time periodicity to monthly
  as_period(period = "month")

# conduct anomaly detection -----
results <-
  df %>%
  # decompose a time series in preparation for anomaly detection
  time_decompose(target = value
                 , merge = TRUE
                 , method = "twitter") %>%
  # detect anomalies using the tidyverse
  anomalize(target = remainder, method = "gesd")

# view results ----
results %>%
  select(MONTH, variable, value, anomaly)
# A time tibble: 222 x 4
# Index: MONTH
#    MONTH      variable  value anomaly
#    <date>     <chr>     <dbl> <chr>  
#  1 2000-01-01 A        -1.21  No     
#  2 2000-02-01 A         0.277 No     
#  3 2000-03-01 A         1.08  No     
#  4 2000-04-01 A        -2.35  No     
#  5 2000-05-01 A         0.429 No     
#  6 2000-06-01 A         0.506 No     
#  7 2000-07-01 A        -0.575 No     
#  8 2000-08-01 A        -0.547 No     
#  9 2000-09-01 A        -0.564 No     
# 10 2000-10-01 A        -0.890 No     
# ... with 212 more rows

# end of script #

会话信息

R version 3.5.1 (2018-07-02)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.6

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] bindrcpp_0.2.2   forcats_0.3.0    stringr_1.3.1    dplyr_0.7.6      purrr_0.2.5     
 [6] readr_1.1.1      tidyr_0.8.1      tibble_1.4.2     ggplot2_3.0.0    tidyverse_1.2.1 
[11] tibbletime_0.1.1 anomalize_0.1.1 

loaded via a namespace (and not attached):
 [1] zoo_1.8-3         tidyselect_0.2.4  haven_1.1.2       lattice_0.20-35  
 [5] timetk_0.1.1.1    colorspace_1.3-2  htmltools_0.3.6   viridisLite_0.3.0
 [9] yaml_2.2.0        rlang_0.2.2       pillar_1.3.0      glue_1.3.0       
[13] withr_2.1.2       modelr_0.1.2      readxl_1.1.0      bindr_0.1.1      
[17] plyr_1.8.4        munsell_0.5.0     gtable_0.2.0      cellranger_1.1.0 
[21] rvest_0.3.2       evaluate_0.11     knitr_1.20        xts_0.11-0       
[25] broom_0.5.0       Rcpp_0.12.18      backports_1.1.2   scales_1.0.0     
[29] jsonlite_1.5      gridExtra_2.3     hms_0.4.2         digest_0.6.16    
[33] stringi_1.2.4     grid_3.5.1        rprojroot_1.3-2   cli_1.0.0        
[37] tools_3.5.1       magrittr_1.5      lazyeval_0.2.1    crayon_1.3.4     
[41] pkgconfig_2.0.2   xml2_1.2.0        lubridate_1.7.4   assertthat_0.2.0 
[45] rmarkdown_1.10    httr_1.3.1        rstudioapi_0.7    viridis_0.5.1    
[49] R6_2.2.2          nlme_3.1-137      compiler_3.5.1