【问题标题】:replace outlier values of a time series with other values in the time series用时间序列中的其他值替换时间序列的异常值
【发布时间】:2019-05-06 12:52:50
【问题描述】:

我有一个时间序列,我想用其他(不是异常值)值随机替换异常值。 时间序列如下:

date           Category      Value1
2018-09-10       A            .4
2018-09-10       B            .6
2018-09-10       A             4
2018-09-10       C            .2
2018-09-10       D             7

然后我尝试确定异常值如下:

qn = quantile(df1$value1, c(0.05, 0.85), na.rm = TRUE)
df6 = within(df1, { value = ifelse(df1$value1 < qn[1], qn[1], df1$value1)
value = ifelse(df1$value1  > qn[2], qn[2], df1$value1 )})

然后我想用列value1 中的一些值替换异常值,这些值不是异常值。

【问题讨论】:

  • 我确实放了一个我不知道的数据框,对此感到抱歉
  • 你为什么想要那个?异常值也是数据。

标签: r dplyr outliers


【解决方案1】:

如果您想随机替换异常值,一种方法是

#Find out indices which are outliers
inds <- df1$Value1 > qn[2] | df1$Value1 < qn[1]

#Replace those outliers by randomly selecting non-outliers  
df1$Value1[inds] <- sample(df1$Value1[!inds], sum(inds))

df1
#       date  Category Value1
#1 2018-09-10        A    0.4
#2 2018-09-10        B    0.6
#3 2018-09-10        A    4.0
#4 2018-09-10        C    4.0
#5 2018-09-10        D    0.6

数据

df1 <- read.table(text = "date           Category      Value1
                          2018-09-10       A            .4
                          2018-09-10       B            .6
                          2018-09-10       A             4
                          2018-09-10       C            .2
                          2018-09-10       D             7", header =T)

qn <- quantile(df1$Value1, c(0.05, 0.85), na.rm = TRUE)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-23
    • 2021-07-16
    • 2022-12-10
    • 2023-04-09
    • 2018-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多