# arbitrary positive definite covariance matrix
cov.nonsparse<- function(p, rgen = rnorm, rgen.diag = rchisq, df = 5)
{
m <- matrix(rgen(p*p), p, p)
m[upper.tri(m)] <- 0
tmp <- m %*% t(m)
tmp.svd <- svd(tmp)
tmp.svd$d <- rgen.diag(p, df)
tmp.svd$u %*% diag(tmp.svd$d) %*% t(tmp.svd$u)
}
# simulate n by p data matrix by a given covariance matrix
sim.data <- function(sc, n, rgen = rnorm)
{
l <- t(chol(sc))
p <- dim(sc)[1]
z <- matrix(rgen(p * n), p, n)
t(l %*% z)
}
p=30
n=30
cov<-cov.nonsparse(p)
x<-sim.data(cov,n)
# input n by p data matrix and assign parameters
novelist.cov.cv(x,lambda=c(0,0.5),delta=c(0,0.5,1),CV=FALSE)
# input n by p data matrix and find optimal parameters for covariance estimator
# achieves the least norm error to the true covariance matirx via cross validation
novelist.cov.cv(x, lambda=seq(0,1,by=0.1))
# input n by p data matrix and find optimal parameters for correlation estimator
# achieves the least norm error to the true correlation matirx via cross validation
novelist.cov.cv(x, lambda=seq(0,1,by=0.1), CV.cov=FALSE)
# input covariance matrix and assign parameters
novelist.cov.cv(cov(x),data=FALSE,lambda=c(0,0.5),delta=c(0,0.5,1),CV=FALSE)
# input correlation matrix and assign parameters
novelist.cov.cv(cor(x),data=FALSE,is.cov=FALSE,lambda=c(0,0.5),delta=c(0,0.5,1),CV=FALSE)
Run the code above in your browser using DataLab