if (FALSE) {
## load ICF data
data(ICFCoreSetCWP)
# adequate coding to get levels 1,..., max
H <- ICFCoreSetCWP[, 1:67] + matrix(c(rep(1, 50), rep(5, 16), 1),
nrow(ICFCoreSetCWP), 67,
byrow = TRUE)
xnames <- colnames(H)
# nonlinear PCA
icf_pca1 <- ordPCA(H, p = 2, lambda = c(5, 0.5, 0.0001), maxit = 1000,
Ks = c(rep(5, 50), rep(9, 16), 5),
constr = c(rep(TRUE, 50), rep(FALSE, 16), TRUE))
# estimated quantifications
icf_pca1$qs[[55]]
plot(1:9, icf_pca1$qs[[55]][,1], type="b",
xlab="category", ylab="quantification", col=1, main=xnames[55],
ylim=range(c(icf_pca1$qs[[55]][,1],icf_pca1$qs[[55]][,2],icf_pca1$qs[[55]][,3])))
lines(icf_pca1$qs[[55]][,2], type = "b", col = 2, lty = 2, pch = 2, lwd=2)
lines(icf_pca1$qs[[55]][,3], type = "b", col = 3, lty = 3, pch = 3, lwd=2)
# compare VAF
icf_pca2 <- ordPCA(H, p = 2, lambda = c(5, 0.5, 0.0001), maxit = 1000,
Ks = c(rep(5, 50), rep(9, 16), 5),
constr = c(rep(TRUE, 50), rep(FALSE, 16), TRUE),
CV = TRUE, k = 5)
icf_pca2$VAFtest
## load ehd data
require(psy)
data(ehd)
# recoding to get levels 1,..., max
H <- ehd + 1
# nonlinear PCA
ehd1 <- ordPCA(H, p = 5, lambda = 0.5, maxit = 100,
constr = rep(TRUE,ncol(H)),
CV = FALSE)
# resulting PCA on the scaled variables
summary(ehd1$pca)
# plot quantifications
oldpar <- par(mfrow = c(4,5))
for(j in 1:length(ehd1$qs))
plot(1:5, ehd1$qs[[j]], type = "b", xlab = "level", ylab = "quantification",
main = colnames(H)[j])
par(oldpar)
# include cross-validation
lambda <- 10^seq(4,-4, by = -0.1)
set.seed(456)
cvResult <- ordPCA(H, p = 5, lambda = lambda, maxit = 100,
constr = rep(TRUE,ncol(H)),
CV = TRUE, k = 5, CVfit = FALSE)
# optimal lambda
lambda[which.max(apply(cvResult$VAFtest,2,mean))]
}
Run the code above in your browser using DataLab