估算的丢失时间序列数据接近平稳(平坦线)的预测

时间:2019-06-26 17:57:24

标签: r datetime time-series kalman-filter forecast

我有一些随时间推移的玩家数据,但几年来缺少玩家人数。我正在尝试填充/预测不同时间间隔内缺少的玩家计数数据。

此处提供数据:https://1drv.ms/u/s!AvEZ_QPY7OZuhJAlKJN89rH185SUhA

我正在按照下面的说明使用KalmanRun估算缺失值。我尝试了3种使用xts对象转换数据的方法,以及2种将其转换为时间序列数据的方法

https://stats.stackexchange.com/questions/104565/how-to-use-auto-arima-to-impute-missing-values

require(forecast)
library(xts)
library(anytime)
library(DescTools)

df_temp = read.csv("r_share.csv")
df_temp[['DateTime']] <- as.Date(strptime(df_temp[['DateTime']], format='%Y-%m-%d %H:%M:%S'))

3种转换数据的方法;通过返回可解释的非零数据,xts似乎效果最好。

#Convert df_temp to TimeSeries object

df_temp = xts(df_temp$Players, df_temp$DateTime)
#df_temp = as.ts(log(df_temp$Players), start = start(df_temp$DateTime), end = end(df_temp$DateTime), frequency = 365)
#df_temp = ts(df_temp$Players, start = c(2013, 02, 02), end = c(2016, 01, 31), frequency = 365)

拟合和绘图:

fit <- auto.arima(df_temp, seasonal = TRUE)
id.na <- which(is.na(df_temp))

kr <- KalmanRun(index(df_temp), fit$model, update = FALSE)

#?KalmanRun$tol

for (i in id.na)
  df_temp[i] <- fit$model$Z %*% kr$states[i,]

plot(df_temp)

预期输出是模仿实际数据中看到的可变性的数据,并且每个间隔都不同,而实际输出则相对平稳且不变(两个间隔的预测值几乎相同)。

1 个答案:

答案 0 :(得分:1)

它必须与模型arima()一起使用吗?。
也许您可以尝试使用Facebook开发的另一种名为Prophet的模型。
在这里您可以找到guidegithub page

如果我了解您想要这样的话:

# Import library

library(prophet)

# Read  data
df = read.csv("C:/Users/Downloads/r_share.csv",sep = ";")

# Transform to date
df["DateTime"] = as.Date(df$DateTime,format = "%d/%m/%Y")

# Change names for the model
colnames(df) = c("ds","y")

# call model
m = prophet(df)


# make "future" just one day greater than past
future = make_future_dataframe(m,periods = 1)

# predict the points
forecast = predict(m,future)

# plot results
plot(m,forecast)

plot