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

大憨熊 提交于 2019-12-10 10:26:47

问题


I am using Anomalize package to detect the Anomalies, but I am getting the mentioned error even though I have defined the Date as index :

Sample Code :

x <- as.data.frame(data %>%
  group_by(date,acc_id) %>%
  summarise(count = as.numeric(n_distinct(d_id))) %>%
  ungroup())

x$acc_id <- as.character(x$acc_id)

x <- x %>% 
  tibbletime::as_tbl_time(index = date)


x %>%
  time_decompose(count, method = "twitter", trend = "2 months") %>%
  anomalize(remainder, method = "gesd") %>%
  time_recompose() %>%
  plot_anomalies(time_recomposed = TRUE)

Error :

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

dput(head(x))

structure(list(date = structure(c(17532, 17532, 17532,  17532, 17532, 17532), class = "Date"), acc_id = c("a44444",  "gg555", "0195459b-5809-4b54-89b5-1a4376c9f126",  "ggg6546", "hhjh77",  "hhjh68777"), count = c(3, 1, 1, 1,  1, 1)), .Names = c("date", "acc_id", "count"), row.names = c(NA, 
-6L), class = c("tbl_time", "tbl_df", "tbl", "data.frame"), index_quo = ~date, index_time_zone = "UTC")

I have the objective to group by date and some other factor not alone with the date.


回答1:


I had the same issue. What helped me was to correctly define your date format:

library(tibbletime)
x <- as_tbl_time(x, index = date)

x %>% 
  as_period("daily")



回答2:


From the help:

frequency Controls the seasonal adjustment (removal of seasonality). Input can be either "auto", a time-based definition (e.g. "2 weeks"), or a numeric number of observations per frequency (e.g. 10). Refer to time_frequency().

trend Controls the trend component For stl, the trend controls the sensitivity of the lowess smoother, which is used to remove the remainder. For twitter, the trend controls the period width of the median, which are used to remove the trend and center the remainder.

I think you swapped them:

x %>%
  time_decompose(count, method = "twitter", frequency* = "2 months") %>%
  anomalize(remainder, method = "gesd") %>%
  time_recompose() %>%
  plot_anomalies(time_recomposed = TRUE)

But it's hard to tell if there are any other problems, as the data is not enough




回答3:


I was getting this error as well, until I removed duplicate dates. I was trying to run the code on data that had multiple observations for each site. Once I aggrigated to single obs per day, all was well.



来源:https://stackoverflow.com/questions/50248133/error-in-mutate-impl-data-dots-evaluation-error-only-year-quarter-month

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