我有一个矢量列表,并希望根据这些矢量制定一些条件。除非这个条件
,否则我的所有条件都会很好if (is.null(w[[i]]))
它返回此错误:
Myfu(W = NULL,垫) w [[i]]出错: 尝试在integerOneIndex中选择少于一个元素
w <- list(c(0.8,0.2),c(0.5,0.4))
mat1 <- c(0, 1, 3, 4, 4,
0, 0, 3, 4, 1,
0, 0, 0, 4, 1,
0, 0, 0, 0, 3,
0, 0, 0, 0, 0)
mat1 <- matrix(mat1, 5, 5)
# define R-vine pair-copula parameter matrix
mat2 <- c(0, 0.2, 0.9, 1.5, 3.9,
0, 0, 1.1, 1.6, 0.9,
0, 0, 0, 1.9, 0.5,
0, 0, 0, 0, 4.8,
0, 0, 0, 0, 0)
mat2 <- matrix(mat2, 5, 5)
mat <- list(mat1,mat2)
Myfu <- function(w,mat){
m <- length(w)
for(i in 1:m){
if (is.null(w[[i]]))
w[[i]] <- rep.int(1/m, m)
if (sum(w[[i]]) > 1)
stop("w must add to one")
if(any(w[[i]] < 0))
stop("w must be positive")
}
return(w)
}
请帮忙吗?