R:使用 made 函数去除时间序列季节性

R: Removing time series seasonality with made 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"))