# # dimensionality reduction using PCA # pca <- function( mat, dimensions ){ # ensure we have a numeric matrix mat <- matrix( as.numeric( mat ), nrow=nrow(mat)); # if desired dimensions are not provided, use the # number of columns in the output range if( missing( dimensions )){ ref <- BERT$.Excel(89); # xlfCaller dimensions <- ncol(ref); } # covariance matrix Sigma <- (t(mat) %*% mat)/nrow(mat); # svd and then reduce pc <- svd(Sigma)$u; mat %*% pc[,1:dimensions] } # # Add documentation to the excel "insert function" dialog # for the function and the arguments, using R attributes # attr( pca, "description" ) <- list( "Reduce dimensionality using principal components analysis (PCA)", mat = "matrix of data", dimensions = "(optional) number of dimensions in result" );