est_camel = function(X,type = c("slasso", "clime")){
out1 = camel::camel.tiger(X,method = type)
#model selection using cross validation
out.select = camel::camel.tiger.select(out1, criterion = "cv")
if (!is.null(out.select$opt.icov))
Omega = as.matrix(out.select$opt.icov)
else
Omega = NULL
Omega
}
est_invPsi = function(res=NULL,sigma = NULL, type = c("diag","sparse")){
if(type == "diag"){
InvPsi = diag(sigma^(-1))
}else{
# here I use tiger, we can also use glasso
out1 = camel::camel.tiger(res, method = "slasso")
#model selection using cross validation
out.select = camel::camel.tiger.select(out1, criterion = "cv")
if (!is.null(out.select$opt.icov))
InvPsi = as.matrix(out.select$opt.icov)
else
InvPsi = NULL
}
return(InvPsi)
}
est_rank = function(X,type = c("max","bcv")){
if(type == "max"){
K = min(dim(X))
}else{
K_list = cate::est.factor.num(X, method = c("bcv"))
K = as.numeric(K_list$r)
}
return(K)
}
est_omega_wood = function(n,P,Lhat,Fhat,invPsi,type){
if(type == "inv_L"){
invC=solve( (1/n) * ( t(Lhat) %*% Lhat ) )
}else{
invC = as.vector((diag((1/n) * ( t(Lhat) %*% Lhat )))^(-1))
if(length(invC) == 1){
invC = matrix(invC,nrow = 1,ncol = 1)
}else{
invC = diag(invC)
}
}
# based on woodbury matrix identity
Omega = invPsi - invPsi %*% Fhat %*% solve(invC + t(Fhat) %*% invPsi %*% Fhat) %*% t(Fhat) %*% invPsi
return(Omega)
}
DM_diagonal = function(n,P){
index1=sort(sample(seq(1:n),(n/2)))
index2=seq(1:n)[-index1]
Sigma = diag(rchisq(P,3))
data = MASS::mvrnorm(n,rep(0,P),Sigma)
Xtest = data[index2,]
Xtrain = data[index1,]
Omega = diag((1/rchisq(P,3)))
return(list(Xtrain = Xtrain, Xtest = Xtest, Omega = Omega))
}
DM_toeplitz = function(n,P){
index1=sort(sample(seq(1:n),(n/2)))
index2=seq(1:n)[-index1]
Sigmatp=function(P){
a=array(0,dim=c(P,P))
for(i in 1:P){
for(j in 1:P){
a[i,j]=max(1-0.1*(abs(i-j)),0)
}
}
return(a)
}
Sigma = Sigmatp(P)
data = MASS::mvrnorm(n,rep(0,P),Sigma)
Xtest = data[index2,]
Xtrain = data[index1,]
Omega = solve(Sigma)
return(list(Xtrain = Xtrain, Xtest = Xtest, Omega = Omega))
}
DM_graph = function(n,P,g_type = NULL,para = NULL){
index1=sort(sample(seq(1:n),(n/2)))
index2=seq(1:n)[-index1]
if(is.null(g_type)){
# default is random graph
data_list = huge::huge.generator(n = n, d = P, prob = para, vis = TRUE)
}else{
data_list = huge::huge.generator(n = n, d = P, graph = g_type, g = para, vis = TRUE)
}
data = data_list$data
Xtest = data[index2,]
Xtrain = data[index1,]
Omega = data_list$omega
return(list(Xtrain = Xtrain, Xtest = Xtest, Omega = Omega))
}