Learn R Programming

psych (version 1.0-17)

principal: Principal components analysis

Description

Does an eigen value decomposition and returns eigen values, loadings, and degree of fit for a specified number of components. Basically just is doing a principal components for n principal components. Can show the residual correlations as well. The quality of reduction in the squared correlations is reported by comparing residual correlations to original correlations.

Usage

principal(r, nfactors = 0, residuals = FALSE,rotate=FALSE, digits=2)

Arguments

r
a correlation matrix
nfactors
Number of components to extract
residuals
FALSE, do not show residuals, TRUE, report residuals
rotate
digits
digits =2 Accuracy of answers as well as display

Value

  • valuesEigen Values of all components -- useful for a scree plot
  • loadingsA standard loading matrix
  • fitFit of the model to the correlation matrix
  • residualResidual matrix -- if requested

Details

Useful for those cases where the correlation matrix is improper (perhaps because of SAPA techniques).

See Also

VSS,factor2cluster,factor.pa, factor.congruence

Examples

Run this code
#Four principal components of the Harmon 24 variable problem
#compare to a four factor principal axes solution using factor.congruence
pc <- principal(Harman74.cor$cov,4,rotate=TRUE)
pa <- factor.pa(Harman74.cor$cov,4,rotate=TRUE)
round(factor.congruence(pc,pa),2)

## The function is currently defined as
function(r,nfactors=0,residuals=FALSE,rotate=FALSE,digits=2) {
   n <- dim(r)[1]
  
   if (n!=dim(r)[2]) r <- cor(r,use="pairwise") # if given a rectangular matrix, the find the correlations first
    
    if (!residuals) { result <- list(values=c(rep(0,n)),loadings=matrix(rep(0,n*n),ncol=n),fit=0)} else { result <- list(values=c(rep(0,n)),loadings=matrix(rep(0,n*n),ncol=n),residual=matrix(rep(0,n*n),ncol=n),fit=0)}
    eigens <- eigen(r)    #call the eigen value decomposition routine
    result$values <- round(eigens$values,digits)
   if(nfactors >0) {loadings <- eigen.loadings(eigens)[,1:nfactors] } else {loadings <- eigen.loadings(eigens)
   nfactors <- n}
   	
   	if(nfactors >1) {
    	maxabs <- apply(apply(loadings,2,abs),2,which.max)
    	sign.max <- vector(mode="numeric",length=nfactors)
    	for (i in 1: nfactors) {sign.max[i] <- sign(loadings[maxabs[i],i])}
  
  		loadings <- loadings      	} else {if(nfactors >0) {mini <- min(loadings)
   			maxi <- max(loadings)
    		if (abs(mini) > maxi) {loadings <- -loadings }
    		loadings <- as.matrix(loadings)
    	 }}
    	
    colnames(loadings) <- paste("PC",1:nfactors,sep='')
    rownames(loadings) <- rownames(r)


    if(rotate) {loadings <- varimax(loadings)$loadings }  
   
    residual<- factor.residuals(r,loadings)
    r2 <- sum(r*r)
    rstar2 <- sum(residual*residual)
    if (residuals) {result$residual <-residual}
    result$fit <- round(1-rstar2/r2,digits)
    result$loadings <- round(loadings,digits)
    return(result)
   }

Run the code above in your browser using DataLab