Raj
Raj

Reputation: 103

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

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.

Upvotes: 3

Views: 1735

Answers (4)

Thomas Halamuda
Thomas Halamuda

Reputation: 21

There is missing a "group_by" at this pipeline. This error is also in the examples of the anomalize package. Error was gone after adding it. This worked:

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

Upvotes: 1

kray
kray

Reputation: 377

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.

Upvotes: 0

Slyrs
Slyrs

Reputation: 131

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

Upvotes: 5

GGamba
GGamba

Reputation: 13680

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

Upvotes: 1

Related Questions