Description Usage Arguments Value Examples
View source: R/BICforPCAwithSparseWeights.R
A function that returns the Bayesian information criterion (BIC) given a set of tuning parameters and a sparse weight based PCA method
1 | BICforPCAwithSparseWeights(X, ncomp, FUN, ...)
|
X |
A data matrix of class 'matrix' |
ncomp |
An integer specifying the number of components |
FUN |
A pointer to a function (i.e. the function name with no brackets) that performs sparse weight based PCA. The function should return a list containing a matrix object called "W" that contains the component weights |
... |
specify all the arguments the function in FUN needs |
The following items in a list
BIC
The BIC given the set of tuning parameters
nNonZeroCoef
The number of non-zero coefficients in the model
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
J <- 10
ncomp <- 3
X <- matrix(rnorm(100 * J), 100, J)
BICforPCAwithSparseWeights(X = X, ncom = ncomp, FUN = scads, ncomp, ridge = 0, lasso = rep(0.1, ncomp),
constraints = matrix(1, J, ncomp), Wstart = matrix(0, J, ncomp),
itr = 100000, nStarts = 1, printLoss = FALSE, tol = 10^-5)
#Extended example: select LASSO parameter using BICforPCAwithSparseWeights with scads()
#create sammple data
ncomp <- 3
J <- 30
comdis <- matrix(1, J, ncomp)
comdis <- sparsify(comdis, 0.7) #set 70 percent of the 1's to zero
variances <- makeVariance(varianceOfComps = c(100, 80, 90), J = J, error = 0.05) #create realistic eigenvalues
dat <- makeDat(n = 100, comdis = comdis, variances = variances)
X <- dat$X
#Use cross-validation to look for the data generating structure
lasso <- seq(0, 1, length.out = 500)
BIC <- rep(NA, length(lasso))
nNonZeroCoef <- rep(NA, length(lasso))
for (i in 1:length(lasso)) {
print(i)
res <- BICforPCAwithSparseWeights(X = X, ncomp = ncomp, FUN = scads, ncomp, ridge = 0, lasso = rep(lasso[i], ncomp),
constraints = matrix(1, J, ncomp), Wstart = matrix(0, J, ncomp),
itr = 100000, nStarts = 1, printLoss = FALSE, tol = 10^-5)
BIC[i] <- res$BIC #store BIC for each lasso
}
#make a plot of all the BIC's choose the minimal BIC or a model with a small BIC
#BIC tends to select very sparse models
x <- 1:length(BIC)
plot(x, BIC)
#Select the model with the lowest BIC (or alternatively select a model around the model with the lowest BIC)
best <- which.min(BIC)
#Do the analysis with the "winning" structure
results <- scads(X = X, ncomp = ncomp, ridge = 0, lasso = rep(lasso[best], ncomp),
constraints = matrix(1, J, ncomp), Wstart = matrix(0, J, ncomp),
itr = 100000, nStarts = 1, printLoss = TRUE , tol = 10^-5)
results$W #inspect results of the estimation
dat$P[, 1:ncomp] #inspect data generating model
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.