使用Anomalize R包遇报错:'arg'需为指定时间单位之一
Hey there, let's work through this error you're facing with the anomalize package in R 3.4.3. That error message is telling you two key things: first, there's a problem with the time frequency argument you're using, and second, since R 3.4.3 is pretty outdated (released back in 2017), version compatibility with newer anomalize and its dependencies is likely playing a role too. Here's how to fix it step by step:
1. Double-check your time unit arguments (singular only!)
The error is triggered because you probably used a plural time unit (like "days" instead of "day") in a function like time_decompose() or anomalize(). The package strictly expects the singular versions listed in the error message: second, minute, hour, day, week, month, year.
For example, if your original code looked like this:
your_data %>% time_decompose(metric_column, frequency = "weeks", trend = "months")
You need to adjust it to use singular units:
your_data %>% time_decompose(metric_column, frequency = "week", trend = "month")
2. Roll back to package versions compatible with R 3.4.3
Newer releases of anomalize and its core dependency tibbletime dropped support for R versions older than 3.5.x. To get things working, you'll need to install older, compatible versions of these packages using devtools:
First, install an older version of devtools that works with R 3.4.3 (if you don't have it already):
install.packages("devtools")
Then install the compatible package versions:
# Install tibbletime v0.1.2 (works with R 3.4.x) devtools::install_version("tibbletime", version = "0.1.2", repos = "http://cran.us.r-project.org") # Install anomalize v0.1.1 (compatible with tibbletime 0.1.2) devtools::install_version("anomalize", version = "0.1.1", repos = "http://cran.us.r-project.org")
3. Ensure your data is formatted as a proper time-based tibble
Anomalize relies on tibbletime to handle time series data correctly. If your date column isn't properly recognized as a time index, it can cause unexpected argument errors. Format your data like this:
library(tibbletime) # Convert date column to Date type (adjust to POSIXct if using datetime) your_data <- your_data %>% mutate(date_column = as.Date(date_column)) %>% # Mark the date column as the time index as_tbl_time(index = date_column)
Final Check
After making these changes, re-run your anomaly detection code. The combination of fixing the time unit arguments, using compatible package versions, and ensuring proper time series formatting should resolve that error.
内容的提问来源于stack exchange,提问作者RPisco

