Varimax Rotation in Excel VBA

by Nick V. Flor • March 7, 2016 • @ProfessorF

Here’s my implementation of the Varimax Rotation algorithm — written in Excel VBA — based on IBM’s SPSS algorithm, which in turn is based on Harman (1976). The main difference is that I use a counter to control iteration. Note: I use custom functions, but the algorithm follows IBM’s math, so the functions should be self-explanatory.

Function fVarimax(F, loops)
    Dim H, Hinv, i, j, k, Lambda, LL, mRows, nCols, Omega
    Dim u, v, A, B, C, D, X, Y, P, rot
    Hinv = calcHinv(F)
    H = calcH(F)
    Lambda = fmMult(Hinv, F)

    getNumRowsCols Lambda, mRows, nCols
    For Z = 1 To loops 
        For i = 0 To (nCols - 2)
            For j = (i + 1) To (nCols - 1)
                u = calcU(Lambda, i, j)
                v = calcV(Lambda, i, j)
                A = 0
                B = 0
                C = 0
                D = 0
                For k = 0 To (mRows - 1)
                    A = A + u(k, 0)
                    B = B + v(k, 0)
                    C = C + (u(k, 0) ^ 2 - v(k, 0) ^ 2)
                    D = D + (2 * u(k, 0) * v(k, 0))
               Next
               X = D - (2 * A * B) / mRows
               Y = C - (A ^ 2 - B ^ 2) / mRows
               P = 0.25 * Atn(X / Y)
               rot = createRot(P)
               LL = rotateFactors(Lambda, i, j, rot)
               Lambda = replaceFactors(Lambda, LL, i, j)
            Next
        Next
    Next
    Omega = fmMult(H, Lambda)
    fVarimax = Omega
End Function
Harman, H. H. (1976). Modern factor analysis. University of Chicago Press.
IBM Corp (2013). IBM SPSS Statistics 23 Documentation. Armonk, NY: IBM Corp.  Retrieved March 6, 2016.
cropped-ProfessorFLogoFreedomSquare.png