if((dimension< 1)|(dimension> n))停止时出错("错误的嵌入维度"):参数长度为零

时间:2018-01-17 13:56:58

标签: r error-handling

我的代码如下:

unemp <- c(1:10)
bsp_li <- list(c(1:10),c(11:20),c(21:30))
var_data_rep <- lapply(bsp_li, function(x) {cbind(as.numeric(x), as.numeric(unemp))} ) # Create VAR data matrices
var_data_rep2 <- lapply(var_data_rep, function(x) {colnames(x) = c("rGDP", "U"); return(x)}) # Name columns
var_data_rep_ts <- lapply(var_data_rep2, function(x) {ts(x, frequency=1, start=c(1977))} ) # Make it ts again
var_data_rep_lag <- lapply(var_data_rep_ts, function(x) {VARselect(x, lag.max = 5, type = "const")} ) # Take lag with lowest SC criteria (VAR.pdf)
VARgdp_rep <- lapply(var_data_rep_ts, function(x) {VAR(x, p = var_data_rep_lag$x$selection[['SC(n)']], type = "const"); return(x)} ) # Lag=lowest SC criteria from var_data_rep_lag

如果我只运行最后一行,r总是给我错误:

Error in if ((dimension < 1) | (dimension > n)) stop("wrong embedding dimension") : argument is of length zero Called from: embed(y, dimension = p + 1)

但是,如果我用Source运行它,那么它似乎有用..任何建议?

1 个答案:

答案 0 :(得分:0)

这似乎有用(至少不会抛出任何错误):

VARgdp_rep <- 
    lapply(index(var_data_rep_ts), 
        function(x) {
                  res <- VAR(var_data_rep_ts[[x]], p = 
var_data_rep_lag[[x]]$selection[['SC(n)']], type = "const"); 
                  return(res)
          } 
       )

在你的代码中,return(x)很奇怪,因为在进行VAR计算之后......你只需返回x withc传递给函数。 而$ x似乎没有任何意义。