我有10次运行模拟的数据,存储为列表 . 我想用这个数据调用函数 FUN1
,而不重复代码10次 . FUN1
的输出是模型参数的值,这样:
$theta=5
$Theta= 0.5
$pi_1 = 0.6
$pi_2 = 0.4
$loglik_1 = 123.6
$loglik_2 = 23.56
那么,这就是 FUN1
的输出当我适应10次运行中的一次时 . 我希望所有运行的输出都没有为每次运行重新调整我的函数 .
我知道我们可以在R中使用循环族函数,例如 lapply
(列表)或 tapply
(向量)函数 . 我试过了他们两个但都没有用 .
这是我的数据:
library(VineCopula)
library(copula)
Runs = 10
Saveas = vector(mode = "list", length = Runs)
pb <- txtProgressBar(min = 0, max = Runs, style = 3)
for(j in 1:Runs) {
setTxtProgressBar(pb, j)
N=2000
dim = dim
U = runif(N, min=0,max=1)
X = matrix(NA, nrow=N, ncol=2)
inds <- U < 0.7
X[inds, ] <- rCopula(sum(inds),
claytonCopula(1, dim=2))
X[!inds, ] <- rCopula(N - sum(inds),
frankCopula(4, dim=2))
Saveas[[j]] = X
}
这是我的功能:
FUN1 <- EM_mixture_copula(data =
Saveas[[j]],pi_1=pi_1,pi_2=pi_2,theta = theta,
Theta=Theta, tol = .00001, maxit = 1000)
这是我尝试的错误:
> result <- tapply(X,FUN1,simplify = T)
Error in tapply(X, FUN, simplify = T) : arguments must have same length.
> Result <– lapply(X,FUN1)
Error in get(as.character(FUN), mode = "function", envir = envir) : object 'F' of mode 'function' was not found.
注意,copula的输出是矩阵(nrow = N,ncol = 2)(因为copula的维数是2) . 例如:
xx <-
rCopula(N=4 ,claytonCopula(0.5))
xx
[,1] [,2]
[1,] 0.6269311043 0.229429156
[2,] 0.3257583519 0.268244546
[3,] 0.7446442267 0.436335203
[4,] 0.3186246504 0.163209827
其中0.5是copula参数 .
有什么帮助吗?
1 回答
我想
dim
里面的dim
应该是1.因为,我没有安装数据或库,这个答案是未经验证的帖子 . 但是,它可以帮助您从这里找出解决方案 .