繁体   English   中英

R:使用制造的 function 删除时间序列季节性

[英]R: Removing time series seasonality with made function

我写了以下 function 来删除数据集的趋势和季节性

# Create a differenced series
difference <- function(dataset, interval=1) {
  my_diff <- NULL
  
  for (i in seq(interval, length(dataset))) {
    value <- dataset[i] - dataset[i-interval]
    my_diff <- c(my_diff, value)
  }
  return(my_diff)
}

# Invert differences forecast
inverse_difference <- function(last_ob, value) {
  return(cumsum(value) + last_ob)
}

# REMOVING A TREND
# Defining a dataset with a linear trend
my_data <- seq(1,20)
my_data

# Difference the dataset
my_diff <- difference(my_data)
my_diff

# Invert the difference
inverted <- inverse_difference(my_data[1], my_diff)
inverted


# REMOVING SEASONALITY
# Defining a dataset with seasonality
my_time <- seq(0,20,0.1)
my_data <- (sin(my_time))

# Plotting seasonal data
plot(my_time, my_data, type="l")
plot(my_data, type="l")

# Difference the dataset
my_diff <- difference(my_data, 20)*2

# Line plot of the differenced seasonal dataset
plot(my_diff, type="l")

# Invert the difference
inverted <- inverse_difference(my_data[1], my_diff)
plot(inverted, type="l")

消除趋势效果很好。 但是,消除季节性并没有,我也不清楚为什么。 当数据不同时,我会期望一条平坦的线。

出了什么问题,我该如何解决?

此代码“翻译”自 Python 代码,位于: https://machinelearningmastery.com/remove-trends-seasonality-difference-transform-python/

以防万一你没有意识到,你在这里重新发明轮子。 R 已经具有函数decomposestl执行季节分解。

使用您自己的数据:

my_time <- seq(0,20,0.1)
my_data <- (sin(my_time))

我们可以通过将my_data转换为时间序列(注意其频率为201/(20/(2*pi)) )然后在结果上调用stl来执行季节性分解:

decomp <- stl(ts(my_data, frequency =  201/(20/(2*pi))), s.window = 25, 
              l.window = 64)

我们可以把它放在一个带有原始时间的数据框中,以获得一些漂亮的组件图:

df <- cbind(time = my_time, as.data.frame(decomp$time.series))

library(ggplot2)

ggplot(df, aes(time, seasonal)) +
  geom_line(aes(color = "Seasonal")) +
  geom_line(aes(y = trend, color = "Trend")) +
  geom_line(aes(y = remainder, color = "Remainder"))

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM