首页 > 解决方案 > ucm 预测问题

问题描述

我在 R 中使用 ucm 来拟合时间序列模型,但无法从中做出预测。这是我的代码。

fitucm<- ucm(formula = x~0, data = x, level = TRUE, slope=TRUE)
fitucm
plot(x, ylab = "Sales")
lines(fitucm$s.level, col = "blue")
legend("topright", legend = c("sales","S_level"), col = c("black","blue"), lty = 1)
predict(fitucm$model, n.ahead = 12)

我收到以下错误。

Error in is.SSModel(model, na.check = TRUE, return.logical = FALSE) : 
  Model is not a proper object of class 'SSModel'. Check dimensions of system matrices.

所以我做了

  newdata1 <- SSModel(as.formula(paste0( "SSMtrend(1, Q =  list(fitucm$est.var.level))",
                                              "+ SSMcycle(365, Q = fitucm$est.var.cycle)")), H = fitucm$irr.var, data=x)
    pred<-predict(fitucm$model, newdata=newdata1)

但即使这样也行不通。我应该如何使用 ucm 模型进行预测?

dput(x)返回

structure(c(305.782684, 626.360641, 346.231655, 393.29374, 338.899657, 580.365259, 442.367196, 403.321188, 286.18491, 563.349778, 346.744479, 352.020911, 427.071993, 293.656763, 517.329794, 352.945061, 297.888494, 284.293553, 392.695937, 300.916378, 341.221086, 311.006093, 326.163204, 477.350303, 281.105887, 384.553564, 293.733286, 275.859299, 366.864272, 268.904152, 302.751203, 317.405698, 342.042525, 423.322273, 404.699796, 316.475645, 338.550231, 342.17192, 386.808382, 352.555864, 376.145766, 362.846193, 361.29863, 330.07548, 295.976383, 284.547718, 363.381609, 358.46016, 242.9839, 401.398545, 271.791979, 287.798405, 276.846896, 283.550623, 403.772366, 521.261135, 361.753866), .Tsp = c(1, 2.07323750855578, 52.1785714285714), class = "ts")

标签: r

解决方案


我已经调试了一些问题,这与导致is.SSModel()函数崩溃的数据频率有关。如果您x <- ts(x)在训练模型之前使用重置频率,则预测正确完成。进行预测的完整代码:

freqx <- frequency(x)
x <- ts(x)
fitucm <- ucm(formula = x~0, data = x, level = TRUE, slope=TRUE)
x2 <- ts(c(x,predict(fitucm$model, n.ahead = 12)), frequency = freqx)
print(x2)
Time Series:
Start = 1 
End = 2.30321697467488 
Frequency = 52.1785714285714 
 [1] 305.7827 626.3606 346.2317 393.2937 338.8997 580.3653 442.3672 403.3212
 [9] 286.1849 563.3498 346.7445 352.0209 427.0720 293.6568 517.3298 352.9451
[17] 297.8885 284.2936 392.6959 300.9164 341.2211 311.0061 326.1632 477.3503
[25] 281.1059 384.5536 293.7333 275.8593 366.8643 268.9042 302.7512 317.4057
[33] 342.0425 423.3223 404.6998 316.4756 338.5502 342.1719 386.8084 352.5559
[41] 376.1458 362.8462 361.2986 330.0755 295.9764 284.5477 363.3816 358.4602
[49] 242.9839 401.3985 271.7920 287.7984 276.8469 283.5506 403.7724 521.2611
[57] 361.7539 347.2653 348.2428 349.2203 350.1979 351.1754 352.1529 353.1305
[65] 354.1080 355.0855 356.0631 357.0406 358.0181

希望这可以帮助!


推荐阅读