TSstudio包提供了一组时间序列数据的描述性和预测性分析工具。这包括用于预处理时间序列数据的实用功能、基于plotly包引擎的交互式可视化功能,以及用于训练和评估预测的forecast、bsts等包中的时间序列预测模型的一组工具,下面小编对该包的使用进行介绍。
- ts_info对象属性函数
install.packages("TSstudio")
library(TSstudio)
> NGC
Qtr1 Qtr2 Qtr3 Qtr4
2000 2050.6 1513.1 1475.0 2587.5
2001 2246.6 1444.4 1494.1 2120.2
2002 2258.4 1591.4 1542.2 2378.9
> ts_info(NGC)
The NGC series is a ts object with 1 variable and 80 observations
Frequency: 4
Start time: 2000 1
End time: 2019 4
ts_info给出了这个ts对象的一些基本属性,包括长度、频率和起止时间。周期/频率是规则的时间序列对象很重要的属性,周期是规则且重复的时间单元,它将序列划分成连续等长的子集。频率定义了周期中的单元个数或者单元长度。在NGC中,周期是1年,观测单元是季度,所以频率就是4。
- ts_lags可视化
ts_lags(USgas, lags = c(12,24,36,48))
- ts_plot可视化函数
ts_plot(tvs_ts, title = "US Monthly total vehicle sales",
Ytitle = "Thousands of Vehicle",slider = TRUE)
- ts_cor函数
UK_ts <- ts(UKdaily$ND,
start = c(year(start_date), yday(start_date)),
frequency = 365)
ts_cor(UK_ts, lag.max = 365 * 4)#分析该系列与过去四年的滞后之间的关系
- ts_ma函数
two_sided_ma <- ts_ma(ts.obj = USVSales,#ts类型
n = c(2,5),#设定5号和11号的平均线
n_left = 6, n_right = 5, #设置12号平均线
plot = TRUE, multiple = TRUE,
margin = 0.04)
two_sided_ma
- ts_quantile函数
ts_quantile(UKgrid, period = "monthly", n = 2)
- ccf_plot可视化函数
ccf_plot(x = USVSales, y = USUnRate, lags = 0:12)
- ts_split函数
#创建训练和测试集
USgas_partitions <- ts_split(USgas, sample.out = 12)
train <- USgas_partitions$train
test <- USgas_partitions$test
- test_forecast模型预测函数
md <- auto.arima(train)
checkresiduals(md)
fc <- forecast (md, h = 12)
accuracy(fc,test)
test_forecast(actual = USgas,
forecast.obj = fc,
test = test)
naive_model <- naive(train, h = 12)
test_forecast(actual = USgas,
forecast.obj = naive_model,
test = test)
accuracy(naive_model,test)
snaive_model <- snaive(train, h = 12)
test_forecast(actual = USgas,
forecast.obj = snaive_model,
test = test)
accuracy(snaive_model, test)
- plot_forecast模型预测可视化函数
md_final <- auto.arima(USgas)
fc_final <- forecast (md_final, h = 12)
plot_forecast(fc_final,
title = "The US Natural Gas Consumption Forecast",
Xtitle = "Year",Ytitle = "Billion Cubic Feet")
#Confidence interval
fc_final2 <- forecast(md_final, h = 60, level = c(80,90))
plot_forecast(fc_final2,
title = "The US Natural Gas Consumption Forecast",
Xtitle = "Year",Ytitle = "Billion Cubic Feet")
- train_model模型训练
library(TSstudio)
USgas_forecast <- train_model(ts.obj = USgas,
periods = 6, models = "abehntw",
error = "MAPE",
window_size = 12,
h = 60, plot = FALSE)
- ts_grid网格函数
deep_grid <- ts_grid(train,
model = "HoltWinters",periods = 6,
window_space = 6,
window_test = 12,
hyper_params = list(alpha = seq(0.1,0.2,0.1),
beta = seq(0,0.2,0.1),
gamma = seq(0.2,0.3,0.1)),
parallel = TRUE, n.cores = 8)
plot_grid(deep_grid, type = "3D", top = 250)