使用svar :: VAR

时间:2019-06-25 16:20:27

标签: r

我正在估计带有回归变量的VAR模型,该回归变量是通过实现cheng y Liu方法的软件包tsoutliers获得的。 该模型的估计正确,但是当我尝试预测h向前的步长(我使用h = 2)时,我得到了NA值作为预测值。

因此,我的回归变量(矩阵)是哑变量,因为它们是AO类型的离群值。我还必须在参数dumvar中使用回归矩阵进行预测,因此我创建了行等于h(在示例中等于2)的矩阵。

我尝试了不同的时期进行预测,也更改了预测中包含的回归矩阵,使用随机统一值矩阵(我怀疑问题是零)。

# Usamos el método de Cheng y Liu
# Restringimos a encontrar outliers de tipo AO con valor de t mayor igual a 3.
agro18_out <- tsoutliers::tso(serie18_diff1[,"agro"], tsmethod = "auto.arima", 
                            args.tsmethod = list(ic = "aicc", seasonal = FALSE, approximation = FALSE,
                                                 stepwise = FALSE), types = c("AO"), cval = 2.0, 
                            discard.method = "en-masse", maxit.iloop = 100, maxit.oloop = 100)
industria18_out <- tsoutliers::tso(serie18_diff1[,"industria"], tsmethod = "auto.arima", 
                                 args.tsmethod = list(ic = "aicc", seasonal = FALSE, approximation = FALSE,
                                                      stepwise = FALSE), types = c("AO"), cval = 3.0,
                                 discard.method = "en-masse", maxit.iloop = 100, maxit.oloop = 100)
comercio18_out <- tsoutliers::tso(serie18_diff1[,"comercio"], tsmethod = "auto.arima", 
                                args.tsmethod = list(ic = "aicc", seasonal = FALSE, approximation = FALSE,
                                                     stepwise = FALSE), types = c("AO"), cval = 3.0,
                                discard.method = "en-masse", maxit.iloop = 100, maxit.oloop = 100)

# Estimación del VAR hasta 2018
# generamos los regresores hasta 2018-2
agro18_ao <- tsoutliers::outliers.effects(agro18_out$outliers, length(serie18_diff1[, "agro"]))
industria18_ao <- tsoutliers::outliers.effects(industria18_out$outliers, length(serie18_diff1[, "agro"]))
comercio18_ao  <- tsoutliers::outliers.effects(comercio18_out$outliers, length(serie18_diff1[, "agro"])) 
colnames(agro18_ao) <- paste("agr", colnames(agro18_ao), sep = "")
colnames(industria18_ao) <- paste("ind", colnames(industria18_ao), sep = "")
colnames(comercio18_ao) <- paste("com", colnames(comercio18_ao), sep = "")
xreg18 <- matrix(nrow = nrow(comercio18_ao), ncol = ncol(comercio18_ao) + ncol(industria18_ao) + ncol(agro18_ao))
xreg18 <- cbind(industria18_ao, comercio18_ao, agro18_ao) 
xreg18
sum(apply(xreg18, 1, sum) > 1) # No tienen AO en los mismos periodos

h = 2
xreg18_pred <- matrix(nrow = 2, ncol = ncol(comercio18_ao) + ncol(industria18_ao) + ncol(agro18_ao))
agro18_ao <- tsoutliers::outliers.effects(agro18_out$outliers, length(serie18_diff1[, "agro"]) + h)
industria18_ao <- tsoutliers::outliers.effects(industria18_out$outliers, length(serie18_diff1[, "agro"]) + h)
comercio18_ao  <- tsoutliers::outliers.effects(comercio18_out$outliers, length(serie18_diff1[, "agro"]) + h) 
colnames(agro18_ao) <- paste("agr", colnames(agro18_ao), sep = "")
colnames(industria18_ao) <- paste("ind", colnames(industria18_ao), sep = "")
colnames(comercio18_ao) <- paste("com", colnames(comercio18_ao), sep = "")


xreg18_pred <- cbind(industria18_ao, comercio18_ao, agro18_ao) 
xreg18_pred <- xreg18_pred[-seq_along(1:nrow(xreg18)),] 
xreg18_pred

# Estimamos inicialmente un var
var18 <- vars::VAR(serie18_diff1, ic = "AIC", type = "none", lag.max = 10, exogen = xreg18)

# predicciones

# Se genera error al usar el dumvar...Por qué??? 
pred18 <- predict(object = var18, n.ahead = 2, ci = 0.95, dumvar = xreg18_pred)

这是我的实际输出 $农业      第一低下CI [1,] NA NA NA 0.06412661 [2,] NA NA NA 0.06449499

$ industria      第一低下CI [1,] NA NA NA 0.05815483 [2,] NA NA NA 0.05861298

$ comercio      第一低下CI [1,] NA NA NA 0.07034564 [2,] NA NA NA 0.07128827

这是我的预期结果

$ agro               第一低下CI [1,] 0.0007866399 -0.07264581 0.07421909 0.07343245 [2,] -0.0007444915 -0.07810270 0.07661372 0.07735821

$ industria               第一低下CI [1,] -0.0042103795 -0.07351935 0.06509859 0.06930897 [2,] -0.0003505954 -0.07081144 0.07011025 0.07046085

$ comercio               第一低下CI [1,] -0.0050694363 -0.08668936 0.07655049 0.08161992 [2,] -0.0002143067 -0.08376733 0.08333872 0.08355302

0 个答案:

没有答案