下面的ucm_Forecast函数为纯时间序列的预测提供负值(无回归)。谁能帮我理解为什么?
我在这里只写多余的东西,因为我只允许在上面的段落中发帖。我收到消息“看起来您的帖子主要是代码;请添加一些详细信息”。 AI的另一面。
library(rucm)
reg<-data.frame(a=c(3,7,2,4,8,3,7,7,5,9),b=c(9,5,6,2,1,7,3,5,8,4))
reg<-as.matrix(reg)
reg1<-data.frame()
h<-4
x1=ts(c(5979511 ,13608701, 6193970 , 5269967 ,10424370 , 9504397),frequency=365.25/52)
u<-ucm_Forecast(x1,h,reg1)
u
#d<-cbind(as.data.frame(x1),reg[1:length(x1),])
ucm_Forecast=function(x,h,xreg){
if (ncol(xreg)>=1){
xregFit=data.frame(xreg[1:length(x),],check.names=FALSE)
x<-x/10000
d<-cbind(data.frame(x=x),xregFit)
independent <- paste0("`", names(d)[-1], "`", collapse = " + ")
fit_ucm<- ucm(as.formula(paste0("`",names(d[1]),"` ~", independent)), data = d, level = TRUE, slope=TRUE, cycle = TRUE, cycle.period = 365.25/52)
print("right before predict")
newdata1 <- SSModel(as.formula(paste0("rep(NA,h) ~ ", independent,
"+ SSMtrend(2, Q = list(fit_ucm$est.var.level,fit_ucm$est.var.slope))",
"+ SSMcycle(365.25/52, Q = fit_ucm$est.var.cycle)")), H = fit_ucm$irr.var, data=as.data.frame(xreg[(length(x)+1):(length(x)+h),]))
fcst<-predict(fit_ucm$model, newdata=newdata1)
#fcst<-predict(fitucm$model, xreg=xreg[(length(x)+1):(length(x)+h),])
print("right after predict")
fcst<-fcst*10000
print(fcst)
} else {
x<-x/10000
x<-as.data.frame(x)
fit_ucm<- ucm(formula = x~0, data = x, level = TRUE, slope=TRUE,cycle=TRUE,cycle.period=265.52/52)
newdata1 <- SSModel(as.formula(paste0("rep(NA,h) ~ ",
" SSMtrend(2, Q = list(fit_ucm$est.var.level,fit_ucm$est.var.slope))",
"+ SSMcycle(365.25/52, Q = fit_ucm$est.var.cycle)")), H = fit_ucm$irr.var)
fcst<-predict(fit_ucm$model, newdata=newdata1, n.ahead = h)
fcst<-fcst*10000
fcst
}
return(fcst)
}