r - 错误:错误 time_decompose():在 R 中执行异常检测时
问题描述
这里我的数据
zem=zem = read.table(
V1
75
74,7
74,4
74,1
73,8
75,5
73,3
73,1
72,9
73
72,8
72,3
72,1
71,9
71,7
71,6
71,3
71,4
71,3
71,2
71,1
70
69,5
69
68,5)
我想检测异常值。所以我决定使用library(anomalize)
. 下面的代码
library(anomalize) #tidy anomaly detectiom
library(tidyverse) #tidyverse packages like dplyr, ggplot, tidyr
zem %>%
time_decompose(V1) %>%
anomalize(remainder) %>%
time_recompose() %>%
filter(anomaly == 'Yes')
我得到了错误
Error: Error time_decompose(): Object is not of class tbl_df or tbl_time.
怎么了?我怎样才能得到想要的结果?
V1 Anomaly
1 75.0 no
2 74.7 no
3 74.4 no
4 74.1 no
5 73.8 no
6 75.5 yes
7 73.3 no
8 73.1 no
9 72.9 no
10 73.0 no
11 72.8 no
12 72.3 no
13 72.1 no
14 71.9 no
15 71.7 no
16 71.6 no
17 71.3 no
18 71.4 no
19 71.3 no
20 71.2 no
21 71.1 no
22 70.0 no
23 69.5 no
24 69.0 no
25 68.5 no
我刚刚尝试为我的任务修改此代码
https://towardsdatascience.com/tidy-anomaly-detection-using-r-82a0c776d523
解决方案
该time_decompose()
函数需要以下形式的数据:
一个 tibble 或 tbl_time 对象
(来自?time_decompose
)
也许zem
是一个data.frame?您可以将其包含as_tibble()
在管道中以确保它提前是一个小标题。
此外,它希望处理基于时间的数据:
它旨在处理基于时间的数据,因此必须有一列包含日期或日期时间信息。
我在您的测试数据中添加了一个带有日期的列。这是一个工作示例:
library(anomalize)
library(tidyverse)
zem$date <- as.Date(Sys.Date() + 1:nrow(zem))
zem %>%
as_tibble() %>%
time_decompose(V1) %>%
anomalize(remainder) %>%
time_recompose() %>%
filter(anomaly == 'Yes')
输出
Converting from tbl_df to tbl_time.
Auto-index message: index = date
frequency = 7 days
trend = 12.5 days
# A time tibble: 4 x 10
# Index: date
date observed season trend remainder remainder_l1 remainder_l2 anomaly recomposed_l1 recomposed_l2
<date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr> <dbl> <dbl>
1 2020-12-28 75.5 0.782 73.8 0.934 -0.555 0.624 Yes 74.0 75.2
2 2021-01-04 72.1 0.782 72.3 -0.996 -0.555 0.624 Yes 72.5 73.7
3 2021-01-10 71.3 -0.229 70.7 0.789 -0.555 0.624 Yes 70.0 71.1
4 2021-01-12 71.1 -0.220 70.1 1.24 -0.555 0.624 Yes 69.3 70.5
这是检测到的异常的视觉效果:
zem %>%
as_tibble() %>%
time_decompose(V1) %>%
anomalize(remainder) %>%
plot_anomaly_decomposition()
阴谋
数据
zem <- structure(list(V1 = c(75, 74.7, 74.4, 74.1, 73.8, 75.5, 73.3,
73.1, 72.9, 73, 72.8, 72.3, 72.1, 71.9, 71.7, 71.6, 71.3, 71.4,
71.3, 71.2, 71.1, 70, 69.5, 69, 68.5), date = structure(c(18619,
18620, 18621, 18622, 18623, 18624, 18625, 18626, 18627, 18628,
18629, 18630, 18631, 18632, 18633, 18634, 18635, 18636, 18637,
18638, 18639, 18640, 18641, 18642, 18643), class = "Date")), row.names = c(NA,
-25L), class = "data.frame")
推荐阅读
- design-patterns - 简化代码以取消过多的 if-else-statements
- r - Residuals.cph error "you must specify x=TRUE in the fit"
- image - 如何使用“反向”vstack 组合视频和图像?
- scala - Play framework file upload and CSRF still not working
- java - Java not loading .dylib and .jnilib
- vim - 使用 rmarkdown 在 vim 中突出显示和语法问题
- javascript - 如何获取“value + id”作为值?
- python - AttributeError:module 'imp' has no attribute 'find_module'
- r - Arithmetical calculation with RStudio
- mysql - SQL 查询中的 JOIN 运算符未返回预期结果