# [R] KMO sampling adequacy and SPSS -- partial solution

G. Jay Kerns gkerns at ysu.edu
Mon Aug 6 19:37:11 CEST 2007

```Hello,

This is in response to a post from a couple of years back regarding
(http://tolstoy.newcastle.edu.au/R/help/05/12/17233.html)

As it turns out, last year Trujillo-Ortiz et al. at the Universidad
Autonoma de Baja California wrote and posted a script for MATLAB that
does the job.  You can see it (with a discussion of KMO statistics) at

Please see their post for documentation/citations/references, etc.

I translated the code into R-speak and added a little to the output;
the function and an example are copied below. You can just copy/paste
to the console to try it out, provided you have the MASS package.

Cheers,
Jay

***************************************************
G. Jay Kerns, Ph.D.
Assistant Professor / Statistics Coordinator
Department of Mathematics & Statistics
Youngstown State University
Youngstown, OH 44555-0002 USA
Office: 1035 Cushwa Hall
Phone: (330) 941-3310 Office (voice mail)
-3302 Department
-3170 FAX
E-mail: gkerns at ysu.edu
http://www.cc.ysu.edu/~gjkerns/

# KMO Kaiser-Meyer-Olkin Measure of Sampling Adequacy
kmo = function( data ){

library(MASS)
X <- cor(as.matrix(data))
iX <- ginv(X)
S2 <- diag(diag((iX^-1)))
AIS <- S2%*%iX%*%S2                      # anti-image covariance matrix
IS <- X+AIS-2*S2                         # image covariance matrix
Dai <- sqrt(diag(diag(AIS)))
IR <- ginv(Dai)%*%IS%*%ginv(Dai)         # image correlation matrix
AIR <- ginv(Dai)%*%AIS%*%ginv(Dai)       # anti-image correlation matrix
a <- apply((AIR - diag(diag(AIR)))^2, 2, sum)
AA <- sum(a)
b <- apply((X - diag(nrow(X)))^2, 2, sum)
BB <- sum(b)
MSA <- b/(b+a)                        # indiv. measures of sampling adequacy

AIR <- AIR-diag(nrow(AIR))+diag(MSA)  # Examine the anti-image of the
# correlation matrix. That is the
# negative of the partial correlations,
# partialling out all other variables.

kmo <- BB/(AA+BB)                     # overall KMO statistic

# Reporting the conclusion
if (kmo >= 0.00 && kmo < 0.50){
test <- 'The KMO test yields a degree of common variance
unacceptable for FA.'
} else if (kmo >= 0.50 && kmo < 0.60){
test <- 'The KMO test yields a degree of common variance miserable.'
} else if (kmo >= 0.60 && kmo < 0.70){
test <- 'The KMO test yields a degree of common variance mediocre.'
} else if (kmo >= 0.70 && kmo < 0.80){
test <- 'The KMO test yields a degree of common variance middling.'
} else if (kmo >= 0.80 && kmo < 0.90){
test <- 'The KMO test yields a degree of common variance meritorious.'
} else {
test <- 'The KMO test yields a degree of common variance marvelous.'
}

ans <- list(  overall = kmo,
report = test,
individual = MSA,
AIS = AIS,
AIR = AIR )
return(ans)

}    # end of kmo()

#  Try Trujillo-Ortiz et al. example
X = scan()
4     1     4     5     2     3     6     7
4     2     7     6     6     3     3     4
6     4     3     4     2     5     7     7
5     3     5     4     3     4     6     7
5     2     4     5     2     5     5     6
6     3     3     5     4     4     7     7
6     2     4     4     4     3     4     5
4     1     3     4     3     3     5     6
5     3     4     3     4     3     6     6
5     4     3     4     4     4     6     7
6     2     4     4     4     3     7     5
5     2     3     3     3     3     7     6

dim(X)=c(8,12)
X=t(X)
kmo(X)

```